Pagination
Kumar Preeti Lata
Microsoft Certified: Senior Data Analyst/ Senior Data Engineer | Prompt Engineer | Gen AI | SQL, Python, R, PowerBI, Tableau, ETL| DataBricks, ADF, Azure Synapse Analytics | PGP Cloud Computing | MSc Data Science
What is Pagination?
Pagination is the technique of dividing a large set of data into smaller, manageable chunks or pages.
Think of it like flipping through pages of a book instead of reading the whole book on one giant scroll.
Why Use Pagination?
Real-Life Example: An Online Store
How Pagination Works in GraphQL
In GraphQL, pagination is often implemented using two main techniques:
Offset-Based Pagination
Example Query: Fetch 5 Users Starting from the 6th User
query {
users(limit: 5, offset: 5) {
id
name
}
}
Expected Response:
{
"data": {
"users": [
{ "id": "6", "name": "User6" },
{ "id": "7", "name": "User7" },
{ "id": "8", "name": "User8" },
{ "id": "9", "name": "User9" },
{ "id": "10", "name": "User10" }
]
}
}
Cursor-Based Pagination (Recommended for Large Data)
Example Query: Fetch 3 Users After a Specific Cursor
query {
users(first: 3, after: "cursor123") {
edges {
node {
id
name
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
Expected Response:
{
"data": {
"users": {
"edges": [
{ "node": { "id": "4", "name": "User4" } },
{ "node": { "id": "5", "name": "User5" } },
{ "node": { "id": "6", "name": "User6" } }
],
"pageInfo": {
"hasNextPage": true,
"endCursor": "cursor456"
}
}
}
}
Key Concepts in Cursor-Based Pagination:
Which One to Use?