Overview
ThegraphqlVariables component demonstrates how to use variables in GraphQL queries with the @wire decorator. It implements a search feature with reactive variables that trigger query re-execution.
Source
force-app/main/default/lwc/graphqlVariables
Key Features
- GraphQL query with variables
- Reactive variable binding using
$variables - Getter function for reactive updates
- Debounced input handling
- Default variable values
GraphQL Query with Variables
Variable Declaration
Declare variables in the query signature with types:$searchKey: String!: Required string variable$limit: Int = 5: Optional integer with default value
Reactive Variables
Use a getter function to make variables reactive. LWC will re-run this function and re-evaluate the GraphQL query when referenced component properties change:Why Use a Getter?
GraphQL variable values are nested within an object. A getter function is required to ensure the variables are reactive and the query re-executes when component properties change.Wire Adapter Configuration
Bind the variables using the$variables syntax:
$ prefix tells the wire adapter to treat variables as a reactive property.
Debouncing Input
Avoid excessive query executions by debouncing user input:searchKey, which triggers the query.
Using Variables in Queries
Reference variables with the$ prefix:
Accessing Data
Access the wire adapter result directly in the template:Pattern: Wildcard Search
Wrap the search term with% for partial matching:
