Variables allow you to parameterize fragments and operations. soda-gql uses standard GraphQL variable syntax in tagged templates and a template literal approach in the options-object path.
Standard GraphQL uses this syntax for variables:
soda-gql provides two approaches depending on the syntax you use:
| Aspect | GraphQL | Tagged Template | Options-Object Path |
|---|---|---|---|
| Syntax | $name: Type! | ($name: Type!) (standard GraphQL) | variables: `($name: Type!)` |
| Required | Type! | Type! | Type! |
| Optional | Type (no suffix) | Type (no suffix) | Type (no suffix) |
| Lists | [Type!]! | [Type!]! | [Type!]! |
| Declaration | In operation header | In template: `($id: ID!) { ... }` | variables: `($id: ID!)` |
| Type Safety | External codegen | Build-time validation | Build-time validation |
When using tagged template syntax, variables are declared using standard GraphQL syntax directly in the template. The options-object path uses the same syntax in a variables template literal. See the Tagged Template Syntax Guide for details.
In tagged templates, declare variables using standard GraphQL syntax at the start of the template:
This is the recommended approach for most operations. The variable syntax is identical to standard GraphQL.
When using the options-object path (for aliases, directives, $dir, $colocate, or programmatic field control), declare variables with a template literal:
Variables use standard GraphQL type notation:
| Declaration | Meaning | GraphQL Equivalent |
|---|---|---|
$id: ID! | Required ID | ID! |
$id: ID | Optional ID | ID |
$name: String! | Required String | String! |
$name: String | Optional String | String |
$count: Int! | Required Int | Int! |
$score: Float | Optional Float | Float |
$active: Boolean! | Required Boolean | Boolean! |
| Declaration | Meaning | GraphQL Equivalent |
|---|---|---|
$tags: [String!]! | Required list of required strings | [String!]! |
$tags: [String!] | Optional list of required strings | [String!] |
$tags: [String]! | Required list of optional strings | [String]! |
$tags: [String] | Optional list of optional strings | [String] |
Use your schema's input types and custom scalars:
Reference declared variables using $ in the options-object path:
In tagged templates, use standard GraphQL $varName syntax directly:
Pass variables to fragments using callback interpolation:
Pass variables directly through the .spread() method:
When a fragment has variables, you must pass values for them. These can be:
{ includeEmail: true }{ includeEmail: $.includeEmail }The $var utility is available in metadata callbacks for inspecting variable references. Methods like $var.getName(), $var.getValue(), and $var.getInner() allow you to extract information from VarRef values. See the Metadata guide for details.
soda-gql recognizes these standard GraphQL scalars:
| Type | TypeScript Type | Description |
|---|---|---|
ID | string | Unique identifier |
String | string | UTF-8 string |
Int | number | 32-bit signed integer |
Float | number | Double-precision float |
Boolean | boolean | true/false |
Custom scalars are defined in your project's inject file:
Variable types are fully inferred:
When calling the query, TypeScript enforces correct variable types: