Sub topic 4
Your first query with gql and @wire(graphql)
This chapter assumes you can write a basic Lightning web component, use @wire with reactive properties, and read a GraphQL query with fields and arguments. It builds one working component that lists accounts. Later chapters cover filtering, relationships, and paging in depth.
The two imports
Both pieces come from one module:
import { gql, graphql } from 'lightning/uiGraphQLApi';gqlis a tagged template function. It takes your query text and returns a form the adapter can read. Write the query inside backticks directly aftergql.graphqlis the wire adapter. You pass it to@wirethe same way you passgetRecord.
The query must be a template literal written in place, because the platform reads the text when the component is compiled. Don't build it by joining strings at runtime.
The shape of a query
Every Salesforce query starts at uiapi, then query, then the object name. That is the path that exposes your objects.
The diagram below shows how the query nests.
Each record sits inside node, and edges is the list of records. Field values come wrapped, so you ask for Name { value } rather than Name. Id is the exception and is returned as a plain string.
The component
This component queries the first ten accounts by name.
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 };
}
}Three parts of this wire configuration matter.
queryholds thegqltext.$firstis a variable declared in the operation header asInt.variables: '$variables'points to the gettervariables. The leading$makes it reactive, as with any wire parameter. The getter returns an object whose keys match the variable names, without the$.- The decorated function receives one object. It has
dataon success anderrorson failure.
The function maps each edge to a plain object. Industry is a picklist, so displayValue gives the translated label and value gives the stored API value. If no label exists, the code falls back to value.
Note that the result is data.uiapi.query.Account.edges. The response mirrors the query, so the path you wrote is the path you read.
The template
<template>
<lightning-card title="Accounts" icon-name="standard:account">
<template lwc:if={errors}>
<p class="slds-p-around_medium">The accounts could not be loaded.</p>
</template>
<ul class="slds-p-around_medium">
<template for:each={accounts} for:item="account">
<li key={account.id}>{account.name} ({account.industry})</li>
</template>
</ul>
</lightning-card>
</template>The template only sees the mapped objects, so it never touches node or value.
Common mistakes
- Using
Nameinstead ofName { value }. The query fails validation because the field is an object type. - Leaving out
variables, or writingvariables: 'variables'without$. The query then runs with nofirstvalue, or never updates. - Reading
data.Accountinstead ofdata.uiapi.query.Account. - Assuming
dataanderrorscan't both be present. Chapter 10 covers partial results, so don't treaterrorsas a full failure until then.
Exercise
A component's query declares $first: Int, but the wire config is variables: { first: 10 } with no $. What is the difference from variables: '$variables' backed by a getter, and which one lets the query rerun when component state changes?
Show answer
A plain object is a fixed value, so the query never reruns. A '$variables' string refers to a component property or getter. When a value it reads changes, the wire reruns the query with the new variables. Use the $ form for reactivity.