Variable
Defines a variable binding for use in resolver fragments.
This is the Java equivalent of Kotlin's @Variable annotation. Variables extract values from the execution environment (arguments, parent object fields, or query fields) and bind them to GraphQL variables used in fragment selection sets.
Usage
Variables are defined in the variables array:
objectValueFragment = """
fragment _ on User {
posts(limit: $postLimit) {
id
title
}
}
""",
variables = {
= "postLimit", fromArgument = "limit")
}
)
Content copied to clipboard
Variable Sources
From Arguments
Extract from the current field's arguments:
= "limit", fromArgument = "maxResults")
Content copied to clipboard
Supports dot-separated paths for nested input object fields:
= "userId", fromArgument = "filters.userId")
Content copied to clipboard
From Object Fields
Extract from parent object selections:
= "authorId", fromObjectField = "author.id")
Content copied to clipboard
From Query Fields
Extract from Query root selections:
= "currentUserId", fromQueryField = "currentUser.id")
Content copied to clipboard
Constraints
- Exactly one of
fromArgument,fromObjectField, orfromQueryFieldmust be specified - Paths must terminate on scalar or enum values (or nullable/list wrappers thereof)
- Paths cannot traverse through list-valued fields
- The type at the path end must be compatible with where the variable is used
- Non-nullable variable locations require non-nullable paths (no nullable traversals)