Sub topic 10
GraphQL API for flexible reads
This chapter assumes you can send an authenticated REST request with a bearer token (covered in the authentication and REST chapters). It also assumes you know SOQL relationship queries.
What the GraphQL API is
The Salesforce GraphQL API lets a client describe the exact shape of the data it wants and get that shape back in one response. REST returns a fixed structure per resource. With GraphQL, the client lists the fields and related records it needs, and nothing else comes back.
It solves two common problems with record reads:
- Over-fetching. A REST record response includes every field you are allowed to see, even if you need two.
- Under-fetching. Reading an Account and its Contacts with REST usually takes several calls, or a SOQL subquery you must parse yourself.
One endpoint, one POST
All GraphQL requests go to a single endpoint with the HTTP method POST. The body is JSON with a query string. Authentication is the same OAuth 2.0 bearer token you use for REST.
curl -X POST "https://MyDomain.my.salesforce.com/services/data/v60.0/graphql" \
-H "Authorization: Bearer $ACCESS_TOKEN" \
-H "Content-Type: application/json" \
-d '{"query": "query { uiapi { query { Account(first: 5, where: { Industry: { eq: \"Technology\" } }) { edges { node { Id Name { value } Contacts(first: 3) { edges { node { LastName { value } } } } } } } } } }"}'The query asks for five Technology accounts, each with its name and up to three Contacts, in one round trip.
Reading the response
Responses mirror the query. Two details surprise newcomers:
- Results sit inside
edgesandnode. This is the standard GraphQL connection pattern. - Most fields are objects, so
Namecomes back as{ "value": "Acme Corp" }. The wrapper can also carrydisplayValueandlabel.Idis returned as a plain value.
{
"data": {
"uiapi": {
"query": {
"Account": {
"edges": [
{
"node": {
"Id": "001000000000001AAA",
"Name": { "value": "Acme Corp" },
"Contacts": {
"edges": [{ "node": { "LastName": { "value": "Smith" } } }]
}
}
}
]
}
}
}
},
"errors": []
}Paging and filtering
Each connection takes a first argument for page size. To get the next page, request pageInfo { hasNextPage endCursor } and pass the cursor back as after. Filters use the where argument with operators such as eq, like, and in, and orderBy sorts the results.
Common errors
- Partial success. A GraphQL request can return HTTP 200 and still contain an
errorsarray. Always check it. - Forgetting
.value. Asking forNamewithout a sub-selection is a validation error. - Large pages. Page size and query complexity are capped. Page with cursors instead of raising
first. - Field and object access. The API respects the user's sharing rules and field-level security, so a field you cannot see is missing or errors.
When to choose it
GraphQL fits read-heavy clients, such as web and mobile apps, that need related records in a specific shape. It is not for bulk loads (use Bulk API 2.0) or custom server-side logic (use Apex REST). Newer API versions also support mutations, but plan on REST or Composite for writes unless you have confirmed support in your org's version.
Exercise
A GraphQL request returns HTTP 200, but the screen shows no data for one field. What should you check first, and why?
Show answer
Check the errors array in the response body. GraphQL can return HTTP 200 with partial data, and a field that fails (for example because of field-level security or a missing .value sub-selection) is reported there instead of through the HTTP status.