0
0
GraphQLquery~5 mins

Mocking resolvers in GraphQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Mocking resolvers
O(n)
Understanding Time 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.

Scenario Under Consideration

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.

Identify Repeating Operations

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).
How Execution Grows With Input

As the number of books requested grows, the time to create them grows too.

Input Size (booksCount)Approx. Operations
1010 book objects created
100100 book objects created
10001000 book objects created

Pattern observation: The work grows directly with the number of books requested.

Final Time Complexity

Time Complexity: O(n)

This means the time to mock data grows in a straight line as you ask for more items.

Common Mistake

[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.

Interview Connect

Understanding how mocking scales helps you explain performance in testing and development setups.

Self-Check

What if we changed the mock to return a fixed small list regardless of the requested size? How would the time complexity change?