Variable

annotation class Variable(source)

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")
  }
)

Variable Sources

From Arguments

Extract from the current field's arguments:


= "limit", fromArgument = "maxResults")

Supports dot-separated paths for nested input object fields:


= "userId", fromArgument = "filters.userId")

From Object Fields

Extract from parent object selections:


= "authorId", fromObjectField = "author.id")

From Query Fields

Extract from Query root selections:


= "currentUserId", fromQueryField = "currentUser.id")

Constraints

  • Exactly one of fromArgument, fromObjectField, or fromQueryField must 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)

Functions

Link copied to clipboard
abstract fun fromArgument(): String
Path to a field in the current field's arguments.
Link copied to clipboard
abstract fun fromObjectField(): String
Path to a field in the parent object's selection set.
Link copied to clipboard
abstract fun fromQueryField(): String
Path to a field in the Query root's selection set.
Link copied to clipboard
abstract fun name(): String
The name of the GraphQL variable being defined.