📖 Scenario: You are building a simple GraphQL API for a bookstore. You want to allow users to add new books to the store by sending a mutation with the book details.
🎯 Goal: Create a GraphQL mutation called addBook that accepts input arguments for the book's title, author, and publishedYear. The mutation should return the added book's details.
📋 What You'll Learn
Define a Book type with fields title, author, and publishedYear.
Create an addBook mutation that takes input arguments title (String!), author (String!), and publishedYear (Int!).
The mutation should return the newly added Book object.
Use input arguments correctly in the mutation definition.
💡 Why This Matters
🌍 Real World
GraphQL mutations with input arguments are used in real APIs to create or update data, such as adding new books to a bookstore database.
💼 Career
Understanding how to define and use input arguments in GraphQL mutations 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 title of type String!, author of type String!, and publishedYear of type Int!.
GraphQL
Hint
Use type Book { ... } and define each field with its type and exclamation mark for required fields.
2
Add the addBook mutation with input arguments
Add a mutation called addBook inside the type Mutation that accepts three input arguments: title of type String!, author of type String!, and publishedYear of type Int!. The mutation should return a Book.
GraphQL
Hint
Define type Mutation { addBook(title: String!, author: String!, publishedYear: Int!): Book }.
3
Create an input type for the book details
Create an input type called BookInput with fields title (String!), author (String!), and publishedYear (Int!).
GraphQL
Hint
Use input BookInput { ... } to define input fields.
4
Update addBook mutation to use BookInput argument
Modify the addBook mutation to accept a single argument called input of type BookInput! instead of separate arguments. The mutation should still return a Book.
GraphQL
Hint
Change mutation argument to input: BookInput!.
Practice
(1/5)
1. What is the main purpose of input arguments in GraphQL mutations?
easy
A. To send data to create, update, or delete records
B. To define the schema of queries
C. To style the GraphQL interface
D. To fetch data without any changes
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 A
Quick Check:
Input arguments = send data for mutations [OK]
Hint: Input arguments send data to mutations for changes [OK]
Common Mistakes:
Confusing input arguments with query filters
Thinking input arguments style the API
Assuming mutations fetch data only
2. Which of the following is the correct way to define an input argument for a mutation in GraphQL?
easy
A. mutation addUser(name: String!) { createUser(name: name) { id } }
B. mutation addUser($name: String!) { createUser(name: $name) { id } }
C. mutation addUser { createUser(name: String!) { id } }
D. mutation addUser($name) { createUser(name: $name) { id } }
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 B
Quick Check:
Variables use $name: Type! and $name inside mutation [OK]
Hint: Declare variables with $ and type, use $ inside mutation [OK]
Common Mistakes:
Omitting $ in variable declaration
Using type inside mutation call instead of variable
Not specifying variable type
3. Given the mutation and variables below, what will be the returned id after execution?
D. Missing $ before variable name in mutation definition
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 D
Quick Check:
Variables require $ prefix in definition and usage [OK]
Hint: Always prefix variables with $ in mutation definitions [OK]
Common Mistakes:
Omitting $ in variable declaration
Using variable name without $ inside mutation
Assuming mutation name must match field
5. You want to create a mutation that accepts a complex input object with fields 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?
hard
A. type PostInput { title: String!, content: String!, tags: [String!]! } mutation createPost(post: PostInput!) { addPost(post: post) { id } }