Default resolvers in GraphQL - Time & Space Complexity
When using default resolvers in GraphQL, it's important to understand how the time it takes to get data changes as the data size grows.
We want to know how the work done by default resolvers increases when we ask for more items.
Analyze the time complexity of the following code snippet.
query {
books {
title
author {
name
}
}
}
This query asks for a list of books and for each book, it fetches the title and the author's name using default resolvers.
Look for repeated actions in the query resolution.
- Primary operation: The default resolver fetches the list of books once and then fetches the author for each book.
- How many times: Once for the books list, and once for each book (for its author).
As the number of books grows, the number of fetches grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 books fetch + 10 author fetches ≈ 11 operations |
| 100 | 1 books fetch + 100 author fetches ≈ 101 operations |
| 1000 | 1 books fetch + 1000 author fetches ≈ 1001 operations |
Pattern observation: The total work grows linearly with the number of books.
Time Complexity: O(n)
This means the time to get all books and their authors grows in a straight line as the number of books increases.
[X] Wrong: "Fetching nested fields with default resolvers is constant time regardless of list size."
[OK] Correct: Each book and its author are fetched separately, so more books mean more work, not the same amount.
Understanding how default resolvers scale helps you explain data fetching costs clearly and shows you know how GraphQL handles nested data.
"What if the author field was a list of authors instead of one? How would the time complexity change?"