📖 Scenario: You are building a simple GraphQL API for a book store. You want to allow users to add new books with detailed information like title, author, and publication year.
🎯 Goal: Create a GraphQL input type called BookInput to accept complex arguments for adding a new book. Then use this input type in a mutation called addBook.
📋 What You'll Learn
Create an input type named BookInput with fields title (String), author (String), and year (Int).
Define a mutation addBook that takes one argument book of type BookInput.
The mutation should return a Book type with the same fields.
Use exact names: BookInput, addBook, book, Book.
💡 Why This Matters
🌍 Real World
GraphQL input types let APIs accept complex data structures, like objects with multiple fields, making it easier to send detailed information in one request.
💼 Career
Understanding input types and mutations is essential for backend developers working with GraphQL APIs to create flexible and maintainable data operations.
Progress0 / 4 steps
1
Define the Book type
Create a GraphQL type called Book with fields title (String), author (String), and year (Int).
GraphQL
Hint
Use type Book { ... } syntax to define the Book type with the specified fields.
2
Create the BookInput input type
Create a GraphQL input type called BookInput with fields title (String), author (String), and year (Int).
GraphQL
Hint
Use input BookInput { ... } syntax similar to the Book type.
3
Define the addBook mutation
Define a mutation called addBook that takes one argument book of type BookInput and returns a Book type.
GraphQL
Hint
Use type Mutation { addBook(book: BookInput): Book } syntax to define the mutation.
4
Complete the schema with Query and Mutation
Add a root type Query with a dummy field _empty of type String to complete the schema. Keep the Mutation type as defined.
GraphQL
Hint
GraphQL schemas need a root Query type. Add type Query { _empty: String } to complete the schema.
Practice
(1/5)
1. What is the main purpose of using input types in GraphQL queries or mutations?
easy
A. To define the output format of a query
B. To create database tables automatically
C. To group related input fields into a reusable structure
D. To write comments inside the schema
Solution
Step 1: Understand the role of input types
Input types in GraphQL are designed to group related fields together for passing complex arguments.
Step 2: Differentiate input from output types
Input types are not for output or database creation but for structuring inputs in queries or mutations.
Final Answer:
To group related input fields into a reusable structure -> Option C
Quick Check:
Input types = group related inputs [OK]
Hint: Input types group inputs, not outputs or tables [OK]
Common Mistakes:
Confusing input types with output types
Thinking input types create database tables
Assuming input types are for comments
2. Which of the following is the correct way to define an input type in GraphQL?
easy
A. type UserInput { name: String, age: Int }
B. input UserInput { name: String, age: Int }
C. interface UserInput { name: String, age: Int }
D. enum UserInput { name, age }
Solution
Step 1: Recall GraphQL syntax for input types
Input types are defined using the input keyword, not type, interface, or enum.
Step 2: Verify the structure
The correct syntax is input UserInput { name: String, age: Int }.
Final Answer:
input UserInput { name: String, age: Int } -> Option B
Quick Check:
Input types use 'input' keyword [OK]
Hint: Use 'input' keyword to define input types [OK]
mutation AddAddress($address: AddressInput!) { addAddress(address: $address) { id } }
What is the correct way to pass the variable $address in a GraphQL request?
medium
A. { "address": null }
B. { "address": "123 Main St, Springfield" }
C. { "address": ["123 Main St", "Springfield"] }
D. { "address": { "street": "123 Main St", "city": "Springfield" } }
Solution
Step 1: Understand input type structure
The input type expects an object with fields street and city, both strings.
Step 2: Match variable format to input type
The variable must be a JSON object with keys matching the input fields, so { "address": { "street": "123 Main St", "city": "Springfield" } } is correct.
Final Answer:
{ "address": { "street": "123 Main St", "city": "Springfield" } } -> Option D
Quick Check:
Input variables match input type fields [OK]
Hint: Pass input variables as objects matching input type fields [OK]