1// datatableWithGraphqlPagination.js
2import { LightningElement, wire } from "lwc";
3import { gql, graphql } from "lightning/graphql";
4
5const columns = [
6 { label: "Name", fieldName: "Name" },
7 { label: "Phone", fieldName: "Phone", type: "phone" },
8 { label: "Website", fieldName: "Website", type: "url" },
9 { label: "Annual Revenue", fieldName: "AnnualRevenue", type: "currency" },
10];
11
12export default class DatatableWithGraphqlPagination extends LightningElement {
13 // Array of accounts to display
14 accounts = undefined;
15 // Errors from the wire adapter
16 errors = undefined;
17 // Columns for datatable
18 columns = columns;
19
20 // Cursor of the last returned record
21 lastCursor = null;
22
23 // Cursor for the next page
24 endCursor = null;
25
26 // Checks if the page is the last page
27 // Disables the next page button if true
28 isFinalPage = false;
29
30 // Total number of query results
31 totalCount = null;
32 page = 1;
33 totalPages = 1;
34 @wire(graphql, {
35 query: "$accountQuery",
36 variables: "$variables",
37 })
38 gqlQuery({ data, errors }) {
39 if (data) {
40 this.accounts = data.uiapi.query.Account.edges.map((edge) => ({
41 Id: edge.node.Id,
42 Name: edge.node.Name.value,
43 Phone: edge.node.Phone.value,
44 Website: edge.node.Website.value,
45 AnnualRevenue: edge.node.AnnualRevenue.displayValue,
46 }));
47 this.isFinalPage = !data.uiapi.query.Account.pageInfo.hasNextPage;
48 this.endCursor = data.uiapi.query.Account.pageInfo.endCursor;
49 this.totalCount = data.uiapi.query.Account.totalCount;
50 this.totalPages = Math.ceil(this.totalCount / 10);
51 }
52 this.errors = errors;
53 }
54
55 // Define the GraphQL query
56 get accountQuery() {
57 return gql`
58 query AccountWithName($after: String) {
59 uiapi {
60 query {
61 Account(after: $after) {
62 edges {
63 node {
64 Id
65 Name {
66 value
67 }
68 Phone {
69 value
70 }
71 Website {
72 value
73 }
74 AnnualRevenue {
75 displayValue
76 }
77 }
78 }
79 pageInfo {
80 endCursor
81 hasNextPage
82 hasPreviousPage
83 }
84 totalCount
85 }
86 }
87 }
88 }
89 `;
90 }
91
92 // Define variables for the GraphQL query
93 get variables() {
94 return {
95 after: this.lastCursor,
96 };
97 }
98
99 get currentPage() {
100 return this.totalCount === 0 ? 0 : this.page;
101 }
102
103 // Click handler for the reset button
104 resetPagingCursor(event) {
105 this.lastCursor = null;
106 this.page = 1;
107 }
108
109 // Click handler for the next page button
110 getNextPage(event) {
111 if (!this.isFinalPage) {
112 this.lastCursor = this.endCursor;
113 this.page++;
114 }
115 }
116}