# How does the @wire service adapter work for retrieving Salesforce data reactively?

**Topic:** LWC · **Difficulty:** Medium · **Tags:** LWC, Wire Service, Data Binding

Read it on the site: https://www.forcechallenges.com/interview-questions/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:**
1. The component is created and connected; the wire service then invokes the adapter with the config object.
2. Parameters starting with `$` are reactive: they are read from the component instance, and when they change the adapter runs again.
3. The adapter emits `{ data, error }`. Before the first result, `data` and `error` are undefined.
4. Properties receive the full `{data, error}` object; functions receive it as an argument, giving you a place for logic.
5. If any reactive parameter is `undefined`, the adapter is not called until it is defined, which is how you conditionally defer a call.
6. 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 `getObjectInfo`, `getPicklistValues`, `getListUi`, `getRelatedListRecords`, `CurrentPageReference` and more. For unsupported or complex queries use Apex; for on-demand or conditional calls use imperative Apex.

**Code / config example:**
```javascript
import { LightningElement, api, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';

export default class ContactList extends LightningElement {
    @api recordId;
    contacts = [];
    error;

    // reactive: re-runs whenever recordId changes
    @wire(getContacts, { accountId: '$recordId' })
    wiredContacts({ data, error }) {
        if (data) { this.contacts = data; this.error = undefined; }
        else if (error) { this.error = error; this.contacts = []; }
    }
}
```
```apex
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 LIMIT 200];
    }
}
```

**Hidden gotchas & must-remember points:**
- Cacheable Apex cannot perform DML and may return stale data until refreshed.
- Only the `$`-prefixed value is reactive; a plain string value is literal.
- Wire data is immutable; mutate a copy.
- A wire does not run while any reactive parameter is undefined.
- `error` and `data` are cleared per emission; do not assume the other one persists.
- Wired Apex data is not refreshed by DML you do imperatively; call `refreshApex` with the stored wire result.

**Best practice:** Use `@wire` for reads that should stay reactive and cached, and imperative Apex for actions.

---
Source: ForceChallenges · last updated 2026-11-02
