pub enum MaybeUndefined<T> {
    Undefined,
    Null,
    Value(T),
}
Expand description

Similar to Option, but it has three states, undefined, null and x.

Reference: https://spec.graphql.org/October2021/#sec-Null-Value

Examples

use async_graphql::*;

struct Query;

#[Object]
impl Query {
    async fn value1(&self, input: MaybeUndefined<i32>) -> i32 {
        if input.is_null() {
            1
        } else if input.is_undefined() {
            2
        } else {
            input.take().unwrap()
        }
    }
}

let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
let query = r#"
    {
        v1:value1(input: 99)
        v2:value1(input: null)
        v3:value1
    }"#;
assert_eq!(
    schema.execute(query).await.into_result().unwrap().data,
    value!({
        "v1": 99,
        "v2": 1,
        "v3": 2,
    })
);

Variants

Undefined

Null

Value(T)

Implementations

Returns true if the MaybeUndefined<T> is undefined.

Returns true if the MaybeUndefined<T> is null.

Returns true if the MaybeUndefined<T> contains value.

Borrow the value, returns None if the the MaybeUndefined<T> is undefined or null, otherwise returns Some(T).

Converts the MaybeUndefined<T> to Option<T>.

Converts the MaybeUndefined<T> to Option<Option<T>>.

Converts the MaybeUndefined<T> to Option<Option<&U>>.

Returns true if the MaybeUndefined<T> contains the given value.

Returns true if the MaybeUndefined<T> contains the given nullable value.

Maps a MaybeUndefined<T> to MaybeUndefined<U> by applying a function to the contained nullable value

Maps a MaybeUndefined<T> to MaybeUndefined<U> by applying a function to the contained value

Update value if the MaybeUndefined<T> is not undefined.

Example
use async_graphql::MaybeUndefined;

let mut value = None;

MaybeUndefined::Value(10i32).update_to(&mut value);
assert_eq!(value, Some(10));

MaybeUndefined::Undefined.update_to(&mut value);
assert_eq!(value, Some(10));

MaybeUndefined::Null.update_to(&mut value);
assert_eq!(value, None);

Transposes a MaybeUndefined of a Result into a Result of a MaybeUndefined.

MaybeUndefined::Undefined will be mapped to Ok(MaybeUndefined::Undefined). MaybeUndefined::Null will be mapped to Ok(MaybeUndefined::Null). MaybeUndefined::Value(Ok(_)) and MaybeUndefined::Value(Err(_)) will be mapped to Ok(MaybeUndefined::Value(_)) and Err(_).

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Deserialize this value from the given Serde deserializer. Read more

Converts to this type from the input type.

Converts to this type from the input type.

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The raw type used for validator. Read more

Type the name.

Qualified typename.

Create type information in the registry and return qualified typename.

Parse from Value. None represents undefined.

Convert to a Value for introspection.

Returns a reference to the raw value.

This method returns an Ordering between self and other. Read more

Compares and returns the maximum of two values. Read more

Compares and returns the minimum of two values. Read more

Restrict a value to a certain interval. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

This method returns an ordering between self and other values if one exists. Read more

This method tests less than (for self and other) and is used by the < operator. Read more

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more

This method tests greater than (for self and other) and is used by the > operator. Read more

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Compare self to key and return true if they are equal.

Returns the argument unchanged.

Attaches the provided Context to this type, returning a WithContext wrapper. Read more

Attaches the current Context to this type, returning a WithContext wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Should always be Self

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more