Basic GraphQL Query
Use thegraphql wire adapter with gql template literals to query Salesforce data.
Query Structure
All GraphQL queries follow this structure:Key Components
- uiapi: The UI API GraphQL endpoint
- query: The query operation
- ObjectName: The Salesforce object (e.g., Contact, Account)
- edges: Connection pattern for paginated results
- node: Individual record in the result set
- value: The actual field value
GraphQL with Variables
Use variables to create dynamic, reactive queries.Variable Types
- String: Text values
- Int: Integer numbers
- Boolean: true/false values
- ! suffix: Required variable (e.g.,
$searchKey: String!) - Default values: Use
=(e.g.,$limit: Int = 5)
Filtering and Sorting
- Where Clause
- OrderBy
- Pagination
eq: Equal tone: Not equal tolike: Pattern matching (use%for wildcards)gt: Greater thanlt: Less thanin: In a list of values
Field Aliases
Use aliases for custom fields to ensure referential integrity:Accessing Query Results
GraphQL results use a connection pattern:Result Structure
Reactive Variables
Make queries reactive by using a getter function for variables:$variables syntax tells LWC to treat the getter as reactive. When any property referenced in the getter changes, the query automatically re-executes.
GraphQL vs Wire Adapters
- GraphQL
- Wire Adapter
- Flexible filtering and sorting
- Query exactly the fields needed
- Traverse relationships easily
- Complex where clauses
- Complex queries with filtering
- Related record queries
- Variable field selection
Best Practices
Use Aliases for Custom Fields
Always alias custom fields to prevent breaking changes
Debounce User Input
Debounce search inputs to avoid excessive queries
Limit Results
Use
first parameter to limit query size and improve performanceReactive Variables
Use getter functions for reactive variable binding
