Introduction
Input types let you send data into GraphQL queries or mutations in a clear and organized way.
Input types let you send data into GraphQL queries or mutations in a clear and organized way.
input InputName {
field1: Type1
field2: Type2
...
}Input types are defined using the input keyword, not type.
Fields inside input types can only be scalars, enums, or other input types.
input UserInput {
name: String!
age: Int
email: String
}input PostFilter {
titleContains: String
published: Boolean
}This mutation uses an input type to create a new user with username, email, and optional age.
input NewUserInput {
username: String!
email: String!
age: Int
}
mutation CreateUser($input: NewUserInput!) {
createUser(input: $input) {
id
username
email
age
}
}Input types cannot have fields that are other object types, only input types or scalars.
Always mark required fields with ! to ensure they are provided.
Input types organize data sent into queries or mutations.
They help keep your API clean and reusable.
Use input keyword and only allowed field types.
input types in GraphQL?input followed by the name and curly braces for fields.input UserInput { ... }. type UserInput { name: String! age: Int } uses type which is for output types. Options C and D have invalid syntax.input ProductInput { id: ID! name: String! price: Float }id and name are required (non-null). price is optional. Types must match: id is ID (string), name is String, price is Float.id and uses string for price (should be Float). mutation { addProduct(input: { id: "123", price: 1.5 }) { id name } } misses required name.input UserInput { id: ID! name: String age: Int! }id. Which input type definition is correct?id must be required (non-null). Other fields should be optional, so no exclamation marks.id: ID! required and others optional. input UpdateUserInput { id: ID name: String! email: String! age: Int! } makes id optional and others required, wrong. input UpdateUserInput { id: ID! name: String! email: String! age: Int! } makes all fields required, wrong. input UpdateUserInput { id: ID! name: String! email: String age: Int } makes name required, which is not desired.Pass the test and this pattern moves unknown