Why is refreshGraphQL not updating the LWC wire?

Question:
I am using the @wire(graphql)
adapter in a Lightning Web Component (LWC) to fetch data. The data is processed as follows:
@wire(graphql, { query: '$getGraphQlQuery' })
wiredRecords(result) {
if (result.errors) console.error(result.errors);
if (!result.data) return;
this.recordsQlResult = result;
this.records = GraphQlService.toPlainObject(result.data, this.objectName);
}
I also have a method to refresh the GraphQL query:
@api
async refresh() {
await refreshGraphQL(this.recordsQlResult);
}
However, calling the refresh()
method successfully fetches new data, as verified through the Chrome DevTools network tab, but the @wire
function (wiredRecords
) is not re-invoked, and the component does not update. Why is this happening, and how can I fix it?
Answer:
The issue arises because of how the dynamic GraphQL query is constructed. Specifically, if the query uses dynamic field composition that is evaluated only once during the first request, subsequent refreshes do not trigger a new invocation of the @wire
function.
CRS Info Solutions is one of the premier institutes offering Salesforce training in Chennai. Enroll for free demo today.!!
Here’s an example of the problematic query construction:
get getGraphQlQuery() {
return gql`
query {
node {
${getFields}
}
}
`;
}
code explanation
The getGraphQlQuery
getter dynamically generates a GraphQL query. It uses the gql
template literal to define the query structure. The ${getFields}
syntax dynamically injects fields into the query, where getFields
is expected to be a variable or function returning a list of fields. This query is then returned for use in a GraphQL operation.
In this case, the getFields
variable is evaluated only once when the query is initially generated. Even though new data is fetched during the refresh()
call, the @wire
function is not triggered because the query string itself does not change.
To fix this issue, ensure that the dynamic fields or variables in the query are evaluated afresh on each invocation. For example, avoid using static evaluations like ${getFields
}
. Instead, dynamically regenerate the query entirely to ensure that the @wire
adapter detects a change in the query.
If the query string remains constant, the @wire
adapter does not treat it as a “new” query and does not re-invoke the function, even if the data changes.
This behavior is not unique to GraphQL but is generally how Lightning Data Service (LDS) and @wire
operate. Dynamic or reactive query generation is crucial for ensuring proper refresh behavior in such scenarios.
Summing Up
The refreshGraphQL
function in an LWC may fail to update the @wire
adapter when the GraphQL query uses static or pre-evaluated dynamic fields, such as ${getFields}
. This results in the query being treated as unchanged, preventing the wire function from being re-invoked even though new data is fetched. To resolve this, ensure the query string is dynamically regenerated on each invocation, so the @wire
adapter detects it as a new query and refreshes the data properly. This approach ensures seamless updates and reliable data handling in your LWC.
Accelerate Your Salesforce Career in Chennai
Elevate your Salesforce skills with expert-led training in Chennai, designed for both beginners and professionals. Gain practical experience through real-world projects and master cloud-based CRM solutions with guidance from certified instructors. Unlock new career opportunities and become a Salesforce expert with comprehensive, hands-on learning!
CRS Info Solutions is a leading Salesforce Training in Chennai, offering a comprehensive, real-time project-based curriculum. Our courses cover Salesforce Admin, Developer, Integration, Marketing Cloud, CPQ, and Lightning Web Components (LWC). With expert instructors providing hands-on training, we ensure you’re equipped for real-world challenges. Join us to become a certified Salesforce professional and kickstart your career.
Enroll now for a free demo at CRS Info Solutions, Chennai..!!