Skip to content

Commit a0cd7ff

Browse files
authored
refactor: use lightning/graphql (#1059)
* refactor: use lightning/graphql * wip * fix: refresh * fix: lint * fix: prettier
1 parent 0d2305d commit a0cd7ff

File tree

16 files changed

+192
-179
lines changed

16 files changed

+192
-179
lines changed

force-app/main/default/lwc/graphqlContacts/__tests__/graphqlContacts.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createElement } from 'lwc';
22
import GraphqlContacts from 'c/graphqlContacts';
3-
import { graphql } from 'lightning/uiGraphQLApi';
3+
import { graphql } from 'lightning/graphql';
44

55
// Mock realistic data
66
const mockGraphQL = require('./data/graphqlContactsResponse.json');

force-app/main/default/lwc/graphqlContacts/graphqlContacts.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LightningElement, wire } from 'lwc';
2-
import { gql, graphql } from 'lightning/uiGraphQLApi';
2+
import { gql, graphql } from 'lightning/graphql';
33

44
export default class GraphqlContacts extends LightningElement {
55
@wire(graphql, {

force-app/main/default/lwc/graphqlMultipleObjects/__tests__/graphqlMultipleObjects.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createElement } from 'lwc';
22
import GraphqlMultipleObjects from 'c/graphqlMultipleObjects';
3-
import { graphql } from 'lightning/uiGraphQLApi';
3+
import { graphql } from 'lightning/graphql';
44

55
// Mock realistic data
66
const mockGraphQL = require('./data/graphqlMultipleObjectsResponse.json');

force-app/main/default/lwc/graphqlMultipleObjects/graphqlMultipleObjects.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LightningElement, wire } from 'lwc';
2-
import { gql, graphql } from 'lightning/uiGraphQLApi';
2+
import { gql, graphql } from 'lightning/graphql';
33

44
export default class GraphqlMultipleObjects extends LightningElement {
55
@wire(graphql, {

force-app/main/default/lwc/graphqlPagination/__tests__/graphqlPagination.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createElement } from 'lwc';
22
import GraphqlPagination from 'c/graphqlPagination';
3-
import { graphql } from 'lightning/uiGraphQLApi';
3+
import { graphql } from 'lightning/graphql';
44

55
// Mock realistic data
66
const mockGraphQLFirstPage = require('./data/graphqlPaginationResponseFirstPage.json');

force-app/main/default/lwc/graphqlPagination/graphqlPagination.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LightningElement, wire } from 'lwc';
2-
import { gql, graphql } from 'lightning/uiGraphQLApi';
2+
import { gql, graphql } from 'lightning/graphql';
33

44
const pageSize = 3;
55

force-app/main/default/lwc/graphqlRefresh/__tests__/graphqlRefresh.test.js

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createElement } from 'lwc';
22
import GraphqlRefresh from 'c/graphqlRefresh';
3-
import { graphql, refreshGraphQL } from 'lightning/uiGraphQLApi';
3+
import { graphql } from 'lightning/graphql';
44
import randomizeAccountData from '@salesforce/apex/AccountController.randomizeAccountData';
55

66
// Mock realistic data
@@ -21,18 +21,6 @@ jest.mock(
2121
{ virtual: true }
2222
);
2323

24-
// Mock uiGraphQLApi module
25-
jest.mock(
26-
'@salesforce/uiGraphQLApi',
27-
() => {
28-
// Inject a refreshGraphQL mock
29-
return {
30-
refreshGraphQL: jest.fn(() => Promise.resolve())
31-
};
32-
},
33-
{ virtual: true }
34-
);
35-
3624
describe('c-graphql-refresh', () => {
3725
afterEach(() => {
3826
// The jsdom instance is shared across test cases in a single file so reset the DOM
@@ -149,15 +137,16 @@ describe('c-graphql-refresh', () => {
149137
});
150138

151139
describe('graphql refresh', () => {
152-
it('calls refreshGraphQL when refresh is clicked', async () => {
140+
it('calls this.refreshGraphQL when refresh is clicked', async () => {
153141
// Create component
154142
const element = createElement('c-graphql-refresh', {
155143
is: GraphqlRefresh
156144
});
157145
document.body.appendChild(element);
158146

147+
const refreshGraphQL = jest.fn().mockResolvedValue();
159148
// Emit data from @wire
160-
graphql.emit(mockGraphQL);
149+
graphql.emit(mockGraphQL, () => true, refreshGraphQL);
161150

162151
// Wait for any asynchronous DOM updates
163152
await flushPromises();

force-app/main/default/lwc/graphqlRefresh/graphqlRefresh.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { LightningElement, wire } from 'lwc';
2-
import { gql, graphql, refreshGraphQL } from 'lightning/uiGraphQLApi';
2+
import { gql, graphql } from 'lightning/graphql';
33
import randomizeAccountData from '@salesforce/apex/AccountController.randomizeAccountData';
44

55
export default class GraphqlRefresh extends LightningElement {
66
graphqlResult;
77
account;
88
errors;
99
isLoading = true;
10+
refreshGraphQL;
1011

1112
@wire(graphql, {
1213
query: gql`
@@ -39,10 +40,11 @@ export default class GraphqlRefresh extends LightningElement {
3940
this.account = undefined;
4041
this.errors = undefined;
4142

42-
// We hold a direct reference to the graphQL query result
43-
// so that we can refresh it with refreshGraphQL
44-
this.graphqlQueryResult = result;
45-
const { errors, data } = result;
43+
const { errors, data, refresh } = result;
44+
// We hold a reference to the refresh function on the graphQL query result so we can call it later.
45+
if (refresh) {
46+
this.refreshGraphQL = refresh;
47+
}
4648
if (data) {
4749
const accounts = data.uiapi.query.Account.edges.map((edge) => ({
4850
Id: edge.node.Id,
@@ -74,7 +76,7 @@ export default class GraphqlRefresh extends LightningElement {
7476
async handleRefreshClick() {
7577
this.isLoading = true;
7678
try {
77-
await refreshGraphQL(this.graphqlQueryResult);
79+
await this.refreshGraphQL?.();
7880
} catch (e) {
7981
this.errors = e;
8082
} finally {

force-app/main/default/lwc/graphqlVariables/__tests__/graphqlVariables.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createElement } from 'lwc';
22
import GraphqlVariables from 'c/graphqlVariables';
3-
import { graphql } from 'lightning/uiGraphQLApi';
3+
import { graphql } from 'lightning/graphql';
44

55
// Mock realistic data
66
const mockGraphQL = require('./data/graphqlVariablesResponse.json');

force-app/main/default/lwc/graphqlVariables/graphqlVariables.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { LightningElement, wire } from 'lwc';
2-
import { gql, graphql } from 'lightning/uiGraphQLApi';
2+
import { gql, graphql } from 'lightning/graphql';
33

44
/** The delay used when debouncing event handlers before invoking Apex. */
55
const DELAY = 300;

0 commit comments

Comments
 (0)