OptionalVariable
@Serializable(with = OptionalVariable.Serializer)
interface OptionalVariable<T : Any?>
OptionalVariable.Undefined |
An implementation of |
OptionalVariable.Value |
An implementation of |
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 |
|---|
class OptionalVariable.Serializer<T : Any?> : KSerializerThe |
|
An implementation of |
class OptionalVariable.Value<T : Any?> : OptionalVariableAn implementation of |
Public functions |
|
|---|---|
T? |
Returns the value encapsulated by this object if the runtime type is |
T |
Returns the value encapsulated by this object if the runtime type is |
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.