Nested resolver execution in GraphQL - Time & Space Complexity
When GraphQL runs nested resolvers, it calls functions inside other functions to get data step-by-step.
We want to know how the total work grows as the data size grows.
Analyze the time complexity of the following code snippet.
query {
users {
id
posts {
id
comments {
id
}
}
}
}
This query fetches users, their posts, and comments on each post using nested resolvers.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: For each user, fetch posts; for each post, fetch comments.
- How many times: Number of users times number of posts per user times number of comments per post.
Explain the growth pattern intuitively.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 users, 5 posts, 3 comments | 10 x 5 x 3 = 150 |
| 100 users, 5 posts, 3 comments | 100 x 5 x 3 = 1500 |
| 100 users, 10 posts, 6 comments | 100 x 10 x 6 = 6000 |
Pattern observation: The total work grows by multiplying the counts at each nested level.
Time Complexity: O(u x p x c)
This means the work grows by multiplying the number of users, posts per user, and comments per post.
[X] Wrong: "The time grows only with the number of users, since posts and comments are just details."
[OK] Correct: Each post and comment requires separate work, so their counts multiply the total time.
Understanding nested resolver time helps you explain how data fetching scales in real apps, showing you know how backend work grows with data.
"What if comments were fetched in a single batch instead of one by one? How would the time complexity change?"