Sub topic 5
Reading edges, nodes, value, and displayValue
This chapter assumes you can write a Lightning web component with a @wire property or function. It also assumes you have run a basic graphql wire query, as in the previous chapter. Here you will learn to read the result: how the data is nested, why each field is wrapped in an object, and how to turn that structure into something a template can render.
The shape of a result
A GraphQL response mirrors the query. The graphql adapter delivers an object with a data property that follows your query exactly, starting at uiapi. For a query on Account, the records sit at data.uiapi.query.Account.
That object is not an array of records. It is a connection, a structure with an edges array. Each element of edges is an edge, and each edge holds a node, which is the record. Edges exist so that cursor-based paging can attach extra information to each position in the list. You will use that in the pagination chapter.
{
"data": {
"uiapi": {
"query": {
"Account": {
"edges": [
{
"node": {
"Id": "001xx000003DGb2AAG",
"Name": { "value": "Acme" },
"Industry": { "value": "Technology", "displayValue": "Technology" },
"AnnualRevenue": { "value": 1200000, "displayValue": "$1,200,000" }
}
}
]
}
}
}
},
"errors": undefined
}The block above is illustrative JSON. The undefined is not valid JSON and only shows that the adapter leaves errors unset on success.
Why fields are wrapped
Look at Id and Name in the sample. Id is a plain string. Name is an object with a value property. The difference is deliberate.
In the uiapi schema, Id has a scalar type. Every other field has a wrapper type that matches its data type, such as a string field type, a currency field type, or a date field type. Each wrapper exposes at least value, and most also expose displayValue. You choose which of these to request in the query. If you write Name { value }, the response contains only value.
This means you cannot request a field by name alone. A query like this fails validation:
// Invalid: Name needs a selection of subfields.
Account(first: 5) { edges { node { Id Name } } }You must write Name { value }.
value versus displayValue
The two properties answer different questions.
valueis the raw stored data in its native type: a string, number, boolean, or ISO-formatted date string. Use it for logic, comparisons, sorting in JavaScript, and for sending data back to the server.displayValueis a string formatted for the user. For a picklist it is the label rather than the API value. For currency, numbers, dates, and times it is formatted for the user's locale and currency settings.
| Field type | `value` example | `displayValue` example |
|---|---|---|
| Text (`Name`) | `"Acme"` | usually `null` |
| Picklist (`Industry`) | `"Technology"` | `"Technology"` (the label) |
| Currency (`AnnualRevenue`) | `1200000` | `"$1,200,000"` |
| Date (`CloseDate`) | `"2026-03-15"` | a locale-formatted date |
| Checkbox | `true` | often `null` |
The exact displayValue text depends on the user's locale, time zone, and org settings, so do not compare against it or parse it. Treat it as display-only.
Null values
An empty field does not make the wrapper disappear. If Industry is blank on a record, you still get an Industry object, but value is null:
"Industry": { "value": null, "displayValue": null }So node.Industry.value is safe to read. The wrapper itself can be missing in one situation: the user cannot see the field because of field-level security, or the query returned a partial error. In those cases the field may be null instead of an object, and node.Industry.value throws a TypeError. When you cannot be sure, use optional chaining: node.Industry?.value.
Mapping edges to plain objects
Templates are easier to write when each item is flat. The usual pattern is to map every edge to a plain object in the wire handler or a getter. This component adds a fallback from displayValue to value, and it guards against missing data:
import { LightningElement, wire } from 'lwc';
import { gql, graphql } from 'lightning/uiGraphQLApi';
export default class OpportunityList extends LightningElement {
opportunities = [];
errors;
@wire(graphql, {
query: gql`
query openOpportunities {
uiapi {
query {
Opportunity(first: 10, orderBy: { CloseDate: { order: ASC } }) {
edges {
node {
Id
Name { value }
StageName { value displayValue }
Amount { value displayValue }
CloseDate { value displayValue }
}
}
}
}
}
}
`
})
wiredOpportunities({ data, errors }) {
if (data) {
const edges = data.uiapi.query.Opportunity?.edges ?? [];
this.opportunities = edges.map(({ node }) => ({
id: node.Id,
name: node.Name?.value,
stage: node.StageName?.displayValue ?? node.StageName?.value,
amount: node.Amount?.displayValue ?? '',
amountRaw: node.Amount?.value,
closeDate: node.CloseDate?.displayValue ?? node.CloseDate?.value
}));
this.errors = undefined;
} else if (errors) {
this.errors = errors;
this.opportunities = [];
}
}
get totalAmount() {
return this.opportunities.reduce((sum, o) => sum + (o.amountRaw ?? 0), 0);
}
}Three habits in this code are worth copying:
displayValue ?? valueshows the label when one exists and the raw value otherwise.amountRawkeeps the numericvaluefor arithmetic intotalAmount. Summing the formatted strings would give wrong results.Opportunity?.edges ?? []makes an empty or missing connection produce an empty list instead of an exception.
The template then reads flat properties:
<template>
<lightning-card title="Open opportunities" icon-name="standard:opportunity">
<template lwc:if={errors}>
<p class="slds-p-around_medium">The opportunities could not be loaded.</p>
</template>
<ul class="slds-p-around_medium">
<template for:each={opportunities} for:item="opp">
<li key={opp.id}>{opp.name}: {opp.stage}, {opp.amount}, closes {opp.closeDate}</li>
</template>
</ul>
</lightning-card>
</template>Note the key={opp.id} on the li. The Id from the node is a stable unique key, which is why Id belongs in nearly every query.
Reading the result without destructuring in the handler
The wire function form above receives { data, errors }. If you assign the result to a property instead, the property holds the whole result object, and you read data through it. In that case the data may not have arrived yet, so guard the path with optional chaining:
get names() {
return (
this.result?.data?.uiapi.query.Account.edges.map(({ node }) => node.Name.value) ?? []
);
}The property starts as undefined, and data is undefined until the first response, so every step of the path needs protection. After the first response, data is set.
Common mistakes
- Treating the connection as an array. Calling
data.uiapi.query.Account.map(...)fails becauseAccountis an object. Map over.edges. - Forgetting the
nodelevel.edge.Nameisundefined. The field lives onedge.node. - Reading
displayValueyou did not request. If the query saysName { value }, thenName.displayValueisundefined. The response contains only what you selected. - Rendering
valuefor picklists. You show the API value instead of the label. RequestdisplayValuefor user-facing text. - Mutating the result. Data from the wire is read-only. Build new objects with
map, as shown, rather than assigning tonodeproperties. - Doing math on
displayValue. It is a formatted string. Usevalue.
Checking your understanding
Exercise
A query selects Amount { displayValue } for an Opportunity. A teammate's code reads node.Amount.value * 2 and gets NaN or undefined. What went wrong, and how do you fix it?
Show answer
The query did not request value, so node.Amount.value is undefined, and undefined * 2 is NaN. A response contains only the subfields the query selects. Fix it by selecting Amount { value displayValue } and doing arithmetic on value, while showing displayValue to the user.
Exercise
Write an expression that turns data from a query on Contact (selecting Id, Name { value }, and Email { value }) into an array of { id, name, email } objects. It must return an empty array if the connection has no edges.
Show answer
(data.uiapi.query.Contact?.edges ?? []).map(({ node }) => ({ id: node.Id, name: node.Name?.value, email: node.Email?.value })). The optional chaining on Contact?.edges and the ?? [] fallback give an empty array when there are no edges, and each record is read from node.