OptionalVariable

@Serializable(with = OptionalVariable.Serializer)
interfaceOptionalVariable<T : Any?>

Known direct subclasses
OptionalVariable.Undefined

An implementation ofOptionalVariable representing an "undefined" value.

OptionalVariable.Value

An implementation ofOptionalVariable representing a "defined" value.


An optional variable to a query or a mutation.

The typical use case of this class is as a property of a class used as the variables of a query or mutation (OperationRef.variables). This allows omitting a variable altogether from the request, in the case ofOptionalVariable.Undefined, allowing the variable to take on its default value as defined in the GraphQL schema or operation, or an explicit value in the case ofOptionalVariable.Value, which may benull if the type parameter is nullable.

Here is an example of such a variables class:

@Serializable
data
classUpdatePersonVariables(
valkey:PersonKey,
valname:OptionalVariable<String>,
valage:OptionalVariable<Int?>,
)

with this "variables" class, to clear a person's age but not modify their name, the instance could be created as follows

valvariables=UpdatePersonVariables(
key=key,
name=OptionalVariable.Undefined,
age=OptionalVariable.Value(42),
)

Summary

Nested types

TheKSerializer implementation forOptionalVariable.

An implementation ofOptionalVariable representing an "undefined" value.

An implementation ofOptionalVariable representing a "defined" value.

Public functions

T?

Returns the value encapsulated by this object if the runtime type isValue, ornull if this object isUndefined.

T

Returns the value encapsulated by this object if the runtime type isValue, or throws an exception if this object isUndefined.

Public functions

valueOrNull

fun valueOrNull(): T?

Returns the value encapsulated by this object if the runtime type isValue, ornull if this object isUndefined.

valueOrThrow

fun valueOrThrow(): T

Returns the value encapsulated by this object if the runtime type isValue, or throws an exception if this object isUndefined.

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-09-18 UTC.