0
0
GraphQLquery~30 mins

Input arguments for mutations in GraphQL - Mini Project: Build & Apply

Choose your learning style9 modes available
Input Arguments for Mutations in GraphQL
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Change mutation argument to input: BookInput!.