📖 Scenario: You are building a simple GraphQL API for a bookstore. You want to allow users to query for books by their id using query variables.
🎯 Goal: Create a GraphQL query that uses a variable called $bookId to fetch the title and author of a book by its id.
📋 What You'll Learn
Define a query named GetBookById that accepts a variable $bookId of type ID!
Use the variable $bookId in the query to fetch the book with that id
Request the title and author fields of the book
💡 Why This Matters
🌍 Real World
GraphQL query variables are used in real applications to make queries dynamic and reusable without rewriting the query for each input.
💼 Career
Understanding query variables is essential for backend and frontend developers working with GraphQL APIs to build efficient and flexible data fetching.
Progress0 / 4 steps
1
Define the GraphQL query with a variable
Write a GraphQL query named GetBookById that accepts a variable $bookId of type ID!. Inside the query, select the book field and pass id: $bookId as an argument.
GraphQL
Hint
Remember to declare the variable $bookId with its type ID! in the query definition.
2
Add the variable value in the query variables section
Create a JSON object named variables with a key "bookId" and value "1" to provide the variable value for the query.
GraphQL
Hint
The variables are sent as a JSON object with keys matching the variable names without the $ sign.
3
Use the query and variables together
Combine the query and the variables JSON object so they can be sent together in a GraphQL request.
GraphQL
Hint
Usually, the query string and variables JSON are sent together in the request body.
4
Complete the query to fetch book details by variable
Ensure the query named GetBookById uses the variable $bookId to fetch the title and author fields of the book with that id. The variables JSON object must have "bookId": "1".
GraphQL
Hint
Double-check that the variable is used correctly in the query and the variables JSON matches the variable name.
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
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.
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.
Final Answer:
To reuse the same query with different input values -> Option D
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
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.
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.
Final Answer:
query getUser($id: ID!) { user(id: $id) { name } } -> Option A
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
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
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.
Step 2: Correct usage of variables
Variables must be referenced with the $ sign inside the query fields, so it should be id: $userId.
Final Answer:
Variable userId inside query should have a $ sign -> Option A
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
Step 1: Understand optional variable declaration
To make a variable optional, declare it without the exclamation mark, e.g., $author: String.
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.
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.
Final Answer:
query getBooks($author: String) { books(author: $author) { title } } -> Option C
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 unknownstrongwhile earning +1 Grip toward your certificate.