Input types let you send structured data to GraphQL queries or mutations. They help organize complex information clearly.
Input type for complex arguments in GraphQL
Introduction
Syntax
GraphQL
input InputName {
field1: Type1
field2: Type2
...
}
# Then use it in a query or mutation
mutation SomeMutation($input: InputName!) {
someAction(input: $input) {
resultField
}
}Input types are defined with the input keyword, not type.
Input types can only contain fields with input types (scalars, enums, or other input types).
Examples
GraphQL
input AddressInput {
street: String!
city: String!
zip: String
}GraphQL
mutation AddUser($user: UserInput!) {
addUser(user: $user) {
id
name
}
}GraphQL
input UserInput {
name: String!
age: Int
address: AddressInput
}Sample Program
This example shows how to define input types for a user and their address, then use them in a mutation to create a user with detailed info.
GraphQL
input AddressInput {
street: String!
city: String!
zip: String
}
input UserInput {
name: String!
age: Int
address: AddressInput
}
mutation CreateUser($user: UserInput!) {
createUser(user: $user) {
id
name
age
address {
street
city
zip
}
}
}Important Notes
Input types cannot have fields that are other object types; only input types or scalars.
Use ! to mark fields as required.
Input types help keep your API organized and easier to maintain.
Summary
Input types group related input fields for queries and mutations.
They allow nested and reusable input structures.
Defined with input keyword and used as argument types.
Practice
1. What is the main purpose of using
input types in GraphQL queries or mutations?easy
Solution
Step 1: Understand the role of input types
Input types in GraphQL are designed to group related fields together for passing complex arguments.Step 2: Differentiate input from output types
Input types are not for output or database creation but for structuring inputs in queries or mutations.Final Answer:
To group related input fields into a reusable structure -> Option CQuick Check:
Input types = group related inputs [OK]
Hint: Input types group inputs, not outputs or tables [OK]
Common Mistakes:
- Confusing input types with output types
- Thinking input types create database tables
- Assuming input types are for comments
2. Which of the following is the correct way to define an input type in GraphQL?
easy
Solution
Step 1: Recall GraphQL syntax for input types
Input types are defined using theinputkeyword, nottype,interface, orenum.Step 2: Verify the structure
The correct syntax isinput UserInput { name: String, age: Int }.Final Answer:
input UserInput { name: String, age: Int } -> Option BQuick Check:
Input types use 'input' keyword [OK]
Hint: Use 'input' keyword to define input types [OK]
Common Mistakes:
- Using 'type' instead of 'input' for input types
- Confusing input with interface or enum
- Missing curly braces or commas
3. Given the input type definition:
and the mutation:
What is the correct way to pass the variable
input AddressInput { street: String, city: String }and the mutation:
mutation AddAddress($address: AddressInput!) { addAddress(address: $address) { id } }What is the correct way to pass the variable
$address in a GraphQL request?medium
Solution
Step 1: Understand input type structure
The input type expects an object with fieldsstreetandcity, both strings.Step 2: Match variable format to input type
The variable must be a JSON object with keys matching the input fields, so { "address": { "street": "123 Main St", "city": "Springfield" } } is correct.Final Answer:
{ "address": { "street": "123 Main St", "city": "Springfield" } } -> Option DQuick Check:
Input variables match input type fields [OK]
Hint: Pass input variables as objects matching input type fields [OK]
Common Mistakes:
- Passing input as a string instead of object
- Using array instead of object for input
- Passing null when input is required
4. Consider this input type and mutation:
If you send the variable
input ProductInput { name: String!, price: Float! }mutation AddProduct($product: ProductInput!) { addProduct(product: $product) { id } }If you send the variable
{ "product": { "name": "Book" } }, what error will occur?medium
Solution
Step 1: Check required fields in input type
Bothnameandpriceare marked with!, meaning they are required.Step 2: Analyze provided variables
The variable only providesname, missingprice, so GraphQL will raise a runtime error for missing required field.Final Answer:
Runtime error: price field is required but missing -> Option AQuick Check:
Required fields missing cause runtime errors [OK]
Hint: Required fields must be present in input variables [OK]
Common Mistakes:
- Assuming missing required fields cause syntax errors
- Thinking mutation succeeds without required fields
- Confusing field types (e.g., name as Int)
5. You want to create a mutation that accepts a complex input type with nested inputs for a user profile:
Which of the following mutation argument definitions correctly uses these input types?
input AddressInput { street: String!, city: String! }input UserProfileInput { name: String!, age: Int, address: AddressInput! }Which of the following mutation argument definitions correctly uses these input types?
hard
Solution
Step 1: Identify correct input type for mutation argument
The mutation should accept the full user profile input, which isUserProfileInput, not just the address or a string.Step 2: Check required modifier
Since the profile is required, the argument should beUserProfileInput!to enforce presence.Final Answer:
mutation updateUserProfile(profile: UserProfileInput!) { ... } -> Option AQuick Check:
Use full nested input type with ! for required [OK]
Hint: Use full nested input type with ! if required [OK]
Common Mistakes:
- Using nested input type instead of full input type
- Omitting required ! modifier
- Using scalar types instead of input types
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
