Pagination with first and after in GraphQL - Time & Space Complexity
When using pagination with first and after in GraphQL, it's important to understand how the time to get results changes as the data grows.
We want to know how the number of operations grows when we ask for pages of data.
Analyze the time complexity of the following GraphQL query snippet.
query GetItems($first: Int, $after: String) {
items(first: $first, after: $after) {
edges {
node {
id
name
}
}
pageInfo {
endCursor
hasNextPage
}
}
}
This query fetches a page of items, starting after a cursor, limited by the number first.
Look for repeated work inside the query execution.
- Primary operation: Scanning items to find the start position after the cursor.
- How many times: Depends on the position of
afterin the list and the numberfirstrequested.
As the total number of items grows, finding the start point after the cursor may require scanning more items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Scan up to 10 items to find start, then return first items. |
| 100 | Scan up to 100 items to find start, then return first items. |
| 1000 | Scan up to 1000 items to find start, then return first items. |
Pattern observation: The work grows roughly in proportion to how far into the list the after cursor is, plus the number of items requested.
Time Complexity: O(n)
This means the time to get a page grows linearly with how far you have to scan to reach the cursor position.
[X] Wrong: "Pagination with first and after always takes the same time no matter how big the data is."
[OK] Correct: The server often needs to scan items up to the cursor position, so if the cursor is far in the list, it takes longer.
Understanding how pagination scales helps you design efficient queries and explain performance trade-offs clearly in real projects.
What if the data was indexed by cursor so the server could jump directly to the after position? How would the time complexity change?
Practice
What does the first argument do in GraphQL pagination?
Solution
Step 1: Understand the role of
Thefirstin paginationfirstargument tells GraphQL how many items to return in the current page.Step 2: Differentiate from
afteraftersets the starting point, butfirstlimits the count of items fetched.Final Answer:
Limits the number of items returned starting from the beginning or after a cursor -> Option AQuick Check:
first= limit count [OK]
- Confusing first with after
- Thinking first sets the start position
- Assuming first returns all items
Which of the following is the correct syntax to fetch the first 5 items after a cursor "abc123" in GraphQL?
{ posts(first: 5, after: "abc123") { edges { node { id } } } }Solution
Step 1: Check argument order and types
The correct syntax usesfirst: 5(number) andafter: "abc123"(string) with a comma separating them.Step 2: Identify syntax errors in other options
{ posts(after: 5, first: "abc123") { edges { node { id } } } } swaps argument names incorrectly. { posts(first: "5", after: abc123) { edges { node { id } } } } uses quotes incorrectly and misses quotes around cursor. { posts(first: 5 after: "abc123") { edges { node { id } } } } misses comma between arguments.Final Answer:
{ posts(first: 5, after: "abc123") { edges { node { id } } } } -> Option CQuick Check:
Correct argument names and types with commas [OK]
- Missing commas between arguments
- Using wrong argument order
- Not quoting cursor strings
Given this GraphQL query:
{ users(first: 3) { edges { node { name } cursor } pageInfo { hasNextPage endCursor } } }And the data has 5 users: Alice, Bob, Carol, Dave, Eve. What will be the edges returned?
Solution
Step 1: Understand
The query requests the first 3 users from the start of the list.first: 3effectStep 2: Identify first 3 users in data
The users are Alice, Bob, Carol, Dave, Eve. The first 3 are Alice, Bob, and Carol.Final Answer:
[{name: "Alice"}, {name: "Bob"}, {name: "Carol"}] -> Option BQuick Check:
first 3 users = Alice, Bob, Carol [OK]
- Returning all users instead of limited
- Confusing first with after
- Returning empty list by mistake
Identify the error in this GraphQL pagination query:
{ comments(first: 10 after: "cursor123") { edges { node { text } } } }Solution
Step 1: Check argument syntax
GraphQL requires commas between arguments. The query misses a comma betweenfirst: 10andafter: "cursor123".Step 2: Validate other parts
Cursor should be a quoted string (correct).firstis a number (correct).edgesis valid in pagination.Final Answer:
Missing comma betweenfirstandafterarguments -> Option AQuick Check:
Arguments need commas [OK]
- Omitting commas between arguments
- Not quoting cursor strings
- Misunderstanding argument types
You have a list of 12 products. You want to fetch products in pages of 4 using first and after. After fetching the first 4 products, you get an endCursor value of "cursor4". What query should you use to fetch the next 4 products?
Solution
Step 1: Use
You want 4 items per page, sofirstto limit items per pagefirst: 4is correct.Step 2: Use
To fetch the next page, useafterwith the last page'sendCursorafter: "cursor4"with quotes as it is a string cursor.Step 3: Check syntax correctness
{ products(first: 4, after: "cursor4") { edges { node { id name } } pageInfo { hasNextPage endCursor } } } uses correct argument order, types, and includespageInfofor navigation.Final Answer:
{ products(first: 4, after: "cursor4") { edges { node { id name } } pageInfo { hasNextPage endCursor } } } -> Option DQuick Check:
Next page = first 4 after last cursor [OK]
- Swapping first and after argument values
- Not quoting cursor strings
- Requesting more items than page size
Ready to prove it?
Pass the test and this pattern moves unknown
