0
0
GraphQLquery~20 mins

Input types in GraphQL - Practice Problems & Coding Challenges

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

Given the following GraphQL mutation and input type, what will be the returned data?

input NewUserInput {
  username: String!
  age: Int
}

mutation CreateUser($input: NewUserInput!) {
  createUser(input: $input) {
    id
    username
    age
  }
}

Variables:

{"input": {"username": "alice", "age": 30}}
GraphQL
input NewUserInput {
  username: String!
  age: Int
}

mutation CreateUser($input: NewUserInput!) {
  createUser(input: $input) {
    id
    username
    age
  }
}
A{"data": {"createUser": {"id": "1", "username": null, "age": 30}}}
B{"data": {"createUser": {"id": "1", "username": "alice"}}}
C{"errors": [{"message": "Field 'age' is required"}]}
D{"data": {"createUser": {"id": "1", "username": "alice", "age": 30}}}
Attempts:
2 left
💡 Hint

Check which fields are required and what the mutation returns.

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

Choose the valid syntax for defining an input type in GraphQL.

A
input UserInput {
  name: String!
  email: String
}
B
}
gnirtS :liame  
!gnirtS :eman  
{ tupnIresU tupni
C
input UserInput {
  name: String!
  email: String!
  age
}
D
type UserInput {
  name: String!
  email: String
}
Attempts:
2 left
💡 Hint

Remember input types use the keyword input and fields must have types.

optimization
advanced
2:30remaining
How to optimize input types for nested objects in GraphQL?

You want to create a mutation that accepts a user with an address. Which input type design is best for performance and clarity?

A
input UserInput {
  name: String!
  street: String
  city: String
}
B
input UserInput {
  name: String!
  address: AddressInput
}

input AddressInput {
  street: String
  city: String
}
C
input UserInput {
  name: String!
  address: String
}
D
type UserInput {
  name: String!
  address: AddressInput
}

input AddressInput {
  street: String
  city: String
}
Attempts:
2 left
💡 Hint

Consider reusability and clear structure for nested data.

🔧 Debug
advanced
2:00remaining
Why does this GraphQL query with input type cause an error?

Given this input type and mutation, why does the following query cause an error?

input ProductInput {
  name: String!
  price: Float!
}

mutation AddProduct($input: ProductInput!) {
  addProduct(input: $input) {
    id
    name
    price
  }
}

Variables:

{"input": {"name": "Book"}}
ANo error, mutation succeeds with price as null.
BError: Field 'name' is missing.
CError: Field 'price' of required type 'Float!' was not provided.
DError: Input type 'ProductInput' is not defined.
Attempts:
2 left
💡 Hint

Check which fields are required and if all required fields are provided.

🧠 Conceptual
expert
3:00remaining
What is the main difference between GraphQL input types and object types?

Choose the statement that best explains the difference between input types and object types in GraphQL.

AInput types are used for passing data into queries or mutations, and cannot have fields that are other object types; object types define the shape of data returned by the server.
BInput types and object types are interchangeable and can be used anywhere in the schema without restrictions.
CObject types are only for mutations, while input types are only for queries.
DInput types can have resolvers, but object types cannot.
Attempts:
2 left
💡 Hint

Think about where input types and object types are used in GraphQL operations.