Sub topic 6
Using reactive variables
This chapter assumes you know how @wire and reactive properties work in LWC, and that you can write a basic gql query and read edges, node, and value from the result. Here you will learn how to drive a query with variables so it reruns when component state changes.
Why variables
A query with hard-coded values, such as first: 10, returns the same data every time. To change the page size, the filter, or the sort order, you declare variables in the query and supply their values from the component.
A variable has three parts:
- A declaration on the operation, such as
query accountList($first: Int). - A use inside the query, such as
Account(first: $first). - A value, passed through the
variableswire parameter.
Passing variables to the wire
The variables parameter takes a string that starts with $. LWC treats this as a reactive reference to a property or getter on the component. The getter returns a plain object whose keys match the variable names, without the $.
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($first: Int) {
uiapi {
query {
Account(first: $first, orderBy: { Name: { order: ASC } }) {
edges {
node {
Id
Name { value }
Industry { value displayValue }
}
}
}
}
}
}
`,
variables: '$variables'
})
wiredAccounts({ data, errors }) {
if (data) {
this.accounts = data.uiapi.query.Account.edges.map(({ node }) => ({
id: node.Id,
name: node.Name.value,
industry: node.Industry.displayValue ?? node.Industry.value
}));
this.errors = undefined;
} else if (errors) {
this.errors = errors;
this.accounts = [];
}
}
get variables() {
return { first: 10 };
}
}The string '$variables' points at the variables getter. Each time the wire service evaluates it, it receives the current object.
How reruns work
The wire service watches the reactive properties that the getter reads. When one of them changes, the service reads the getter again and sends the query with the new values. Your handler then runs with the new result.
In the earlier example, first is fixed, so the query runs once. To make it respond to the user, read a tracked property inside the getter:
pageSize = 10;
get variables() {
return { first: this.pageSize };
}
handleSizeChange(event) {
this.pageSize = Number(event.target.value);
}Class fields are reactive in current LWC, so assigning a new value to pageSize triggers the rerun. The same pattern drives the after cursor in pagination, which a later chapter covers.
Common errors
- Type mismatch. A variable declared as
Intmust receive a number. Values from input events are strings, so convert them withNumber(). - Missing declaration. Using
$firstin the query without declaring it on the operation makes the query invalid. - Name mismatch. The key in the returned object must match the variable name exactly.
{ pageSize: 10 }does not fill$first. - Mutating in place. Return a new object from the getter, not a stored object you edit. The wire service compares values to decide whether to rerun.
- Undefined values. If a required variable is
undefined, the query may not run or may fail. Give each variable a default, or guard the getter.
Exercise
A component declares query pagedAccounts($first: Int) and its getter returns { pageSize: this.size }. The query returns no records and reports an error. What is wrong, and how do you fix it?
Show answer
The key pageSize does not match the declared variable $first, so the variable receives no value. Change the getter to return { first: this.size }. Also confirm this.size is a number, not a string.