Connection pattern (edges, nodes, pageInfo) in GraphQL - Time & Space Complexity
When using the connection pattern in GraphQL, we want to know how the time to get data changes as the list grows.
We ask: How does fetching edges, nodes, and pageInfo scale with more items?
Analyze the time complexity of the following GraphQL query using the connection pattern.
query GetUsers($first: Int, $after: String) {
users(first: $first, after: $after) {
edges {
node {
id
name
}
cursor
}
pageInfo {
hasNextPage
endCursor
}
}
}
This query fetches a page of users with their IDs and names, plus info to get the next page.
Look for repeated work inside the query.
- Primary operation: Fetching each user node inside edges.
- How many times: Once for each user requested (the page size).
As you ask for more users, the work grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 user nodes fetched |
| 100 | About 100 user nodes fetched |
| 1000 | About 1000 user nodes fetched |
Pattern observation: Doubling the number of users doubles the work.
Time Complexity: O(n)
This means the time grows linearly with the number of users requested in the page.
[X] Wrong: "Fetching pageInfo adds extra loops making it slower as n grows."
[OK] Correct: pageInfo is a small fixed set of data, so it does not grow with n and does not add to the main work.
Understanding how connection patterns scale helps you explain efficient data fetching in APIs clearly and confidently.
What if we added nested lists inside each node? How would that affect the time complexity?