0
0
GraphQLquery~5 mins

Args argument in GraphQL

Choose your learning style9 modes available
Introduction

The Args argument lets you send extra information to a GraphQL query or mutation. It helps you ask for exactly what you want.

When you want to get data for a specific user by their ID.
When you want to filter a list of items by a category.
When you want to limit how many results you get back.
When you want to pass input data to create or update a record.
Syntax
GraphQL
type Query {
  getItem(id: ID!): Item
}

# Args are inside parentheses after the field name
Args are written inside parentheses after the field name.
Each arg has a name and a type, like id: ID! means 'id' is required.
Examples
This query takes an id argument to get one user.
GraphQL
type Query {
  user(id: ID!): User
}
This query takes an optional category argument to filter posts.
GraphQL
type Query {
  posts(category: String): [Post]
}
This mutation takes name and age as args to add a user.
GraphQL
type Mutation {
  addUser(name: String!, age: Int): User
}
Sample Program

This query asks for the user with ID "123" and requests their id and name.

GraphQL
query GetUser {
  user(id: "123") {
    id
    name
  }
}
OutputSuccess
Important Notes

Args help make queries flexible and specific.

Required args have an exclamation mark ! after their type.

You can have multiple args separated by commas inside the parentheses.

Summary

Args let you pass extra info to queries or mutations.

They are written inside parentheses after the field name.

Args make your GraphQL requests precise and useful.