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?
Practice
pageInfo field in the GraphQL connection pattern?Solution
Step 1: Understand the role of
ThepageInfopageInfofield contains metadata about pagination such as whether there are more pages available.Step 2: Differentiate from
Unlikeedgesandnodesedgesornodeswhich hold data items,pageInfoonly provides pagination status.Final Answer:
To provide information about pagination like if there are more pages -> Option CQuick Check:
pageInfo = pagination info [OK]
- Confusing pageInfo with nodes or edges
- Thinking pageInfo holds actual data items
- Ignoring pagination metadata role
edges and node fields in a GraphQL connection?Solution
Step 1: Recall correct field names in connection pattern
The connection pattern usesedges(plural) and inside each edge anode(singular) holds the data.Step 2: Check each option's syntax
{ users { edges { node { id name } } } }correctly usesedgesandnode.{ users { nodes { id name } } }usesnodeswhich is valid but not with edges.{ users { pageInfo { hasNextPage } } }queries only pageInfo.{ users { edge { node { id name } } } }usesedgesingular which is incorrect.Final Answer:
{ users { edges { node { id name } } } } -> Option DQuick Check:
edges plural, node singular inside edges [OK]
- Using 'edge' instead of 'edges'
- Mixing nodes and edges syntax
- Querying pageInfo instead of data fields
{ posts(first: 2) { edges { cursor node { id title } } pageInfo { hasNextPage } } }And the server returns two posts with ids 101 and 102, what will
pageInfo.hasNextPage be if there are more posts after these two?Solution
Step 1: Understand the meaning of
hasNextPagehasNextPageis true if more items exist beyond the current page.Step 2: Apply to given scenario
Since the server returns 2 posts but more posts exist after them,hasNextPagemust be true.Final Answer:
true -> Option AQuick Check:
More posts after current page means hasNextPage = true [OK]
- Assuming hasNextPage is false when items equal page size
- Confusing hasNextPage with hasPreviousPage
- Expecting null or undefined instead of boolean
{ comments { edge { node { id text } } } }But it returns an error. What is the likely cause?
Solution
Step 1: Check the connection field names
The connection pattern usesedges(plural) notedge(singular).Step 2: Identify the error cause
Usingedgecauses a field not found error because the schema expectsedges.Final Answer:
The field should be 'edges' not 'edge' -> Option BQuick Check:
edges plural required, edge singular causes error [OK]
- Using 'edge' instead of 'edges'
- Thinking 'node' should be plural
- Assuming pageInfo is mandatory for query success
Solution
Step 1: Understand the difference between nodes and edges
nodesgives a simple list of items without pagination details, whileedgesincludes cursors for pagination.Step 2: Choose the query matching the need
Since pagination info is not needed, queryingnodesdirectly is simpler and efficient.Final Answer:
{ products { nodes { id name } } } -> Option AQuick Check:
Use nodes for simple list without pagination [OK]
- Using edges and pageInfo when not needed
- Querying only pageInfo without data
- Ignoring simpler nodes field for lists
Ready to prove it?
Pass the test and this pattern moves unknown
