Bird
Raised Fist0
GraphQLquery~30 mins

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

Choose your learning style10 modes available
GraphQL Input Type for Complex Arguments
📖 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

  1. Step 1: Understand the role of input types

    Input types in GraphQL are designed to group related fields together for passing complex arguments.
  2. Step 2: Differentiate input from output types

    Input types are not for output or database creation but for structuring inputs in queries or mutations.
  3. Final Answer:

    To group related input fields into a reusable structure -> Option C
  4. 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

  1. Step 1: Recall GraphQL syntax for input types

    Input types are defined using the input keyword, not type, interface, or enum.
  2. Step 2: Verify the structure

    The correct syntax is input UserInput { name: String, age: Int }.
  3. Final Answer:

    input UserInput { name: String, age: Int } -> Option B
  4. Quick Check:

    Input types use 'input' keyword [OK]
Hint: Use 'input' keyword to define input types [OK]
Common Mistakes:
  • Using 'type' instead of 'input' for input types
  • Confusing input with interface or enum
  • Missing curly braces or commas
3. Given the input type definition:
input AddressInput { street: String, city: String }

and the mutation:
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

  1. Step 1: Understand input type structure

    The input type expects an object with fields street and city, both strings.
  2. 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.
  3. Final Answer:

    { "address": { "street": "123 Main St", "city": "Springfield" } } -> Option D
  4. Quick Check:

    Input variables match input type fields [OK]
Hint: Pass input variables as objects matching input type fields [OK]
Common Mistakes:
  • Passing input as a string instead of object
  • Using array instead of object for input
  • Passing null when input is required
4. Consider this input type and mutation:
input ProductInput { name: String!, price: Float! }

mutation AddProduct($product: ProductInput!) { addProduct(product: $product) { id } }

If you send the variable { "product": { "name": "Book" } }, what error will occur?
medium
A. Runtime error: price field is required but missing
B. No error, mutation succeeds
C. Syntax error: missing price field
D. Type error: name should be Int

Solution

  1. Step 1: Check required fields in input type

    Both name and price are marked with !, meaning they are required.
  2. Step 2: Analyze provided variables

    The variable only provides name, missing price, so GraphQL will raise a runtime error for missing required field.
  3. Final Answer:

    Runtime error: price field is required but missing -> Option A
  4. Quick Check:

    Required fields missing cause runtime errors [OK]
Hint: Required fields must be present in input variables [OK]
Common Mistakes:
  • Assuming missing required fields cause syntax errors
  • Thinking mutation succeeds without required fields
  • Confusing field types (e.g., name as Int)
5. You want to create a mutation that accepts a complex input type with nested inputs for a user profile:
input AddressInput { street: String!, city: String! }
input UserProfileInput { name: String!, age: Int, address: AddressInput! }

Which of the following mutation argument definitions correctly uses these input types?
hard
A. mutation updateUserProfile(profile: UserProfileInput!) { ... }
B. mutation updateUserProfile(profile: UserProfileInput) { ... }
C. mutation updateUserProfile(profile: AddressInput!) { ... }
D. mutation updateUserProfile(profile: String!) { ... }

Solution

  1. Step 1: Identify correct input type for mutation argument

    The mutation should accept the full user profile input, which is UserProfileInput, not just the address or a string.
  2. Step 2: Check required modifier

    Since the profile is required, the argument should be UserProfileInput! to enforce presence.
  3. Final Answer:

    mutation updateUserProfile(profile: UserProfileInput!) { ... } -> Option A
  4. Quick Check:

    Use full nested input type with ! for required [OK]
Hint: Use full nested input type with ! if required [OK]
Common Mistakes:
  • Using nested input type instead of full input type
  • Omitting required ! modifier
  • Using scalar types instead of input types
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard