0
0
GraphQLquery~20 mins

Query variables in GraphQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GraphQL Query Variables Master
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 variables?

Given the following GraphQL query and variables, what will be the value of user.name in the response?

query GetUser($id: ID!) {
  user(id: $id) {
    name
  }
}

Variables:

{"id": "42"}

Assume the user with ID "42" has the name "Alice".

GraphQL
query GetUser($id: ID!) {
  user(id: $id) {
    name
  }
}
A{"errors": [{"message": "Variable '$id' not provided"}]}
B{"data": {"user": {"name": "Alice"}}}
C{"data": {"user": {"name": "42"}}}
D{"data": {"user": null}}
Attempts:
2 left
💡 Hint

Variables replace the placeholders in the query. The user with ID "42" is named "Alice".

📝 Syntax
intermediate
1:30remaining
Which option correctly declares a required variable in a GraphQL query?

Choose the correct syntax to declare a required variable $limit of type Int in a GraphQL query.

Aquery GetItems($limit: Int!) { items(limit: $limit) { id } }
Bquery GetItems($limit: Int) { items(limit: $limit) { id } }
Cquery GetItems($limit Int!) { items(limit: $limit) { id } }
Dquery GetItems(limit: Int!) { items(limit: $limit) { id } }
Attempts:
2 left
💡 Hint

Required variables use an exclamation mark ! after the type and must be declared with a $ prefix.

optimization
advanced
2:00remaining
How can query variables improve GraphQL query performance?

Which of the following best explains how using query variables can improve performance in GraphQL?

AVariables encrypt the query data, improving security and speed.
BVariables reduce the size of the response by compressing data automatically.
CVariables enable the client to send multiple queries in one request, reducing network calls.
DVariables allow the server to cache the query plan and reuse it with different inputs, reducing parsing and validation time.
Attempts:
2 left
💡 Hint

Think about how the server handles queries with the same structure but different inputs.

🔧 Debug
advanced
2:00remaining
What error occurs if a required variable is missing in a GraphQL request?

Given this query:

query GetPost($postId: ID!) {
  post(id: $postId) {
    title
  }
}

If the request is sent without providing postId in variables, what error will the server return?

A{"errors": [{"message": "Field 'post' not found."}]}
B{"data": {"post": null}}
C{"errors": [{"message": "Variable '$postId' of required type 'ID!' was not provided."}]}
D{"data": {}}
Attempts:
2 left
💡 Hint

Required variables must be provided or the server returns an error.

🧠 Conceptual
expert
2:30remaining
Why use query variables instead of hardcoding values in GraphQL queries?

Which of the following is the best reason to use query variables rather than hardcoding values directly in GraphQL queries?

AVariables allow reuse of the same query with different inputs without changing the query text, improving maintainability and caching.
BVariables force the server to execute queries faster by parallel processing.
CVariables reduce the size of the query by removing unnecessary fields.
DVariables automatically encrypt sensitive data sent to the server.
Attempts:
2 left
💡 Hint

Think about how variables help with query reuse and server caching.