What are Input Types in GraphQL?

In GraphQL, input types are a special kind of type that allows you to define the structure of the data that can be passed as arguments to queries and mutations. They are particularly useful for complex data structures, enabling clients to send structured data in a clear and type-safe manner.

Key Features of Input Types

  • Structured Data: Input types allow you to define complex objects with multiple fields, making it easier to pass structured data to your API.
  • Type Safety: By defining input types, you ensure that the data sent to the server adheres to a specific structure, reducing the likelihood of errors.
  • Reusability: Input types can be reused across different queries and mutations, promoting consistency in your API.

Defining Input Types

Input types are defined using the input keyword in your GraphQL schema. Here’s a simple example of how to define an input type for creating a user:


input UserInput {
name: String!
email: String!
}

In this example, the UserInput input type has two fields: name and email. The exclamation mark (!) indicates that these fields are required.

Using Input Types in Mutations

Input types are commonly used in mutations to define the structure of the data being sent to the server. Here’s how you can use the UserInput type in a mutation to create a new user:


type Mutation {
createUser (input: UserInput!): User
}

In this example, the createUser mutation takes a single argument called input, which is of type UserInput.

Sample Mutation Implementation


const resolvers = {
Mutation: {
create:User (parent, { input }) => {
const newUser = {
id: generateId(), // Assume a function to generate unique IDs
name: input.name,
email: input.email,
};
// Save the new user to the database (not shown)
return newUser ;
},
},
};

Sample Mutation Query

Here’s how you might call the createUser mutation using the UserInput type:


mutation {
createUser (input: { name: "Alice", email: "alice@example.com" }) {
id
name
email
}
}

Sample Response

The server's response to the above mutation might look like this:


{
"data": {
"createUser ": {
"id": "1",
"name": "Alice",
"email": "alice@example.com"
}
}
}

Nested Input Types

Input types can also be nested, allowing you to create complex data structures. For example, if you want to include an address when creating a user, you can define an AddressInput type:


input AddressInput {
street: String!
city: String!
country: String!
}

input UserInput {
name: String!
email: String!
address: AddressInput
}

You can then use the nested input type in your mutation:


type Mutation {
createUser (input: UserInput!): User
}

Conclusion

Input types in GraphQL are a powerful feature that allows you to define structured and type-safe data for queries and mutations. By using input types, you can ensure that the data sent to your API adheres to a specific structure, improving the reliability and maintainability of your GraphQL services. Understanding how to define and use input types is essential for building robust GraphQL APIs that handle complex data interactions.