Sub topic 8
Following relationships to parent and child records
This chapter assumes you can already write a basic @wire(graphql) query and read edges, node, and value. It also assumes you know Salesforce lookup and master-detail relationships.
Two directions, two shapes
A GraphQL query can follow a relationship in either direction, and the two directions return different shapes.
- Parent (child to parent): a lookup or master-detail field on the record you are querying. Use the relationship name, such as
Accounton a Contact. The result is a single object with the parent's fields. There is noedgeswrapper. - Child (parent to children): a list of related records. Use the child relationship name, such as
Contactson an Account. The result is a connection, so it hasedgesandnode, and it acceptsfirst,where, andorderBy.
Querying children
Nest the child relationship inside the parent's node. Each level can have its own arguments.
import { LightningElement, wire } from 'lwc';
import { gql, graphql } from 'lightning/uiGraphQLApi';
export default class AccountContacts extends LightningElement {
accounts = [];
errors;
@wire(graphql, {
query: gql`
query accountsWithContacts {
uiapi {
query {
Account(first: 5, orderBy: { Name: { order: ASC } }) {
edges {
node {
Id
Name { value }
Contacts(first: 3, orderBy: { LastName: { order: ASC } }) {
edges {
node {
Id
Name { value }
}
}
}
}
}
}
}
}
}
`
})
wiredAccounts({ data, errors }) {
if (data) {
this.accounts = data.uiapi.query.Account.edges.map(({ node }) => ({
id: node.Id,
name: node.Name.value,
contacts: node.Contacts.edges.map((edge) => ({
id: edge.node.Id,
name: edge.node.Name.value
}))
}));
this.errors = undefined;
} else if (errors) {
this.errors = errors;
this.accounts = [];
}
}
}The first: 3 on Contacts limits the contacts for each account, not the total. Without it, the query returns a default page size for each child list.
Querying parents
When you query Contacts, ask for the parent by its relationship name and select fields inside it.
const CONTACTS_QUERY = gql`
query contactsWithAccount {
uiapi {
query {
Contact(first: 10) {
edges {
node {
Id
Name { value }
Account {
Name { value }
Industry { value displayValue }
}
}
}
}
}
}
}
`;Read the parent with node.Account?.Name.value. The optional chaining matters, because a Contact with no Account returns null for Account.
Naming rules
| Relationship | Name in the query | Example |
|---|---|---|
| Standard parent | Field name without `Id` | `Account` |
| Custom parent | Ends in `__r` | `Region__r` |
| Standard child | Plural child relationship name | `Contacts` |
| Custom child | Ends in `__r` | `Invoices__r` |
The lookup field itself, such as AccountId, is still available as a plain field if you only need the ID.
Polymorphic parents
Some lookups can point to more than one object. Owner can be a User or a Group. Use an inline fragment to select fields by type.
const OWNER_FIELDS = gql`
query accountOwners {
uiapi {
query {
Account(first: 5) {
edges {
node {
Owner {
... on User { Name { value } }
}
}
}
}
}
}
}
`;Exercise
You query Account and select Contacts(first: 2) { edges { node { Id } } }. A learner writes node.Contacts.map(...) and gets an error. What is wrong, and how do you fix it?
Show answer
A child relationship returns a connection, not an array. Map over node.Contacts.edges and read each contact from edge.node. The first: 2 limit applies to each account's contacts separately.
Exercise
In a Contact query, why should you write node.Account?.Name.value instead of node.Account.Name.value?
Show answer
The parent field is null when the Contact has no Account, or when the user cannot see it. Without optional chaining, reading Name on null throws a TypeError.