0
0
GraphqlConceptBeginner · 3 min read

What is Input Type in GraphQL: Definition and Usage

In GraphQL, an input type is a special kind of type used to define the shape of data that clients can send to the server in mutations or queries. It allows you to specify structured input objects with fields, making it easier to pass complex data as arguments.
⚙️

How It Works

Think of an input type in GraphQL like a form you fill out when ordering food online. Instead of sending just a single value, you send a structured set of information like your name, address, and order details all grouped together. This grouping helps the server understand exactly what data you are sending.

In GraphQL, input types define these groups of fields that clients can send as arguments to mutations or queries. Unlike regular object types used for responses, input types are designed only for input and cannot have fields that resolve to other objects. This keeps the input simple and predictable.

Using input types makes your API cleaner and easier to maintain because you can reuse the same input structure in many places and validate the data shape before processing it.

💻

Example

This example shows how to define an input type for creating a new user, and how to use it in a mutation.

graphql
input CreateUserInput {
  name: String!
  email: String!
  age: Int
}

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

type User {
  id: ID!
  name: String!
  email: String!
  age: Int
}
🎯

When to Use

Use input types whenever you need to send complex or multiple pieces of data to your GraphQL server, especially in mutations that create or update data. For example, when a user signs up, updates their profile, or submits a form with many fields, input types help organize and validate that data.

They are also useful to keep your API consistent and clear, as you can reuse input types across different mutations or queries that require the same data structure.

Key Points

  • Input types define structured data sent from client to server.
  • They are used mainly in mutation arguments but can be used in queries.
  • Input types cannot have fields that resolve to other objects, only scalars or other input types.
  • They help keep APIs clean, reusable, and easier to validate.

Key Takeaways

Input types in GraphQL define the shape of data clients send to the server.
They are mainly used as arguments in mutations to pass complex data.
Input types only contain fields with scalar types or other input types, not regular object types.
Using input types improves API clarity, reusability, and validation.
They help organize data like filling out a form before sending it to the server.