Input arguments let you send data to change or add information in a database using mutations.
Input arguments for mutations in 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.
mutation AddUser($userInput: UserInput!) {
addUser(input: $userInput) {
id
name
}
}mutation UpdateProduct($productId: ID!, $price: Float!) {
updateProduct(id: $productId, price: $price) {
id
price
}
}This mutation creates a new post by sending a postInput object with title and content.
mutation CreatePost($postInput: PostInput!) {
createPost(input: $postInput) {
id
title
content
}
}
# Variables:
# {
# "postInput": {
# "title": "My First Post",
# "content": "Hello, this is my first post!"
# }
# }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.
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.
Practice
Solution
Step 1: Understand the role of mutations
Mutations in GraphQL are used to change data, such as creating or updating records.Step 2: Identify the use of input arguments
Input arguments provide the data needed for these changes, like new values or identifiers.Final Answer:
To send data to create, update, or delete records -> Option AQuick Check:
Input arguments = send data for mutations [OK]
- Confusing input arguments with query filters
- Thinking input arguments style the API
- Assuming mutations fetch data only
Solution
Step 1: Recognize variable declaration syntax
Input arguments must be declared with a $ prefix and type, like $name: String!.Step 2: Use variables inside mutation
Inside the mutation body, use the variable with $name to pass the value.Final Answer:
mutation addUser($name: String!) { createUser(name: $name) { id } } -> Option BQuick Check:
Variables use $name: Type! and $name inside mutation [OK]
- Omitting $ in variable declaration
- Using type inside mutation call instead of variable
- Not specifying variable type
id after execution?mutation updateUser($id: ID!, $email: String!) { updateUser(id: $id, email: $email) { id email } }{ "id": "123", "email": "new@example.com" }Solution
Step 1: Understand mutation input and output
The mutation updates the user with id "123" and new email, returning the user's id and email.Step 2: Identify returned id value
The returnedidis the same as the input id, which is "123".Final Answer:
"123" -> Option AQuick Check:
Returned id matches input id "123" [OK]
- Confusing returned id with email
- Assuming null if email changes
- Expecting error without missing fields
mutation deleteUser(id: ID!) { removeUser(id: id) { success } }Solution
Step 1: Check variable declaration syntax
Variables in mutation definitions must start with $, e.g., $id: ID!.Step 2: Verify usage inside mutation
Inside the mutation body, use the variable with $id, not just id.Final Answer:
Missing $ before variable name in mutation definition -> Option DQuick Check:
Variables require $ prefix in definition and usage [OK]
- Omitting $ in variable declaration
- Using variable name without $ inside mutation
- Assuming mutation name must match field
title (String), content (String), and tags (list of Strings). Which is the correct way to define the input argument and use it in the mutation?Solution
Step 1: Define input type correctly
Useinputkeyword with required fields and list type:tags: [String!]!means list of non-null strings, list itself required.Step 2: Declare variable and use it properly
Declare variable with $post: PostInput! and use it asinput: $postinside mutation.Final Answer:
input PostInput { title: String!, content: String!, tags: [String!]! }
mutation createPost($post: PostInput!) { addPost(input: $post) { id } } -> Option CQuick Check:
Use input type and $variable with correct usage [OK]
- Using type instead of input for input objects
- Omitting $ in variable declaration or usage
- Incorrect list type syntax or missing non-null markers
- Passing variable without $ inside mutation
Ready to prove it?
Pass the test and this pattern moves unknown
