📖 Scenario: You are building a simple GraphQL API for a bookstore. You want to allow users to add new books to the store's database.
🎯 Goal: Build a GraphQL mutation pattern that lets users add a new book with a title and author.
📋 What You'll Learn
Create a GraphQL type called Book with fields id, title, and author.
Create an input type called BookInput with fields title and author.
Create a mutation called addBook that takes a BookInput and returns a Book.
Use a simple in-memory list to store books with unique id values.
💡 Why This Matters
🌍 Real World
GraphQL mutations are used in real applications to change data, such as adding new records or updating existing ones.
💼 Career
Understanding how to create mutation patterns is essential for backend developers working with GraphQL APIs.
Progress0 / 4 steps
1
Define the Book type
Create 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 a GraphQL object type with the specified fields.
2
Create the BookInput input type
Create a GraphQL input type called BookInput with fields title and author, both of type String!.
GraphQL
Hint
Use the input keyword to define an input type for mutations.
3
Define the addBook mutation
Create a mutation called addBook that takes one argument input of type BookInput! and returns a Book.
GraphQL
Hint
Define a Mutation type with the addBook field that accepts BookInput! and returns Book.
4
Add a simple in-memory store and resolver pattern
Add a simple in-memory list called books to store book objects. Add a resolver function called addBook that takes input, assigns a unique id, adds the new book to books, and returns the new book.
GraphQL
Hint
Use a JavaScript array to store books and a resolver function to add new books with unique IDs.
Practice
(1/5)
1. What is the main purpose of a mutation in GraphQL?
easy
A. To optimize query performance
B. To create, update, or delete data in the database
C. To define the schema types
D. To fetch data without changing it
Solution
Step 1: Understand GraphQL operation types
GraphQL has queries for reading data and mutations for changing data.
Step 2: Identify mutation role
Mutations are used to create, update, or delete data in the database.
Final Answer:
To create, update, or delete data in the database -> Option B
Quick Check:
Mutation = change data [OK]
Hint: Mutations always change data, queries only read data [OK]
Common Mistakes:
Confusing mutations with queries
Thinking mutations only fetch data
Mixing schema definition with data operations
2. Which of the following is the correct syntax to start a mutation with an input variable named input of type CreateUserInput!?
easy
A. mutation createUser($input: CreateUserInput!) { ... }
B. query createUser(input: CreateUserInput!) { ... }
C. mutation createUser(input CreateUserInput!) { ... }
D. mutation createUser($input CreateUserInput!) { ... }
Solution
Step 1: Recall mutation syntax with variables
Variables in GraphQL mutations start with $ and have a colon before the type.
Step 2: Check each option
mutation createUser($input: CreateUserInput!) { ... } correctly uses $input: CreateUserInput!. Others miss $ or colon.
Final Answer:
mutation createUser($input: CreateUserInput!) { ... } -> Option A
Quick Check:
Variables need $ and colon [OK]
Hint: Variables start with $ and use colon before type [OK]
Common Mistakes:
Omitting $ before variable name
Missing colon between variable and type
Using query instead of mutation keyword
3. Given this mutation:
mutation AddBook($book: BookInput!) {
addBook(input: $book) {
id
title
}
}
What fields will the server return after running this mutation?
medium
A. Only the input object sent
B. All fields of the book including author and publishedYear
C. No fields, mutation returns nothing
D. Only the id and title of the new book
Solution
Step 1: Identify requested return fields
The mutation requests id and title fields inside addBook.
Step 2: Understand GraphQL response
GraphQL returns only the fields explicitly requested, so only id and title are returned.
Final Answer:
Only the id and title of the new book -> Option D
Quick Check:
Requested fields = returned fields [OK]
Hint: Returned fields match exactly requested fields in mutation body [OK]
Assuming the server expects the input argument to be named input, not post.
medium
A. The mutation keyword should be query
B. The variable $post should be $input
C. The argument name post should be input
D. The return fields id and content are invalid
Solution
Step 1: Check argument name in mutation call
The mutation calls createPost(post: $post), but server expects input argument.
Step 2: Correct argument name
Changing post to input matches server expectation and fixes error.
Final Answer:
The argument name post should be input -> Option C
Quick Check:
Argument names must match server schema [OK]
Hint: Match argument names exactly as server expects [OK]
Common Mistakes:
Using wrong argument name in mutation call
Confusing variable name with argument name
Changing mutation keyword incorrectly
5. You want to create a mutation to add a new user with fields name and email. Which mutation correctly uses input variables and requests the new user's id and email in the response?
hard
A. mutation AddUser($input: UserInput!) { addUser(input: $input) { id email } }
B. mutation AddUser(input: UserInput!) { addUser(input: input) { id email } }
C. mutation AddUser($user: UserInput!) { addUser(user: $user) { name email } }
D. mutation AddUser($input: UserInput!) { addUser(input: $input) { name } }
Solution
Step 1: Check variable declaration and usage
mutation AddUser($input: UserInput!) { addUser(input: $input) { id email } } correctly declares $input: UserInput! and uses it as input: $input.
Step 2: Verify requested return fields
mutation AddUser($input: UserInput!) { addUser(input: $input) { id email } } requests id and email, matching the question requirement.
Final Answer:
mutation AddUser($input: UserInput!) { addUser(input: $input) { id email } } -> Option A
Quick Check:
Correct variable and requested fields = mutation AddUser($input: UserInput!) { addUser(input: $input) { id email } } [OK]
Hint: Use $variable and request needed fields explicitly [OK]
Common Mistakes:
Omitting $ in variable declaration or usage
Requesting wrong fields in response
Using wrong argument names
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.