Sub topic 12
Security behavior and limits to plan for
This chapter assumes you can write a graphql wire with variables and read edges, node, and the errors array, as covered in earlier chapters. It explains what Salesforce enforces for you and which limits can break a query that looks correct.
The query runs as the current user
The graphql wire adapter calls Salesforce's UI API on behalf of the logged-in user. You don't add security checks in the component. The platform applies these rules to every query:
- Object permissions: the user needs read access to each object in the query.
- Sharing: only records the user can see are returned. Two users running the same query can get different edges.
- Field-level security (FLS): fields the user can't read are not returned.
This differs from Apex, where without sharing or system-mode code can return more than the user should see. A wired GraphQL query has no such mode.
Design for differing results
Because results depend on the user, write handlers that don't assume a field or record is present.
- Don't assume a record you saw in testing as an admin will appear for a standard user.
- Test with a profile or permission set that has minimal access.
- Treat a missing field value as a normal case. Use optional chaining and fallbacks, as the earlier
displayValue ?? valueexample does.
When a field is inaccessible, expect an entry in errors. Depending on the failure, data may still hold the parts that succeeded. Check both, as the earlier error-handling chapter showed.
import { LightningElement, wire } from 'lwc';
import { gql, graphql } from 'lightning/uiGraphQLApi';
export default class SecureAccountList extends LightningElement {
accounts = [];
errorMessages = [];
@wire(graphql, {
query: gql`
query secureAccounts($first: Int) {
uiapi {
query {
Account(first: $first) {
edges {
node {
Id
Name { value }
AnnualRevenue { value }
}
}
}
}
}
}
`,
variables: '$variables'
})
wiredAccounts({ data, errors }) {
this.errorMessages = errors ? errors.map((error) => error.message) : [];
const edges = data?.uiapi?.query?.Account?.edges ?? [];
this.accounts = edges.map(({ node }) => ({
id: node.Id,
name: node.Name?.value,
revenue: node.AnnualRevenue?.value ?? null
}));
}
get variables() {
return { first: 10 };
}
}The handler reads errors and data independently. A user without access to AnnualRevenue still gets a working list, and the component can show the messages if it wants to.
Limits to plan for
The GraphQL API caps query size and shape, and it counts against API usage. These are the planning points:
| Concern | What to plan for |
|---|---|
| Page size | `first` has an upper bound (200 at the time of writing). Page with `after` and `pageInfo` for larger sets. |
| Query depth and breadth | Deeply nested relationships and many child queries are capped. Request only what the view shows. |
| Query cost | Broad filters, many fields, and large pages cost more. Narrow with `where` and trim fields. |
| Supported objects | Only objects and fields exposed through UI API appear under `uiapi`. |
Exact numbers change between releases. Check the current GraphQL API limits in the Salesforce developer documentation before you finalize a design.
A query that exceeds a limit returns an entry in errors instead of data. Keep queries small so you don't depend on edge-of-limit behavior.
Client-side data is not a security boundary
Wired results are cached in the browser, and anything the user's browser receives is visible to that user. Use FLS and sharing to control what is returned. Don't fetch a field and hide it in the template.
Exercise
A component's GraphQL query includes AnnualRevenue. A user without field-level access to that field loads the page. What should the wire handler expect, and how should the component respond?
Show answer
The field is not returned to that user, and errors will typically contain an entry about it. The handler should read errors and data separately, use optional chaining or fallbacks for the missing value, and still render the rest of the data. The component must not assume the field is present.