Sub topic 2
Reading Salesforce GraphQL Results in LWC
Assume you know basic LWC classes, templates, @wire, Salesforce objects, and GraphQL fields and arguments.
Reading edges, nodes, value, and displayValue
Salesforce GraphQL returns record collections in a connection. The collection contains edges. Each edge contains a node, which is one record.
Many fields are wrapper objects. Read the stored field value through value. Use displayValue when you want Salesforce's formatted label.
const accounts = data.uiapi.query.Account.edges.map(({ node }) => ({
id: node.Id,
name: node.Name.value,
industry: node.Industry.displayValue ?? node.Industry.value
}));displayValue can be useful for dates, currencies, picklists, and relationship fields. It can be absent, so a fallback to value is often useful.
Using reactive variables
A reactive variable is a component property whose change reruns the wire adapter. Return variables from a getter and reference that getter with $.
pageSize = 10;
get variables() {
return { first: this.pageSize };
}
@wire(graphql, {
query: gql`
query accounts($first: Int) {
uiapi {
query {
Account(first: $first) {
edges {
node {
Id
Name { value }
}
}
}
}
}
}
`,
variables: '$variables'
})
wiredAccounts({ data, errors }) {
// Handle data and errors here.
}When pageSize changes, the wire service runs the query again.
Filtering and sorting records
Use where to filter records and orderBy to sort them.
Account(
where: { Name: { like: "Acme%" } }
orderBy: { Name: { order: ASC } }
)The available operators depend on the field type and Salesforce GraphQL schema.
Following relationships
Select parent fields inside the parent relationship. Select child records through another connection with its own edges and node values.
Account(first: 10) {
edges {
node {
Name { value }
Owner {
Name { value }
}
Contacts(first: 5) {
edges {
node {
FirstName { value }
LastName { value }
}
}
}
}
}
}Exercise
Where do you read an account's name from a GraphQL result?
Show answer
Map through Account.edges, access each edge.node, and read node.Name.value or node.Name.displayValue.