📖 Scenario: You are building a simple GraphQL API for a bookstore. You want to allow users to update the details of a book, such as its title and author.
🎯 Goal: Build a GraphQL updateBook mutation that updates a book's id, title, and author fields.
📋 What You'll Learn
Create a GraphQL input type called UpdateBookInput with fields id, title, and author.
Define a mutation called updateBook that takes an input argument of type UpdateBookInput.
The mutation should return the updated book's id, title, and author.
Use the exact names UpdateBookInput, updateBook, and input in your code.
💡 Why This Matters
🌍 Real World
GraphQL update mutations are used in APIs to allow clients to modify existing data records safely and clearly.
💼 Career
Understanding how to write update mutations is essential for backend developers working with GraphQL APIs in real-world applications.
Progress0 / 4 steps
1
Define the UpdateBookInput input type
Create a GraphQL input type called UpdateBookInput with the fields id of type ID!, title of type String, and author of type String.
GraphQL
Hint
Use the input keyword to define an input type with the specified fields.
2
Add the updateBook mutation field
Add a mutation field called updateBook that takes an argument named input of type UpdateBookInput!.
GraphQL
Hint
Define a Mutation type and add the updateBook field with the correct argument.
3
Define the Book type
Define a GraphQL type called Book with fields id of type ID!, title of type String, and author of type String.
GraphQL
Hint
Use the type keyword to define the Book type with the specified fields.
4
Complete the update mutation schema
Ensure the full schema includes the UpdateBookInput input type, the Book type, and the Mutation type with the updateBook mutation returning a Book.
GraphQL
Hint
Review your schema to confirm all parts are included and correctly linked.
Practice
(1/5)
1. What is the main purpose of an update mutation in GraphQL?
easy
A. To create new data records
B. To change existing data by specifying an ID and new values
C. To delete data from the database
D. To fetch data without any changes
Solution
Step 1: Understand the role of update mutations
Update mutations modify existing records by identifying them and applying new values.
Step 2: Differentiate from other mutation types
Delete mutations remove data, create mutations add new data, and queries fetch data without changes.
Final Answer:
To change existing data by specifying an ID and new values -> Option B
Quick Check:
Update mutation = change existing data [OK]
Hint: Update mutations modify existing records by ID [OK]
Common Mistakes:
Confusing update with create or delete mutations
Thinking update fetches data without changes
Assuming update creates new records
2. Which of the following is the correct syntax for an update mutation in GraphQL?
easy
A. mutation { updateUser(id: "123", input: {name: "Alice"}) { id name } }
B. mutation { createUser(id: "123", input: {name: "Alice"}) { id name } }
C. query { updateUser(id: "123", input: {name: "Alice"}) { id name } }
D. mutation { deleteUser(id: "123") { id name } }
Solution
Step 1: Identify the mutation keyword and operation
Update mutations use the mutation keyword and specify the update operation with an ID and input object.
Step 2: Check the options for correct mutation syntax
mutation { updateUser(id: "123", input: {name: "Alice"}) { id name } } uses mutation with updateUser and input fields correctly; others use wrong operation or query keyword.
Final Answer:
mutation { updateUser(id: "123", input: {name: "Alice"}) { id name } } -> Option A
C. { "data": { "updatePost": { "id": "5", "title": "New Title", "content": "Old content" } } }
D. Error: Post with ID 5 not found
Solution
Step 1: Understand the mutation effect
The mutation updates the post with ID 5, changing only the title to "New Title" while keeping other fields unchanged.
Step 2: Predict the returned data
The response includes the updated title and the unchanged content "Old content" along with the ID.
Final Answer:
{ "data": { "updatePost": { "id": "5", "title": "New Title", "content": "Old content" } } } -> Option C
Quick Check:
Update mutation returns updated fields plus unchanged fields [OK]
Hint: Update changes specified fields, others remain same [OK]
Common Mistakes:
Assuming all fields reset or become null
Expecting error when post exists
Ignoring unchanged fields in output
4. Identify the error in this update mutation:
mutation { updateUser(input: {name: "Bob"}) { id name } }
medium
A. Missing the ID argument to specify which user to update
B. Input object should be named 'data' instead of 'input'
C. Mutation keyword should be 'query' instead of 'mutation'
D. Cannot update the 'name' field
Solution
Step 1: Check required arguments for update mutation
Update mutations require an ID to identify the record to update.
Step 2: Analyze the given mutation
The mutation lacks the ID argument, so it cannot know which user to update.
Final Answer:
Missing the ID argument to specify which user to update -> Option A
Quick Check:
Update mutation must include ID argument [OK]
Hint: Always include ID to update specific record [OK]
Common Mistakes:
Omitting the ID argument
Confusing input object name
Using 'query' instead of 'mutation'
5. You want to update a user's email and phone number but only if the phone number is provided. Which mutation pattern correctly handles this conditional update?