What is an input type in GraphQL and when do you use one?
Learn what a GraphQL input type is, how it differs from object types, and when to use one to pass complex arguments into mutations, with clear code examples.
Expected Interview Answer
An input type in GraphQL is a special object type, declared with the 'input' keyword, used to pass structured, complex arguments into a query or mutation instead of listing many separate scalar arguments.
Regular object types (declared with 'type') are for data you return to the client and may contain fields with resolvers and circular references. Input types are strictly for data coming in: their fields can only be scalars, enums, other input types, or lists of those. You typically use them for mutation payloads (like creating or updating a record) so a single 'input' argument carries a whole form's worth of fields, keeping the schema clean and versionable.
- Groups many arguments into one reusable, named structure
- Keeps mutation and query signatures short and readable
- Enforces validation and required/optional rules at the schema level
- Reusable across multiple operations and easy to evolve
- Prevents accidentally using return-only object types as arguments
AI Mentor Explanation
An input type is like the team sheet a captain hands the match referee before play: one structured form carrying batting order, wicket-keeper, and twelfth man together. You do not shout each name across the field separately; you fill one agreed sheet with defined slots, and the referee accepts only that format for entering a team into the game.
Step-by-Step Explanation
Step 1
Declare with the input keyword
Use 'input CreateUserInput { ... }' rather than 'type', signalling the structure is for incoming arguments only.
Step 2
Add only input-safe fields
Fields may be scalars, enums, lists, or other input types — never object types with resolvers.
Step 3
Mark required vs optional
Use '!' for required fields (like 'email: String!') and leave optional fields nullable.
Step 4
Reference it as an argument
Pass it in a mutation signature, e.g. 'createUser(input: CreateUserInput!): User'.
Step 5
Read it in the resolver
Destructure the single 'input' argument object and use its fields to perform the operation.
What Interviewer Expects
- Knows input types use the 'input' keyword, not 'type'
- Understands input types are for arguments, output types for return data
- Knows input fields cannot be object types or have resolvers
- Can explain why mutations commonly take a single input argument
- Can write a small schema and resolver example
Common Mistakes
- Trying to use a regular 'type' as an argument
- Adding resolver-backed or object-type fields to an input type
- Confusing input types with interfaces or unions
- Forgetting the '!' to mark required input fields
- Duplicating the same argument list across many mutations instead of reusing an input
Best Answer (HR Friendly)
“An input type is a way to bundle several pieces of information into one neat form so a GraphQL request can send them together. It is mostly used when creating or updating data, so instead of listing lots of separate values you pass one organized object.”
Code Example
input CreateUserInput {
name: String!
email: String!
age: Int
role: Role = MEMBER
}
type Mutation {
createUser(input: CreateUserInput!): User!
}const resolvers = {
Mutation: {
createUser: (_parent, { input }, context) => {
const { name, email, age, role } = input
return context.db.users.create({ name, email, age, role })
},
},
}Follow-up Questions
- Why can't an input type contain a field of an object type?
- How do you set a default value on an input field?
- Can input types be nested inside other input types?
- How do you validate input beyond the schema's type checks?
- What is the difference between an input type and an argument list?
MCQ Practice
1. Which keyword declares an input type in GraphQL?
Input types are declared with the 'input' keyword; 'type' declares output object types.
2. Which field is NOT allowed inside an input type?
Input types may only contain scalars, enums, lists, and other input types — never resolver-backed object types.
3. Input types are most commonly used for?
They group complex arguments — typically the payload of a create/update mutation — into one structured value.
Flash Cards
What keyword defines a GraphQL input type? — 'input' — e.g. 'input CreateUserInput { ... }'.
Input type vs object type? — Object types return data and can have resolvers; input types carry incoming arguments and cannot.
What fields can an input type contain? — Scalars, enums, lists, and other input types — no object types.
Why use one input instead of many arguments? — It groups a whole payload into one reusable, readable, versionable structure.