Mutation Input Types
You'll learn how to use GraphQL input object types to pass complex, structured data into mutations cleanly and safely.
What Are Input Types?
When you write mutations that need multiple related fields—like creating a user with name, email, and address—passing each as a separate argument becomes messy. GraphQL input types let you bundle related data into a single, structured argument, making your mutations cleaner and more maintainable.
Think of it like filling out a form: instead of handing someone 15 separate sticky notes with information, you hand them one completed form with all the details organized.
Why Use Input Types?
Without input types, a mutation might look like this:
createUser(name: String!, email: String!, street: String!, city: String!, zip: String!)
That's hard to read and maintain. With an input type, you define a reusable structure:
input CreateUserInput {
name: String!
email: String!
address: AddressInput!
}
input AddressInput {
street: String!
city: String!
zip: String!
}
mutation {
createUser(input: CreateUserInput!): User
}
Now your mutation call is simple:
mutation CreateUser($input: CreateUserInput!) {
createUser(input: $input) {
id
name
email
}
}
With variables:
{
"input": {
"name": "Alice",
"email": "alice@example.com",
"address": {
"street": "123 Main St",
"city": "Boston",
"zip": "02101"
}
}
}
Input types can be nested, reused across multiple mutations, and make validation easier. They're the standard pattern for complex mutations.
Key Takeaway: Input object types bundle related mutation arguments into structured, reusable units, keeping your GraphQL mutations organized and your API clean.