Mocking resolvers in GraphQL - Time & Space Complexity
When we mock resolvers in GraphQL, we simulate data fetching without real database calls.
We want to know how the time to get data changes as we ask for more items.
Analyze the time complexity of the following GraphQL mocking resolver snippet.
const mocks = {
Query: () => ({
books: () => new Array(booksCount).fill(null).map(() => ({
title: 'Sample Book',
author: 'Author Name'
}))
})
};
This code creates a list of books with fixed data, simulating a database response.
Look for repeated actions in the code.
- Primary operation: Creating each book object in the array.
- How many times: Once for each book requested (booksCount times).
As the number of books requested grows, the time to create them grows too.
| Input Size (booksCount) | Approx. Operations |
|---|---|
| 10 | 10 book objects created |
| 100 | 100 book objects created |
| 1000 | 1000 book objects created |
Pattern observation: The work grows directly with the number of books requested.
Time Complexity: O(n)
This means the time to mock data grows in a straight line as you ask for more items.
[X] Wrong: "Mocking data is instant and does not depend on how many items we create."
[OK] Correct: Even though data is fake, creating each item takes time, so more items mean more work.
Understanding how mocking scales helps you explain performance in testing and development setups.
What if we changed the mock to return a fixed small list regardless of the requested size? How would the time complexity change?