Sub topic 1
Recap: wiring a cacheable Apex method
This chapter recaps how a Lightning Web Component (LWC) reads data from Apex with @wire. Later chapters build on it to show how cached data goes stale and how refreshApex fixes that.
Prerequisites
This chapter assumes you know the following:
- Basic LWC structure: templates, decorators, and the component lifecycle.
- Basic Apex:
@AuraEnabledmethods and SOQL queries. - Promises and
async/awaitin JavaScript.
What cacheable=true means
An Apex method that an LWC wires must be marked @AuraEnabled(cacheable=true). This setting has two effects:
- The platform may store the method's result in a client-side cache. A later call with the same parameters can return the stored result instead of making a server round trip.
- The method must not change data. Apex rejects DML inside a cacheable method.
The Apex class below has one cacheable read method and one method that saves a change. The second method is not cacheable, so the platform lets it run DML.
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts(Id accountId) {
return [
SELECT Id, Name, Email
FROM Contact
WHERE AccountId = :accountId
WITH USER_MODE
ORDER BY Name
LIMIT 50
];
}
@AuraEnabled
public static void updateEmail(Id contactId, String email) {
Contact c = new Contact(Id = contactId, Email = email);
update as user c;
}
}WITH USER_MODE and update as user make the queries and the update respect the running user's permissions.
Importing and wiring the method
You import the Apex method by its path, @salesforce/apex/ClassName.methodName. You then pass it to @wire along with an object of parameters.
A parameter value that starts with $ is reactive. The wire reads the named property from the component and runs the method again whenever that property changes.
import { LightningElement, api, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactListSimple extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' })
contacts;
}Here @api recordId receives the record's Id from the page. The wire calls getContacts with it and stores the result in contacts.
What the wire provisions
The wire service assigns an object with two properties to contacts:
dataholds the returned records when the call succeeds.errorholds the error when the call fails.
Before the first response arrives, both properties are undefined. Your template should handle that state.
<template>
<template lwc:if={contacts.data}>
<template for:each={contacts.data} for:item="contact">
<p key={contact.Id}>{contact.Name}: {contact.Email}</p>
</template>
</template>
<template lwc:if={contacts.error}>
<p>Could not load contacts.</p>
</template>
</template>This object, not just data, is what refreshApex will need later. Keep that in mind as you work through the next chapters.
How the pieces fit together
The diagram shows the path of a wired call.
Check your understanding
Exercise
A teammate adds an update statement to a method marked @AuraEnabled(cacheable=true) and wires it. Why is this a problem, and how should the Apex be organized?
Show answer
Cacheable methods cannot modify data, so the DML is rejected. Keep the read in a cacheable method for the wire, and put the DML in a separate @AuraEnabled method without cacheable=true, which the component calls imperatively.
Exercise
In @wire(getContacts, { accountId: '$recordId' }) contacts;, what does the $ prefix do, and what does contacts hold?
Show answer
The $ makes the parameter reactive, so the wire calls the method again whenever recordId changes. contacts holds an object with data and error properties.