Sub topic 5
Reading edges, nodes, value, and displayValue
This chapter assumes you can write a basic LWC with @wire, and that you have already run a first query with gql and @wire(graphql). Here you will learn how to read the data that comes back.
The shape of a result
A query for records returns a connection. A connection is an object that wraps a list of records so it can also carry paging details. Each record sits inside an edge, and each edge has a node that holds the record's fields.
For a query on Account, the wire adapter gives you data with this path:
data.uiapi.query.Account.edges[n].nodeThe path mirrors your query. Every level you wrote in the query appears in data, and nothing you left out appears.
Reading value and displayValue
Most scalar fields do not return a bare value. They return a wrapper object, and you choose which parts to request:
valueis the raw data as stored. A date is an ISO string, a picklist is its API value, and a number is a number.displayValueis the same data formatted for the user. It follows the user's locale and, for picklists, uses the translated label.
Id is the exception. It is returned directly, with no wrapper, so you read node.Id.
const query = gql`
query accountList {
uiapi {
query {
Account(first: 5) {
edges {
node {
Id
Name { value }
Industry { value displayValue }
AnnualRevenue { value displayValue }
}
}
}
}
}
}
`;A node from this query looks like this:
{
"Id": "001xx000003DGb2AAG",
"Name": { "value": "Acme" },
"Industry": { "value": "Technology", "displayValue": "Technology" },
"AnnualRevenue": { "value": 1200000, "displayValue": "$1,200,000" }
}Request displayValue only for fields you show as text. Use value for logic such as comparisons or sorting in JavaScript.
Flattening the result for the template
Templates read plain properties more easily than nested wrappers. Map each edge to a simple object once, in the wire handler.
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 }
Industry { value displayValue }
}
}
}
}
}
}
`
})
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 = [];
}
}
}The ?? operator falls back to value when displayValue is null. The template then loops over plain objects:
<template>
<ul>
<template for:each={accounts} for:item="account">
<li key={account.id}>{account.name} ({account.industry})</li>
</template>
</ul>
</template>Common errors
| Symptom | Cause | Fix |
|---|---|---|
| `Cannot read properties of undefined (reading 'edges')` | The code reads `data` before it arrives, or the path skips `uiapi.query` | Check `if (data)` and use the full path |
| A field shows `[object Object]` | The wrapper was rendered instead of `value` | Read `node.Name.value` |
| `node.Name` is `undefined` | The field was not selected in the query | Add `Name { value }` to the query |
| `displayValue` is `null` | Some field types have no formatted form, or the value is empty | Fall back to `value` |
Exercise
A query selects Phone { value } and Type { value displayValue } on Account. Write the expression that reads the phone number and the readable account type from a node.
Show answer
node.Phone.value gives the phone number, and node.Type.displayValue gives the readable type, with node.Type.value as a fallback if displayValue is null.