Sub topic 3
How Salesforce Exposes Objects Under uiapi
This chapter assumes you can write a basic Lightning web component, use @wire with reactive parameters, and recognize Salesforce objects, fields, and relationships. It also assumes you know what a GraphQL query, field, and argument are. Here you learn the shape of the Salesforce GraphQL schema, so you can read and write queries before you run them in the next chapter.
One schema, one entry point
Salesforce does not publish a separate GraphQL type for each object at the top level. It places everything under a single root field named uiapi. Inside uiapi, a field named query holds one field per object you can read, such as Account, Contact, or a custom object like Invoice__c.
The path to a record list always follows the same pattern:
uiapiquery- The object API name, for example
Account edgesnode, which holds the record's fields
A minimal query
This query asks for the Id and Name of up to five accounts.
import { gql } from 'lightning/uiGraphQLApi';
export const ACCOUNT_QUERY = gql`
query firstAccounts {
uiapi {
query {
Account(first: 5) {
edges {
node {
Id
Name { value }
}
}
}
}
}
}
`;The object field, Account, accepts arguments. You will use first here, and later where, orderBy, and after. The result is a connection: a wrapper that holds a list of edges. Each edge has a node, and the node is the record.
Field types and the value wrapper
Most fields on a node are not plain values. Salesforce returns them as small objects, so the query must select a sub-field. The two you will see most often are value and displayValue.
| Field | What you select | What you get |
|---|---|---|
| `Id` | Nothing more | The record Id as a string |
| `Name` | `Name { value }` | The stored text |
| `Industry` | `Industry { value displayValue }` | The API value and the label shown to the user |
| `CreatedDate` | `CreatedDate { value displayValue }` | A timestamp and a formatted version |
Id is the exception. It is returned directly, so you write Id and not Id { value }. Selecting a sub-field on Id is an error, as is leaving value off a field that needs it.
Reading these wrappers from results is covered in a later chapter. For now, remember that the shape of the response mirrors the shape of the query, so each wrapper you ask for appears in the data.
Objects you can reach
A query can only name objects and fields that your user can see. If an object does not exist in the org, or the user has no access to it, the field is missing from the schema for that user, and a query that names it fails. The schema is the same for every org only in its structure. The list of objects and fields depends on the org's metadata and the user's permissions.
Custom objects and custom fields use their full API names, including the __c suffix.
import { gql } from 'lightning/uiGraphQLApi';
export const INVOICE_QUERY = gql`
query invoices {
uiapi {
query {
Invoice__c(first: 3) {
edges {
node {
Id
Amount__c { value }
}
}
}
}
}
}
`;Here Invoice__c and Amount__c stand in for any custom object and field in your org. Replace them with real API names before you run the query.
Common mistakes
- Skipping
uiapiorquery. A query that starts atAccountfails. Both levels are required. - Forgetting
edgesandnode. Fields sit onnode, not on the object field itself. - Wrong API name. Use
Invoice__c, not the label "Invoice". - Selecting
valueonId. SelectIdalone.
Exercise
You write Account(first: 5) { edges { node { Id Name } } } and the query is rejected. What is wrong, and how do you fix it?
Show answer
Name is a wrapped field, so it needs a sub-field selection. Change it to Name { value }. Id can stay as it is, because it is returned directly.