Sub topic 3
How Salesforce Exposes Objects Under uiapi
This chapter assumes you can build a basic Lightning web component, use @wire with reactive properties, and read a GraphQL query (fields, arguments, nested selections). It explains the shape of the Salesforce GraphQL schema so you can write queries without guessing. The next chapter runs your first query.
Why the schema shape matters
The graphql wire adapter does not accept SOQL. It accepts GraphQL documents that follow the Salesforce GraphQL schema. Every query you write for the adapter starts the same way, and most errors from beginners come from getting the first few levels wrong. If you know how the schema is organized, you can predict what a valid query looks like for any object, standard or custom.
The schema is rooted at a field named uiapi. Everything you read from Salesforce data through this adapter sits beneath it.
The path from root to records
A read query nests four levels before it reaches a record's fields.
uiapiis the root field for Salesforce data.queryis a field underuiapithat holds the queryable objects.- The object API name, such as
Account, is a field underquery. It takes arguments and returns a connection. - The connection contains
edges, and each edge contains anode, which is one record.
query accountNames {
uiapi {
query {
Account(first: 3) {
edges {
node {
Id
Name { value }
}
}
}
}
}
}The names under query are API names. A custom object called Project__c is queried as Project__c, and a custom field is selected as Budget__c. Names are case sensitive in GraphQL, so account is not Account.
Objects are connections
The field named after the object does not return a list of records directly. It returns a connection, which is a structure defined by the GraphQL Cursor Connections pattern. A connection exposes these parts.
| Part | What it holds |
|---|---|
| `edges` | A list of edge objects, one per record in the current page |
| `node` (inside an edge) | The record itself, with the fields you selected |
| `cursor` (inside an edge) | An opaque string that marks the record's position |
| `pageInfo` | Paging data such as `hasNextPage` and `endCursor` |
| `totalCount` | The number of records that match the query, when you select it |
You will use pageInfo and cursors in the pagination chapter. For now, remember that the extra edges and node levels are part of every result. The adapter returns the data in the same shape as the query, so data.uiapi.query.Account.edges mirrors the uiapi { query { Account { edges } } } selection.
Fields are wrapped, not bare
Most fields do not return a plain string or number. A field such as Name returns an object, and you select inside it to get the data. The field type decides which subfields exist.
Name { value }
Industry { value displayValue }
AnnualRevenue { value displayValue }valueis the stored value in raw form. For a picklist it is the API value. For a number it is a number.displayValueis the value formatted for the current user, such as a translated picklist label or a currency string. It is available on fields where formatting applies, and it can benullwhen no formatted version exists.
The field Id is the exception. You select it directly with no subselection, as in Id. Selecting Id { value } is an error, and selecting Name with no subselection is also an error. The schema tells you which form each field takes.
| Field | How to select it |
|---|---|
| `Id` | `Id` |
| `Name` (text) | `Name { value }` |
| `Industry` (picklist) | `Industry { value displayValue }` |
| `AnnualRevenue` (currency) | `AnnualRevenue { value displayValue }` |
| `CreatedDate` (date/time) | `CreatedDate { value displayValue }` |
Field types have their own value types in the schema, for example a string type for text and a double or currency type for numbers. You rarely need these type names in a component, but they appear in the schema documentation and in error messages.
Arguments you pass to an object field
The object field accepts arguments that shape the result set. This chapter only names them. Later chapters use each one.
firstlimits how many records come back in a page.afteris a cursor that says where the next page starts.wherefilters records.orderBysorts records.scopelimits records to a subset such as those the user owns or a team. Availability depends on the object and API version.
Because these arguments sit on the object field, you can query two objects in one document and give each its own arguments.
query accountsAndContacts {
uiapi {
query {
Account(first: 2) {
edges { node { Id Name { value } } }
}
Contact(first: 5) {
edges { node { Id Name { value } } }
}
}
}
}One document, one request, two result sets. Under data.uiapi.query you find Account and Contact side by side.
What the schema does not include
The schema covers objects the current user can access through the User Interface API. A few consequences follow.
- An object that the user cannot read does not appear in results for that user. Access rules are covered in a later chapter.
- You cannot write SOQL clauses such as
GROUP BYorLIMITin the document. The GraphQL equivalents are arguments such asfirst, and the aggregate support covered in the final chapter. - Some objects and fields are not supported by the User Interface API at all. If a field name is valid in Apex but the query reports an unknown field, check whether that object or field is available in the API version your component uses.
The same uiapi root also holds mutation fields for creating, updating, and deleting records. This chapter only reads data. Mutations get a short overview at the end of the tutorial.
Finding what you can query
You do not have to memorize the schema. There are three practical ways to learn it.
- Use a GraphQL client that can load the schema from your org, such as the Salesforce GraphQL tooling or a tool like Altair with your org's endpoint and a session token. Autocomplete then lists the objects under
queryand the fields on each node. - Read the object and field API names in Object Manager, then apply the patterns in this chapter. A standard field keeps its API name, and a custom field ends in
__c. - Run a small query in your component and read the
errorsarray. Error messages name unknown fields and wrong selections, which makes them a quick way to correct a query.
A tested query in a GraphQL client is faster to debug than one that you only run inside a component, because the client shows the full response and the errors together.
Common mistakes
- Skipping levels. Writing
uiapi { Account { ... } }fails becauseAccountlives underquery, not directly underuiapi. - Treating the connection as an array.
data.uiapi.query.Account.map(...)fails. Use.edges.map(({ node }) => ...). - Selecting a wrapped field bare.
Nameneeds{ value }. OnlyIdis selected directly. - Assuming
displayValueis always present. Fall back tovaluewhen it isnull, as the example below does. - Using relationship names or labels instead of API names. Query with the API name, for example
Project__c, and not the label "Project".
A complete read in a component
This component applies the structure above. It selects fields using both wrapper forms and reads them back through the same path.
import { LightningElement, wire } from 'lwc';
import { gql, graphql } from 'lightning/uiGraphQLApi';
export default class AccountSnapshot extends LightningElement {
rows = [];
@wire(graphql, {
query: gql`
query accountSnapshot {
uiapi {
query {
Account(first: 5) {
edges {
node {
Id
Name { value }
Industry { value displayValue }
}
}
}
}
}
}
`
})
wiredSnapshot({ data }) {
if (data) {
this.rows = data.uiapi.query.Account.edges.map(({ node }) => ({
id: node.Id,
name: node.Name.value,
industry: node.Industry.displayValue ?? node.Industry.value
}));
}
}
}Trace the path in the handler: data is the result, uiapi and query mirror the document, Account is the connection, edges is the list, and node is the record. The next chapter explains the gql tag and the wire configuration in detail.
Summary
- Data queries start at
uiapi { query { ObjectApiName { ... } } }. - An object field returns a connection with
edges,node,cursor, andpageInfo. - Every field except
Idis an object that you open with{ value }and, where available,displayValue. - Arguments such as
first,after,where, andorderBygo on the object field. - Several objects can share one query document.
Exercise
A teammate writes data.uiapi.query.Contact.map(c => c.Name.value) and gets an error. Name two things wrong with the code, and write the corrected expression.
Show answer
The first problem is that Contact is a connection, not an array, so .map is not available on it. The records are in edges, and each edge holds a node. The second problem is that c would be an edge, so the field must be read from c.node. The corrected expression is data.uiapi.query.Contact.edges.map(({ node }) => node.Name.value).
Exercise
This query fails validation. Identify the two mistakes and fix them. query { uiapi { Account(first: 3) { edges { node { Id { value } Name } } } } }
Show answer
First, Account must sit under query, so the selection needs to be uiapi { query { Account(...) { ... } } }. Second, the wrapper forms are reversed: Id is selected bare, and Name needs a subselection. The fixed query is query { uiapi { query { Account(first: 3) { edges { node { Id Name { value } } } } } } }.