Overview
ThegraphqlRefresh component demonstrates how to programmatically refresh a GraphQL query result. It stores a reference to the refresh function and calls it to reload data after external changes.
Source
force-app/main/default/lwc/graphqlRefresh
Key Features
- Manual query refresh using the
refreshfunction - Storing refresh function reference
- Wire adapter callback pattern
- Loading state management
- Integration with Apex for data updates
Wire Adapter with Callback
Storing the Refresh Function
Capture therefresh function from the wire adapter result:
Wire Adapter Callback Pattern
Use a method instead of a property to receive the wire adapter result:refresh function and manual control over data processing.
Calling the Refresh Function
Invoke the stored refresh function to reload data:?.() safely handles cases where the refresh function might not be available.
Use Case: External Data Updates
Refresh is useful when data changes outside the component’s GraphQL query:refreshGraphQL() to reload the query result.
Loading State Management
Track loading state during refresh operations:Template with Loading Spinner
Refresh Pattern Summary
- Use wire adapter callback pattern to access the result object
- Destructure
refreshfunction from the result - Store refresh function in a component property
- Call the stored function to re-execute the query
- Handle loading state during refresh operations
When to Use Refresh
- After making data changes via Apex or imperative calls
- When polling for updated data
- After record mutations outside the GraphQL query
- When implementing manual reload functionality
