OptionalVariable

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

Known direct subclasses
OptionalVariable.Undefined

An implementation of OptionalVariable representing an "undefined" value.

OptionalVariable.Value

An implementation of OptionalVariable 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 of OptionalVariable.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 of OptionalVariable.Value, which may be null if the type parameter is nullable.

Here is an example of such a variables class:

@Serializable
data
class UpdatePersonVariables(
val key: PersonKey,
val name: OptionalVariable<String>,
val age: OptionalVariable<Int?>,
)

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

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

Summary

Nested types

The KSerializer implementation for OptionalVariable.

An implementation of OptionalVariable representing an "undefined" value.

An implementation of OptionalVariable representing a "defined" value.

Public functions

T?

Returns the value encapsulated by this object if the runtime type is Value, or null if this object is Undefined.

T

Returns the value encapsulated by this object if the runtime type is Value, or throws an exception if this object is Undefined.

Public functions

valueOrNull

fun valueOrNull(): T?

Returns the value encapsulated by this object if the runtime type is Value, or null if this object is Undefined.

valueOrThrow

fun valueOrThrow(): T

Returns the value encapsulated by this object if the runtime type is Value, or throws an exception if this object is Undefined.