GraphQL Input Types
In GraphQL, input types are used to pass structured data as arguments to mutations, queries, or field resolvers. They are similar to object types, but are specifically designed for inputs — not outputs.
Why Use Input Types?
- Simplify and group related arguments.
- Improve readability and maintainability.
- Allow for nested structured inputs.
- Enforce input validation at the schema level.
Defining Input Types
Input types are defined using the input
keyword.
You can then use this input type as an argument in a mutation:
Example Mutation
mutation CreateUser {
createUser(input: { name: "Alice", email: "alice@example.com", age: 30 }) {
id
name
}
}
Key Differences: Input vs Object Types
Feature | Object Type (type ) |
Input Type (input ) |
---|---|---|
Used for response data | ✅ | ❌ |
Used for request data | ❌ | ✅ |
Can have resolvers | ✅ | ❌ |
Can reference other types | ✅ | ❌ (only input types, enums, and scalars) |
❗ Input types cannot reference object types. They can only reference other input types, scalars, or enums.
Nesting Input Types
Input types can include other input types to support deeply structured data.
input AddressInput {
street: String!
city: String!
zip: String!
}
input RegisterUserInput {
name: String!
email: String!
address: AddressInput!
}
Input Type Validation
Validation can be enforced:
- At the schema level (e.g., non-null fields)
- In resolvers (e.g., custom business logic)
- With custom scalars (e.g.,
Email
,Date
,PhoneNumber
)
Best Practices
- ✅ Use input types for complex arguments
- ✅ Validate inputs both in the schema and server-side
- ✅ Use enums and custom scalars to constrain values
- ✅ Group related arguments into input types instead of flat arguments
- ✅ Keep naming consistent (e.g.,
CreateUserInput
,UpdateProfileInput
)
Tools & Resources
- GraphQL Input Types – Official Docs
- Apollo Server – Using Input Types
- graphql-scalars — for validating common input formats
Summary
- Input types define structured, validated input for your GraphQL operations.
- They're ideal for mutations and complex arguments.
- They improve your API's clarity, reusability, and type safety.