Sub topic 3
Paging with Cursors in the GraphQL Wire Adapter
Assume you know basic LWC classes, @wire, Salesforce relationships, and GraphQL fields and arguments.
Paging with cursors
Cursor pagination loads a stable slice of records instead of requesting every record at once. first sets the page size. after identifies the cursor after which Salesforce should continue. pageInfo reports whether another page exists and provides its cursor.
import { LightningElement, wire } from 'lwc';
import { gql, graphql } from 'lightning/uiGraphQLApi';
export default class AccountPager extends LightningElement {
accounts = [];
after = null;
pageInfo = { hasNextPage: false, endCursor: null };
errors;
@wire(graphql, {
query: gql`
query accounts($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'
})
wiredAccounts({ data, errors }) {
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;
this.errors = undefined;
} else if (errors) {
this.errors = errors;
}
}
get variables() {
return { first: 10, after: this.after };
}
handleNext() {
if (this.pageInfo.hasNextPage) {
this.after = this.pageInfo.endCursor;
}
}
}Changing the reactive after property reruns the wire adapter. Replace the displayed page rather than appending unless you explicitly need an accumulated list.
Handling errors and partial data
GraphQL can return both data and errors. Render usable data when it exists, but show an error state when errors is present. Avoid assuming every requested field is available.
| Result | Recommended behavior |
|---|---|
| `data` only | Render the page. |
| `errors` only | Show an error and offer retry. |
| `data` and `errors` | Render available data and explain that some results may be incomplete. |
Refreshing results after changes
Keep the complete wire result when a later operation changes records. Pass that result to refreshGraphQL.
import { refreshGraphQL } from 'lightning/uiGraphQLApi';
async handleRefresh() {
await refreshGraphQL(this.graphqlResult);
}The result property must be assigned by the wire adapter, for example, graphqlResult. Refreshing updates the cached query result.
Security behavior and limits to plan for
The UI API applies sharing, field-level security, and object permissions for the current user. A missing field can therefore appear unavailable, and a query can fail because of access restrictions. Keep pages small, request only needed fields, and handle limits and partial results without exposing internal error details.
Going further: aggregates and mutations
Aggregates can summarize records when the supported schema exposes the required operation. Mutations change data and should be followed by a refresh of affected wired queries. Check the Salesforce schema and release documentation for supported aggregate and mutation fields before designing around them.
Exercise
Which value should the component pass as after to request the next page?
Show answer
Pass the previous page's pageInfo.endCursor, after confirming hasNextPage is true.