0
0
GraphQLquery~5 mins

Input types in GraphQL

Choose your learning style9 modes available
Introduction

Input types let you send data into GraphQL queries or mutations in a clear and organized way.

When you want to add new data to a database using a mutation.
When you need to update existing data with specific fields.
When you want to filter or search data by passing parameters.
When you want to keep your queries clean by grouping related inputs.
When you want to reuse the same input structure in multiple places.
Syntax
GraphQL
input InputName {
  field1: Type1
  field2: Type2
  ...
}

Input types are defined using the input keyword, not type.

Fields inside input types can only be scalars, enums, or other input types.

Examples
This input type defines a user with a required name and optional age and email.
GraphQL
input UserInput {
  name: String!
  age: Int
  email: String
}
This input type can be used to filter posts by title or published status.
GraphQL
input PostFilter {
  titleContains: String
  published: Boolean
}
Sample Program

This mutation uses an input type to create a new user with username, email, and optional age.

GraphQL
input NewUserInput {
  username: String!
  email: String!
  age: Int
}

mutation CreateUser($input: NewUserInput!) {
  createUser(input: $input) {
    id
    username
    email
    age
  }
}
OutputSuccess
Important Notes

Input types cannot have fields that are other object types, only input types or scalars.

Always mark required fields with ! to ensure they are provided.

Summary

Input types organize data sent into queries or mutations.

They help keep your API clean and reusable.

Use input keyword and only allowed field types.