Sub topic 1
Recap: the Wire Service and Reactive Parameters
Prerequisites
Assume you know basic Lightning web component structure, JavaScript classes, HTML templates, Salesforce objects and fields, and the LWC @wire decorator. You should also know that a reactive property causes a wired service to run again when its value changes.
The wire service and reactive parameters
The LWC wire service provisions data to a component. A wired function receives the result, while a wired property stores it for the template. A parameter beginning with $ is reactive. When the matching component property changes, LWC reruns the wire adapter.
For example, variables: '$variables' tells LWC to use the component's variables getter. Changing a value returned by that getter causes a new request.
get variables() {
return { first: this.pageSize };
}A wire adapter can return data, errors, or both. Always handle the error path before rendering data that might be absent.
GraphQL essentials for Salesforce
GraphQL is a query language where the client requests the fields it needs. A query contains fields and can pass arguments, such as a record limit or sort order. GraphQL responses follow the shape of the query, so nested selections produce nested JavaScript objects.
The gql tag parses a query string. The graphql wire adapter sends that query through Salesforce's UI API.
Salesforce objects under `uiapi`
Salesforce places UI API GraphQL queries below the uiapi field. Records are usually returned through a connection:
querystarts a record query.Accountidentifies the Salesforce object.edgescontains result entries.nodecontains one record.Name { value }reads a field's raw value.displayValueprovides a formatted value when available.
Your first GraphQL wire query
import { LightningElement, wire } from 'lwc';
import { gql, graphql } from 'lightning/uiGraphQLApi';
export default class AccountList extends LightningElement {
accounts = [];
errors;
@wire(graphql, {
query: gql`
query accountList {
uiapi {
query {
Account(first: 10, orderBy: { Name: { order: ASC } }) {
edges {
node {
Id
Name { value }
}
}
}
}
}
}
`
})
wiredAccounts({ data, errors }) {
if (data) {
this.accounts = data.uiapi.query.Account.edges
.map(({ node }) => ({ id: node.Id, name: node.Name.value }));
this.errors = undefined;
} else if (errors) {
this.errors = errors;
this.accounts = [];
}
}
}Exercise
What causes a wired GraphQL query using variables: '$variables' to run again?
Show answer
Changing a reactive value returned by the component's variables getter.