Bird
Raised Fist0
GraphQLquery~20 mins

Query variables in GraphQL - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using query variables in GraphQL?
easy
A. To create new types in the GraphQL schema
B. To define the schema of the GraphQL server
C. To store query results on the client side
D. To reuse the same query with different input values

Solution

  1. Step 1: Understand what query variables do

    Query variables allow you to write a query once and provide different inputs each time you run it.
  2. Step 2: Compare options with this purpose

    Only To reuse the same query with different input values describes reusing queries with different inputs, which matches the purpose of query variables.
  3. Final Answer:

    To reuse the same query with different input values -> Option D
  4. Quick Check:

    Query variables = reuse queries with different inputs [OK]
Hint: Variables let you change inputs without rewriting queries [OK]
Common Mistakes:
  • Confusing variables with schema definitions
  • Thinking variables store data permanently
  • Mixing variables with client-side caching
2. Which of the following is the correct way to declare a variable in a GraphQL query?
easy
A. query getUser($id: ID!) { user(id: $id) { name } }
B. query getUser(id: $ID) { user(id: id) { name } }
C. query getUser($id) { user(id: id) { name } }
D. query getUser { user(id: $id) { name } }

Solution

  1. Step 1: Identify correct variable syntax

    Variables must be declared with a $ sign and a type, e.g., $id: ID! inside parentheses after the query name.
  2. Step 2: Check each option

    query getUser($id: ID!) { user(id: $id) { name } } correctly declares $id: ID! and uses $id inside the query. Others miss type, $ sign, or parentheses.
  3. Final Answer:

    query getUser($id: ID!) { user(id: $id) { name } } -> Option A
  4. Quick Check:

    Variable declaration = $name: Type! in parentheses [OK]
Hint: Variables need $ and type declared after query name [OK]
Common Mistakes:
  • Omitting the variable type
  • Using variable without $ inside query
  • Not placing variables in parentheses after query name
3. Given the query:
query getBook($bookId: ID!) { book(id: $bookId) { title author } }

and variables:
{ "bookId": "123" }

What will the query return if the book with ID 123 has title "GraphQL Guide" and author "Anna"?
medium
A. { "data": { "book": null } }
B. { "data": { "book": { "title": "GraphQL Guide", "author": "Anna" } } }
C. SyntaxError: Variable not defined
D. { "errors": [ { "message": "Book not found" } ] }

Solution

  1. Step 1: Understand the query and variables

    The query asks for a book by ID using variable $bookId. The variable value is "123".
  2. Step 2: Match variable value to data

    The book with ID "123" exists with title "GraphQL Guide" and author "Anna". So the query returns this data.
  3. Final Answer:

    { "data": { "book": { "title": "GraphQL Guide", "author": "Anna" } } } -> Option B
  4. Quick Check:

    Variable value matches book data = correct result [OK]
Hint: Variables fill query inputs to get matching data [OK]
Common Mistakes:
  • Assuming variable names are optional
  • Expecting errors when data exists
  • Confusing null with errors
4. Identify the error in this GraphQL query using variables:
query getUser($userId: ID) { user(id: userId) { name } }
medium
A. Variable userId inside query should have a $ sign
B. Variable $userId is missing the non-null type indicator
C. Variables must be declared outside the query
D. Query name cannot have variables declared

Solution

  1. Step 1: Check variable usage inside query

    The variable is declared as $userId but inside the query it is used as userId without the $ sign.
  2. Step 2: Correct usage of variables

    Variables must be referenced with the $ sign inside the query fields, so it should be id: $userId.
  3. Final Answer:

    Variable userId inside query should have a $ sign -> Option A
  4. Quick Check:

    Use $ before variable inside query fields [OK]
Hint: Always use $ before variable inside query body [OK]
Common Mistakes:
  • Forgetting $ before variable in query
  • Thinking variables declared outside query
  • Confusing variable declaration syntax
5. You want to write a GraphQL query to fetch a list of books filtered by optional author name using variables. Which query correctly declares the variable and uses it to filter books only if the variable is provided?
hard
A. query getBooks($author: String!) { books(author: $author) { title } }
B. query getBooks { books(author: $author) { title } }
C. query getBooks($author: String) { books(author: $author) { title } }
D. query getBooks($author) { books(author: author) { title } }

Solution

  1. Step 1: Understand optional variable declaration

    To make a variable optional, declare it without the exclamation mark, e.g., $author: String.
  2. Step 2: Check variable usage in query

    Use $author inside the query to filter books. query getBooks($author: String) { books(author: $author) { title } } correctly declares and uses the optional variable.
  3. Step 3: Review other options

    query getBooks($author: String!) { books(author: $author) { title } } makes variable required with !, C uses variable without declaration, D misses type and uses variable incorrectly.
  4. Final Answer:

    query getBooks($author: String) { books(author: $author) { title } } -> Option C
  5. Quick Check:

    Optional variable = no ! and use $ in query [OK]
Hint: Optional variables omit ! and use $ inside query [OK]
Common Mistakes:
  • Making optional variables required with !
  • Using variables without declaration
  • Forgetting $ in query usage
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