Sub topic 2
GraphQL essentials you need for Salesforce
This chapter covers the GraphQL ideas you need before writing Salesforce queries. It assumes you can read a Lightning web component (a JavaScript class plus an HTML template). It also assumes you know how @wire and reactive properties work, and that you know Salesforce objects, fields, and relationships. You don't need prior GraphQL experience, but you should know what a query is.
What GraphQL gives you
GraphQL is a query language for APIs. The client sends one request that describes the exact fields it wants. The server returns JSON in the same shape as the request.
Two consequences matter for Lightning web components:
- You never receive fields you didn't ask for, so responses stay small.
- You can read several related things in one request instead of making several calls.
The building blocks
A query has four parts you will see in every example in this tutorial.
| Part | What it does | Example |
|---|---|---|
| Operation | Names the kind of request and, optionally, a label | `query accountList` |
| Field | A piece of data you want back | `Name` |
| Selection set | Braces that list the fields to return for an object | `{ Id Name }` |
| Argument | A value in parentheses that changes what a field returns | `Account(first: 5)` |
Fields can nest. A field that returns an object needs its own selection set, and that set can contain more objects. This nesting is how a query walks from one record to its related data.
Variables
Hard-coding values in a query string works, but it ties the query to one answer. A variable lets you declare an input once and supply its value separately.
query accountList($first: Int) {
uiapi {
query {
Account(first: $first) {
edges {
node {
Id
Name { value }
}
}
}
}
}
}Here $first is a variable of type Int. The declaration sits after the query name. The query uses $first as the value of the first argument. A caller then passes { "first": 10 } alongside the query.
In a component, this separation is what makes queries reactive. You change the variables, and the wire reruns the same query with new values. A later chapter covers that in detail.
Types and required values
GraphQL is strongly typed. Each variable declares a type such as Int, String, or Boolean. A type with ! after it, such as String!, is required. A type without it is optional and may be null.
query pagedAccounts($first: Int, $after: String) {
uiapi {
query {
Account(first: $first, after: $after) {
edges { node { Id } }
}
}
}
}Both variables here are optional. On a first page, after can be null.
Reading and changing data
GraphQL has two main operation types:
- A query reads data. It does not change anything.
- A mutation writes data.
This tutorial focuses on queries. The final chapter gives an overview of mutations.
How it maps onto Salesforce
Salesforce exposes its data through a GraphQL schema that starts at a root field named uiapi. Under it, query holds one field per object you can read, such as Account or Contact. You select an object, then its edges, then each node, then the fields you want.
The diagram shows the path a single field takes from the root.
The next chapter walks through each step of this path.
Common mistakes
- Forgetting the selection set. Writing
Namealone fails for Salesforce fields, because each field returns an object. Ask forName { value }. - Mismatched variable types. Declaring
$first: Stringand using it where anIntis expected produces a validation error before any data is read. - Missing declaration. Using
$afterin a field without declaring it in the operation line is an error. - Treating the response as flat. Results follow the query shape, so you must walk down through
uiapi,query, the object, andedges.
Exercise
A query declares query recent($count: Int) but the Account field uses Account(first: $limit). What goes wrong, and how do you fix it?
Show answer
The query uses $limit, which was never declared, so it fails validation before running. Either rename the declaration to $limit: Int or change the argument to first: $count. The names must match.
Exercise
Why does Name need to be written as Name { value } in a Salesforce GraphQL query?
Show answer
Each Salesforce field in the uiapi schema returns an object, not a plain value. The selection set picks the parts of that object you want, such as value.