Query variables in GraphQL - Time & Space Complexity
We want to understand how the time to run a GraphQL query changes when we use variables.
Specifically, how does the query's work grow as the input data or variables change?
Analyze the time complexity of the following GraphQL query using variables.
query GetUser($id: ID!) {
user(id: $id) {
name
posts {
title
comments {
text
}
}
}
}
This query fetches a user by ID, then gets their posts and comments on those posts.
Look for repeated actions in the query.
- Primary operation: Fetching posts and then comments for each post.
- How many times: For each post, all comments are fetched, so this repeats for every post.
As the number of posts and comments grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 posts, 5 comments each | About 10 + (10 x 5) = 60 operations |
| 100 posts, 5 comments each | About 100 + (100 x 5) = 600 operations |
| 1000 posts, 5 comments each | About 1000 + (1000 x 5) = 6000 operations |
Pattern observation: The total work grows roughly in proportion to the number of posts times the number of comments per post.
Time Complexity: O(p + p x c)
This means the time grows with the number of posts (p) plus the number of comments (c) per post multiplied by posts.
[X] Wrong: "Using variables makes the query always run in constant time."
[OK] Correct: Variables only change input values; the query still processes all posts and comments, so time depends on data size.
Understanding how query variables affect time helps you explain performance clearly and shows you know how data size impacts work.
What if we added a variable to limit the number of posts returned? How would that change the time complexity?
Practice
query variables in GraphQL?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 DQuick Check:
Query variables = reuse queries with different inputs [OK]
- Confusing variables with schema definitions
- Thinking variables store data permanently
- Mixing variables with client-side caching
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$idinside the query. Others miss type, $ sign, or parentheses.Final Answer:
query getUser($id: ID!) { user(id: $id) { name } } -> Option AQuick Check:
Variable declaration = $name: Type! in parentheses [OK]
- Omitting the variable type
- Using variable without $ inside query
- Not placing variables in parentheses after query name
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"?
Solution
Step 1: Understand the query and variables
The query asks for a book by ID using variable$bookId. The variable value is "123".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.Final Answer:
{ "data": { "book": { "title": "GraphQL Guide", "author": "Anna" } } } -> Option BQuick Check:
Variable value matches book data = correct result [OK]
- Assuming variable names are optional
- Expecting errors when data exists
- Confusing null with errors
query getUser($userId: ID) { user(id: userId) { name } }Solution
Step 1: Check variable usage inside query
The variable is declared as$userIdbut inside the query it is used asuserIdwithout the $ sign.Step 2: Correct usage of variables
Variables must be referenced with the $ sign inside the query fields, so it should beid: $userId.Final Answer:
Variable userId inside query should have a $ sign -> Option AQuick Check:
Use $ before variable inside query fields [OK]
- Forgetting $ before variable in query
- Thinking variables declared outside query
- Confusing variable declaration syntax
author name using variables. Which query correctly declares the variable and uses it to filter books only if the variable is provided?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$authorinside 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 CQuick Check:
Optional variable = no ! and use $ in query [OK]
- Making optional variables required with !
- Using variables without declaration
- Forgetting $ in query usage
Ready to prove it?
Pass the test and this pattern moves unknown
