Resolver chains in GraphQL - Time & Space Complexity
When using resolver chains in GraphQL, each resolver calls another to get data. We want to understand how the total work grows as we add more linked resolvers.
How does the time to get the final data change when the chain gets longer?
Analyze the time complexity of the following code snippet.
query {
user(id: "1") {
posts {
comments {
text
}
}
}
}
// Each field calls a resolver that fetches related data.
// user resolver fetches user info.
// posts resolver fetches posts for that user.
// comments resolver fetches comments for each post.
This query uses resolver chains: user -> posts -> comments. Each resolver depends on the previous one's result.
Look for repeated work in the chain.
- Primary operation: For each post, fetching its comments.
- How many times: Number of posts times number of comments per post.
As the number of posts and comments grows, the total work grows too.
| Input Size (posts x comments) | Approx. Operations |
|---|---|
| 10 posts x 5 comments | 50 fetches |
| 100 posts x 5 comments | 500 fetches |
| 100 posts x 20 comments | 2000 fetches |
Pattern observation: The total operations grow roughly by multiplying posts and comments counts.
Time Complexity: O(p × c)
This means the time grows with the number of posts times the number of comments per post.
[X] Wrong: "Each resolver runs once, so time is just O(n) where n is total fields."
[OK] Correct: Because some resolvers run multiple times--once per item in a list--so total time multiplies by list sizes.
Understanding resolver chains helps you explain how nested data fetching impacts performance. This skill shows you can reason about real-world GraphQL queries and their costs.
"What if the comments resolver used batching to fetch all comments at once? How would the time complexity change?"
Practice
Solution
Step 1: Understand resolver chains concept
Resolver chains allow each resolver to use data from the previous resolver's result, enabling step-by-step data fetching.Step 2: Compare options with concept
Only To fetch data step-by-step using results from previous resolvers describes this stepwise data fetching using parent results, which matches the purpose of resolver chains.Final Answer:
To fetch data step-by-step using results from previous resolvers -> Option CQuick Check:
Resolver chains = step-by-step data fetching [OK]
- Confusing resolver chains with database indexing
- Thinking resolver chains create tables
- Assuming resolver chains encrypt data
Solution
Step 1: Recall resolver function signature
In GraphQL, the first argument to a resolver is the parent object, followed by args and context.Step 2: Match signature with options
Only function resolver(parent, args, context) { ... } correctly places parent as the first argument, allowing access to previous resolver's result.Final Answer:
function resolver(parent, args, context) { ... } -> Option DQuick Check:
Parent is first argument in resolver [OK]
- Swapping order of arguments
- Omitting the parent argument
- Confusing context with parent
const resolvers = {
Query: {
user: () => ({ id: 1, name: 'Alice' })
},
User: {
posts: (parent) => [
{ id: 101, title: 'Post 1', authorId: parent.id },
{ id: 102, title: 'Post 2', authorId: parent.id }
]
}
};
// Query: { user { name posts { title } } }Solution
Step 1: Analyze user resolver output
The user resolver returns an object with id 1 and name 'Alice'.Step 2: Analyze posts resolver using parent
The posts resolver receives parent with id 1, so it returns two posts with authorId 1 and titles 'Post 1' and 'Post 2'.Final Answer:
{ user: { name: 'Alice', posts: [{ title: 'Post 1' }, { title: 'Post 2' }] } } -> Option BQuick Check:
Posts use parent.id = 1, two posts returned [OK]
- Assuming posts resolver returns three posts
- Expecting empty posts array
- Confusing parent argument as undefined
const resolvers = {
Query: {
book: () => ({ id: 5, title: 'GraphQL Guide' })
},
Book: {
author: (args) => {
return { id: 2, name: 'John Doe' };
}
}
};Solution
Step 1: Check author resolver signature
The author resolver should receive the parent object as the first argument to access book data.Step 2: Identify missing parent argument
Here, author resolver only has args, so it cannot access parent data, breaking the resolver chain.Final Answer:
The author resolver is missing the parent argument -> Option AQuick Check:
Resolvers need parent as first argument [OK]
- Ignoring parent argument in nested resolvers
- Thinking book resolver must return a list
- Assuming author resolver cannot return objects
orders resolver returns an array of orders with customerId. How should you implement the customer resolver to correctly use resolver chains?Solution
Step 1: Understand data flow in resolver chains
The orders resolver returns orders with customerId; the customer resolver uses parent to access customerId.Step 2: Use parent argument in customer resolver
The customer resolver should use parent.customerId to fetch the correct customer data from the database.Final Answer:
Use the parent argument to get customerId and fetch customer data from database -> Option AQuick Check:
Parent passes customerId to customer resolver [OK]
- Fetching all customers instead of one
- Ignoring parent and missing customerId
- Returning static data ignoring dynamic parent info
Ready to prove it?
Pass the test and this pattern moves unknown
