Input Types in GraphQL — Complete Guide
In this tutorial, you will learn about Input Types in Graphql. We cover key concepts, practical examples, and best practices to help you master this topic.
Input types in GraphQL define structured objects for passing complex data to mutations and queries. Unlike object types that describe return shapes, input types describe argument shapes. They use the input keyword and cannot have interfaces or unions.
What You'll Learn
- Defining input types with the input keyword
- Nesting input types for complex data structures
- Required vs optional fields in inputs
- Reusing input types across mutations
Why It Matters
Input types keep mutation signatures clean and maintainable. Without them, mutations with many fields would require long argument lists. Input types also enable validation rules at the schema level.
Real-World Use
Shopify's productCreate mutation accepts a ProductInput type with over 30 fields for title, description, variants, images, and options. GitHub's commitMutation uses an input type for the commit message, tree, and author details.
flowchart TD
Mutation[Mutation] --> Input[Input Type]
Input --> Field1[Required: title String!]
Input --> Field2[Optional: description String]
Input --> Field3[Nested: metadata JSON]
Input --> Field4[Enum: status OrderStatus]
Input --> Return[Return Payload Type]
Teacher Mindset
Input types mirror the shape of the data the mutation accepts. Keep them focused on a single mutation or reuse them across similar mutations. Do not mix input types with output object types.
Code Examples
# Example 1: Simple input type for creating a user
input CreateUserInput {
username: String!
email: String!
password: String!
fullName: String
avatarUrl: String
}
type Mutation {
createUser(input: CreateUserInput!): User!
updateUser(id: ID!, input: UpdateUserInput!): User!
}
# Example 2: Nested input types
input AddressInput {
street: String!
city: String!
state: String!
zipCode: String!
country: String!
}
input CreateCustomerInput {
name: String!
email: String!
address: AddressInput
tags: [String!]
}
# Example 3: Reusing input types with partial fields
input UpdateUserInput {
username: String
email: String
fullName: String
}
# Only the fields that should be updatable
# All fields are optional so clients send only changed values
Common Mistakes
- Using object types instead of input types for mutation arguments
- Making every input field required when partial updates need optional fields
- Duplicating input field definitions instead of creating reusable input types
- Nesting input types too deeply, making client code complex
- Forgetting to add validation descriptions to input type fields
Practice
- Define an input type for creating a blog post with title, body, and tags.
- Create a nested address input type and use it in a createCustomer mutation.
- Write both create and update input types for a Product type.
- Add input validation through schema descriptions.
- Challenge: Design a polymorphic input type using a discriminator field.
FAQ
Mini Project
Define input types for all mutations in your e-commerce schema. Create CreateProductInput, UpdateProductInput, CreateOrderInput, AddToCartInput. Use nested AddressInput for shipping details.
What's Next
Next, you will learn about arguments in GraphQL, including how to pass parameters to fields for filtering, sorting, and pagination.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro