Bird
Raised Fist0
GraphQLquery~20 mins

Input type for complex arguments in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GraphQL Input Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate
2:00remaining
What is the output of this GraphQL query with complex input?

Given the following GraphQL schema snippet:

input AddressInput {
  street: String!
  city: String!
  zip: String
}

type Query {
  getUserAddress(id: ID!, address: AddressInput!): String
}

And this query:

{
  getUserAddress(id: "123", address: {street: "Main St", city: "Springfield", zip: "01101"})
}

What is the expected output assuming the resolver returns the concatenated address string?

A{"data": {"getUserAddress": "Main St, Springfield"}}
B{"data": {"getUserAddress": "Main St Springfield 01101"}}
C{"data": {"getUserAddress": "Main St, Springfield, 01101"}}
D{"errors": [{"message": "Field 'zip' is required"}]}
Attempts:
2 left
💡 Hint

Check how the input type fields are used and if optional fields are included.

📝 Syntax
intermediate
1:30remaining
Which option is a valid GraphQL input type definition for a complex argument?

Choose the correct GraphQL input type definition for a complex argument representing a product with name (string), price (float), and tags (list of strings).

A
input ProductInput {
  name: String!
  price: Float
  tags: String!
}
B
input ProductInput {
  name: String!
  price: Float!
  tags: [String!]!
}
C
input ProductInput {
  name: String
  price: Int
  tags: String
}
D
type ProductInput {
  name: String!
  price: Float!
  tags: [String!]!
}
Attempts:
2 left
💡 Hint

Remember input types use input keyword and field types must match expected data.

🔧 Debug
advanced
2:00remaining
Why does this GraphQL query fail with a validation error?

Given this input type and query:

input FilterInput {
  category: String
  priceRange: [Float!]
}

query getProducts($filter: FilterInput!) {
  products(filter: $filter) {
    id
    name
  }
}

Variables sent:

{"filter": {"category": "Books", "priceRange": [10, null, 30]}}

Why does the query fail?

ABecause 'priceRange' contains a null value but is defined as a list of non-null floats.
BBecause the variable 'filter' is missing required fields.
CBecause 'category' is optional but provided as a string.
DBecause the query is missing a selection set for 'products'.
Attempts:
2 left
💡 Hint

Check the nullability of list elements in the input type.

❓ optimization
advanced
1:30remaining
How to optimize a GraphQL mutation with complex input to reduce payload size?

You have a mutation that accepts a complex input type with many optional fields. Which approach best reduces the payload size sent from client to server?

ASend only the fields that have changed values in the input object.
BUse a list of key-value pairs instead of an input object.
CConvert the input object to a JSON string and send as a single scalar.
DSend all fields with default or null values to ensure completeness.
Attempts:
2 left
💡 Hint

Think about minimizing data transfer by avoiding unnecessary fields.

🧠 Conceptual
expert
2:30remaining
What is the main reason GraphQL uses input types for complex arguments instead of regular object types?

Choose the best explanation for why GraphQL requires separate input types for complex arguments rather than reusing type definitions.

AInput types automatically generate database schemas, unlike regular types.
BInput types allow circular references which regular object types do not support.
CInput types can include subscriptions which regular types cannot.
DInput types are designed to be immutable and cannot have fields that resolve to functions, ensuring safe input validation.
Attempts:
2 left
💡 Hint

Consider the difference in purpose between input and output types in GraphQL.

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