Skip to main content
Lightning Web Components support GraphQL queries through the UI API GraphQL endpoint. This provides a flexible alternative to REST-based wire adapters with powerful filtering and relationship traversal.

Basic GraphQL Query

Use the graphql 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

Operators:
  • eq: Equal to
  • ne: Not equal to
  • like: Pattern matching (use % for wildcards)
  • gt: Greater than
  • lt: Less than
  • in: In a list of values

Field Aliases

Use aliases for custom fields to ensure referential integrity:
This ensures the field can be found even if Salesforce updates the field name internally. Standard field names never change, so aliases aren’t required for them.

Accessing Query Results

GraphQL results use a connection pattern:

Result Structure

Reactive Variables

Make queries reactive by using a getter function for variables:
The $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

Advantages:
  • Flexible filtering and sorting
  • Query exactly the fields needed
  • Traverse relationships easily
  • Complex where clauses
Best for:
  • 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 performance

Reactive Variables

Use getter functions for reactive variable binding