The GraphQL wire adapter automatically caches your query results to improve the loading performance of subsequent queries. You can trigger a refresh of the query results and update the cache by calling the refresh method on the emitted data from the GraphQL wire adapter.
If you use a wire function, you can assign data and errors to a result variable like this.
@wire(graphql, { query: ... })
graphqlQueryResults(result) {
const { data, errors, refresh } = result;
}
Implement Cache Refresh in LWC
tip: The graphqlRefresh component in the lwc-recipes repo demonstrates how to refresh a query using the refresh method.
Here's an example that adds a refresh button to the example in Make Your Variables Reactive.
//refreshAccountsGQL.js
import { LightningElement, wire } from "lwc";
import { gql, graphql } from "lightning/graphql";
export default class AccountsGQL extends LightningElement {
records;
errors;
graphqlRefresh;
minAmount = "5000000";
minAmounts = [
{ label: "All", value: "0" },
{ label: "$5,000,000", value: "5000000" },
{ label: "$50,000,000", value: "50000000" },
{ label: "$500,000,000", value: "500000000" },
];
@wire(graphql, {
query: gql`
query bigAccounts($minAmount: Currency) {
uiapi {
query {
Account(where: { AnnualRevenue: { gte: $minAmount } }) {
edges {
node {
Id
Name {
value
}
AnnualRevenue {
displayValue
}
}
}
}
}
}
}
`,
variables: "$variables",
})
graphqlQueryResult({ data, errors, refresh }) {
if (data) {
this.records = data.uiapi.query.Account.edges.map((edge) => edge.node);
}
this.errors = errors;
// Store the refresh method to call later
this.graphqlRefresh = refresh;
}
async handleRefresh() {
return this.graphqlRefresh();
}
get variables() {
return {
minAmount: this.minAmount,
};
}
// Called when the user selects a new minimum amount
handleMinAmountChange(event) {
this.minAmount = event.detail.value;
}
}
If a record that's contained in a query result changes, Lightning Data Service emits a new value to the GraphQL wire adapter. However, Lightning Data Service doesn't reevaluate the query to see if the record still matches the query.
To ensure that Lightning Data Service reevaluates your query, use the refresh method. This example adds a button that enables you to trigger a refresh of the query results.
<!-- refreshAccountsGQL.html -->
<template>
<lightning-card title="accountsGQL" icon-name="standard:account">
<lightning-button slot="actions" label="Refresh" title="Refresh"
icon-name="utility:refresh" onclick={handleRefresh}
class="slds-m-left_x-small">
</lightning-button>
<div class="slds-var-m-horizontal_medium">
<lightning-combobox
name="minAmount"
label="Amount"
value={minAmount}
placeholder="Select a minimum amount"
options={minAmounts}
onchange={handleMinAmountChange} ></lightning-combobox>
<template lwc:if={records}>
<template for:each={records} for:item="account">
<div class="card-spacer" key={account.Id}>
<h1 slot="title">{account.Name.value}</h1>
{account.AnnualRevenue.displayValue}
</div>
</template>
</template>
</div>
</template>