Sub topic 11
Refreshing results after changes
This chapter assumes you can write a @wire(graphql) query with the gql tag and read edges and node from the result. It also assumes you know how reactive variables trigger reruns. Here you will learn how to re-run a wired query after the underlying data changes.
Why a wired query goes stale
The wire service caches results. If another part of the page edits a record, or your code creates one, the wired query does not re-run on its own. The component keeps showing the cached data until something tells the adapter to fetch again.
Changing a reactive variable does cause a new request, but only because the query inputs are different. When the inputs have not changed, you need an explicit refresh.
Keep the whole result object
refreshGraphQL needs the object the wire service gave you, not the data inside it. That object holds data, errors, and the internal handle used to refresh the query.
Assign the wire result to a property, as in the example below. Do not destructure it in a handler method, because that discards the handle.
import { LightningElement, wire } from 'lwc';
import { gql, graphql, refreshGraphQL } from 'lightning/uiGraphQLApi';
export default class RefreshableAccounts extends LightningElement {
@wire(graphql, {
query: gql`
query recentAccounts {
uiapi {
query {
Account(first: 5, orderBy: { CreatedDate: { order: DESC } }) {
edges { node { Id Name { value } } }
}
}
}
}
`
})
graphqlResult;
get accounts() {
return this.graphqlResult?.data?.uiapi.query.Account.edges.map(({ node }) => ({
id: node.Id,
name: node.Name.value
})) ?? [];
}
async handleRefresh() {
await refreshGraphQL(this.graphqlResult);
}
}Three details matter here:
graphqlResultis a property wire, so it receives{ data, errors }objects as the query runs.- The getter reads
datawith optional chaining, because the property isundefinedbefore the first response. refreshGraphQLreturns a promise. Await it if you need to run code after the new data arrives, such as hiding a spinner.
When to call refresh
Use the diagram below to follow the order of events.
Call refreshGraphQL after the change has finished. If you call it while the save is still in progress, the query can return the old values and you will see no update.
Typical triggers include:
- A button the user clicks to reload the list.
- The success handler after an Apex call or a record update.
- An event from a child component that edited data.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Refresh does nothing | You passed `data` or a copy instead of the wire result | Pass the property that holds the full wire result |
| `this.graphqlResult` is undefined in the handler | The first response has not arrived yet | Guard the call or disable the button until data exists |
| List still shows old values | Refresh ran before the save finished | Await the save, then call refresh |
| Refresh works for one component only | Each component holds its own wired result | Call refresh in each component, or notify them with an event |
Refresh and pagination
A refresh re-runs the query with its current variables. If the user is on page three because after holds a cursor, the refresh fetches page three again. It does not return to the first page. Reset after to null yourself if you want the list to start over.
Exercise
A component destructures the wire result as wiredAccounts({ data, errors }) and stores only data in this.accounts. A button handler then calls refreshGraphQL(this.accounts). Why does the list not update, and what is the smallest change that fixes it?
Show answer
refreshGraphQL needs the full wire result object, which carries the handle for the query. this.accounts holds only the mapped data, so there is nothing for the adapter to refresh. Save the whole result in a field, for example this.graphqlResult = result inside the wired function, and pass that field to refreshGraphQL.