An input type in GraphQL is a special kind of type used to define complex arguments for queries or mutations. It groups multiple fields together so you can pass structured data as a single argument.
Click to reveal answer
intermediate
Why do we use input types instead of regular object types for arguments?
Input types are designed specifically for input data and cannot have fields that are other object types with resolvers. This keeps input data simple and safe, avoiding execution of server-side logic during input parsing.
Click to reveal answer
beginner
How do you define an input type for a complex argument in GraphQL?
You define an input type using the input keyword followed by the input name and its fields. For example:
mutation AddAddress($address: AddressInput!) { addAddress(address: $address) { id } }
What is the correct way to pass the variable $address in a GraphQL request?
medium
A. { "address": null }
B. { "address": "123 Main St, Springfield" }
C. { "address": ["123 Main St", "Springfield"] }
D. { "address": { "street": "123 Main St", "city": "Springfield" } }
Solution
Step 1: Understand input type structure
The input type expects an object with fields street and city, 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 D
Quick Check:
Input variables match input type fields [OK]
Hint: Pass input variables as objects matching input type fields [OK]