How does the @wire service adapter work for retrieving Salesforce data reactively?
Answer
Basics: Instead of writing "call the server, wait, store result", you declare what you want: @wire(getRecord, { recordId: '$recordId', fields: FIELDS }) record;. The platform runs the adapter, caches results client-side and pushes new values to your component. The data is read-only. It suits read operations that should stay in sync with the UI.
How it works:
- The component is created and connected; the wire service then invokes the adapter with the config object.
- Parameters starting with
$are reactive: they are read from the component instance, and when they change the adapter runs again. - The adapter emits
{ data, error }. Before the first result,dataanderrorare undefined. - Properties receive the full
{data, error}object; functions receive it as an argument, giving you a place for logic. - If any reactive parameter is
undefined, the adapter is not called until it is defined, which is how you conditionally defer a call. - Apex methods are wired only if annotated
@AuraEnabled(cacheable=true).
Advanced / deep dive: Wire adapters sit on top of Lightning Data Service, so identical requests from different components share one cache entry and one network call. Cached Apex results are held client-side; refreshApex(wiredResult) bypasses the cache and re-requests. Because cacheable=true methods cannot do DML (the platform raises an error), writes must go through imperative Apex or updateRecord. Wire results are frozen objects; clone them to edit. Emission order versus lifecycle hooks: provisioning happens after the component is connected, and it can fire again later, so code in the handler must be re-entrant. Wire for a function adapter lets you reshape data and handle errors in one place. Adapters also exist for , , , , and more. For unsupported or complex queries use Apex; for on-demand or conditional calls use imperative Apex.