Input arguments let you send data to change or add information in a database using mutations.
0
0
Input arguments for mutations in GraphQL
Introduction
When you want to add a new user with details like name and email.
When you need to update a product's price or description.
When deleting an item by specifying its ID.
When creating a new post with a title and content.
When changing a user's password securely.
Syntax
GraphQL
mutation MutationName($inputName: InputType!) {
mutationField(input: $inputName) {
returnField
}
}Input arguments are defined with a $ sign and a type before the mutation body.
The exclamation mark (!) means the input is required.
Examples
This mutation adds a user by passing a userInput object with user details.
GraphQL
mutation AddUser($userInput: UserInput!) {
addUser(input: $userInput) {
id
name
}
}This mutation updates a product's price using two input arguments: productId and price.
GraphQL
mutation UpdateProduct($productId: ID!, $price: Float!) {
updateProduct(id: $productId, price: $price) {
id
price
}
}Sample Program
This mutation creates a new post by sending a postInput object with title and content.
GraphQL
mutation CreatePost($postInput: PostInput!) {
createPost(input: $postInput) {
id
title
content
}
}
# Variables:
# {
# "postInput": {
# "title": "My First Post",
# "content": "Hello, this is my first post!"
# }
# }OutputSuccess
Important Notes
Always define input types in your GraphQL schema to organize input data.
Use variables to keep your mutation queries clean and reusable.
Input arguments help keep your API flexible and secure by controlling what data is sent.
Summary
Input arguments let you send data to mutations to create, update, or delete records.
They are defined with types and passed as variables for clarity and safety.
Using input arguments makes your GraphQL API easy to use and maintain.