0
0
GraphQLquery~5 mins

Refetching and polling in GraphQL - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Refetching and polling
O(n)
Understanding Time Complexity

When using refetching and polling in GraphQL, we want to understand how the time it takes to get data changes as we ask for more updates.

We ask: How does the number of operations grow when we repeatedly fetch data?

Scenario Under Consideration

Analyze the time complexity of the following GraphQL polling query.


query GetMessages {
  messages {
    id
    content
    timestamp
  }
}

# Polling every 5 seconds to get new messages

This query fetches a list of messages repeatedly at fixed intervals to keep data fresh.

Identify Repeating Operations

Look for repeated actions that affect time.

  • Primary operation: Fetching the entire list of messages each poll.
  • How many times: Once every polling interval (e.g., every 5 seconds).
How Execution Grows With Input

As the number of messages grows, each poll fetches more data, so the work grows with the list size.

Input Size (n messages)Approx. Operations per Poll
1010 fetch operations
100100 fetch operations
10001000 fetch operations

Pattern observation: The work grows linearly as the number of messages increases.

Final Time Complexity

Time Complexity: O(n)

This means the time to fetch data grows directly with the number of messages each time we poll.

Common Mistake

[X] Wrong: "Polling only fetches new messages, so time stays the same no matter how many messages exist."

[OK] Correct: Polling as shown fetches the whole list every time, so more messages mean more work each poll.

Interview Connect

Understanding how repeated data fetching scales helps you explain real-world app behavior and design efficient updates.

Self-Check

What if we changed polling to only fetch messages newer than the last one received? How would the time complexity change?