Sub topic 9
Paging with Cursors
This chapter assumes you can write a @wire(graphql) query, read edges and node, and pass reactive variables through a getter. Here you will page through a large result set with first, after, and pageInfo.
Why cursors
The GraphQL API in Salesforce does not use page numbers or offsets. Each page request carries a cursor, which is an opaque string that marks a position in the result set. You ask for the records that come after that position.
Three pieces work together:
firstsets the page size.aftertakes the cursor of the last record you already have.pageInfocomes back with the results and tells you whether another page exists and which cursor to use next.
Treat cursors as opaque. Do not parse them, build them yourself, or store them for later sessions.
Requesting pageInfo
Add a pageInfo selection next to edges, and add $after as a query variable:
query pagedAccounts($first: Int, $after: String) {
uiapi {
query {
Account(first: $first, after: $after, orderBy: { Name: { order: ASC } }) {
edges { node { Id Name { value } } }
pageInfo { hasNextPage endCursor }
}
}
}
}hasNextPageistruewhen more records follow the current page.endCursoris the cursor of the last record on this page. Pass it asafterto get the next page.
The first request sends after: null, which means "start at the beginning".
A component that pages forward
The reactive variables getter reads pageSize and after. When handleNext changes after, the wire reruns with the new variables.
import { LightningElement, wire } from 'lwc';
import { gql, graphql } from 'lightning/uiGraphQLApi';
export default class AccountPager extends LightningElement {
pageSize = 5;
after = null;
accounts = [];
pageInfo = { hasNextPage: false, endCursor: null };
@wire(graphql, {
query: gql`
query pagedAccounts($first: Int, $after: String) {
uiapi {
query {
Account(first: $first, after: $after, orderBy: { Name: { order: ASC } }) {
edges { node { Id Name { value } } }
pageInfo { hasNextPage endCursor }
}
}
}
}
`,
variables: '$variables'
})
wiredPage({ data }) {
if (data) {
const result = data.uiapi.query.Account;
this.accounts = result.edges.map(({ node }) => ({ id: node.Id, name: node.Name.value }));
this.pageInfo = result.pageInfo;
}
}
get variables() {
return { first: this.pageSize, after: this.after };
}
handleNext() {
if (this.pageInfo.hasNextPage) {
this.after = this.pageInfo.endCursor;
}
}
}The wired function replaces accounts on every response. The component shows one page at a time, not a growing list.
The request cycle
Common mistakes
- Forgetting
pageInfoin the query. Without it,endCursoris undefined and you cannot continue. - Not guarding
handleNext. CheckhasNextPagefirst, or the button can send a stale cursor on the last page. - Keeping the order unstable. Always include
orderBy. Cursors are positions in an ordered list, so the order must be the same on every request. - Changing
whereororderBymid-paging. An old cursor does not apply to a different query. Resetaftertonullwhenever a filter or sort changes. - Building a cursor yourself. Only use values from
endCursor.
Exercise
A user changes a sort dropdown while on page 3 of a paged list. What should the component do with after, and why?
Show answer
Set after back to null along with the new orderBy value. A cursor marks a position in one specific ordered result set. After the sort changes, the old cursor no longer matches the query, so the list must restart from the first page.
Exercise
Which two pageInfo fields does a "Next" button rely on, and how is each used?
Show answer
hasNextPage decides whether the button is enabled or the click is handled. endCursor is assigned to the after variable so the wire fetches the following page.