The connection pattern helps you get lists of items in small parts, making it easy to load and show data step-by-step.
Connection pattern (edges, nodes, pageInfo) in GraphQL
Introduction
Syntax
GraphQL
type Connection {
edges: [Edge]
nodes: [Node]
pageInfo: PageInfo!
}
type Edge {
cursor: String!
node: Node
}
type PageInfo {
hasNextPage: Boolean!
hasPreviousPage: Boolean!
startCursor: String
endCursor: String
}edges hold the items with cursors to track position.
nodes is a shortcut list of items without cursors.
Examples
GraphQL
query {
users(first: 3) {
edges {
cursor
node {
id
name
}
}
pageInfo {
hasNextPage
endCursor
}
}
}GraphQL
query {
posts(first: 5) {
nodes {
id
title
}
pageInfo {
hasPreviousPage
startCursor
}
}
}Sample Program
This query asks for the first 2 books, their ids and titles, and if there are more books after them.
GraphQL
query {
books(first: 2) {
edges {
cursor
node {
id
title
}
}
pageInfo {
hasNextPage
endCursor
}
}
}Important Notes
Use edges when you need the cursor for each item to load more data later.
pageInfo tells you if you can load more items before or after the current list.
nodes is simpler but does not give cursor info for pagination.
Summary
The connection pattern helps load lists in small parts for better performance.
It uses edges with cursors and pageInfo to manage pagination.
You can also use nodes for a simpler list without pagination details.
Practice
1. What is the main purpose of the
pageInfo field in the GraphQL connection pattern?easy
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]
Hint: Remember pageInfo tells if more pages exist [OK]
Common Mistakes:
- Confusing pageInfo with nodes or edges
- Thinking pageInfo holds actual data items
- Ignoring pagination metadata role
2. Which of the following is the correct way to query the
edges and node fields in a GraphQL connection?easy
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]
Hint: Edges is plural; node is singular inside edges [OK]
Common Mistakes:
- Using 'edge' instead of 'edges'
- Mixing nodes and edges syntax
- Querying pageInfo instead of data fields
3. Given this GraphQL query:
And the server returns two posts with ids 101 and 102, what will
{ 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?medium
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]
Hint: More items after current page means hasNextPage true [OK]
Common Mistakes:
- Assuming hasNextPage is false when items equal page size
- Confusing hasNextPage with hasPreviousPage
- Expecting null or undefined instead of boolean
4. You wrote this GraphQL query:
But it returns an error. What is the likely cause?
{ comments { edge { node { id text } } } }But it returns an error. What is the likely cause?
medium
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]
Hint: Use 'edges' plural, not 'edge' singular [OK]
Common Mistakes:
- Using 'edge' instead of 'edges'
- Thinking 'node' should be plural
- Assuming pageInfo is mandatory for query success
5. You want to fetch a list of products with their IDs and names using the connection pattern. You only need the list of products without pagination info. Which query is best?
hard
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]
Hint: Use nodes for simple lists without pagination [OK]
Common Mistakes:
- Using edges and pageInfo when not needed
- Querying only pageInfo without data
- Ignoring simpler nodes field for lists
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
