pub struct Union { /* private fields */ }
Expand description

A GraphQL union type

Examples

use async_graphql::{dynamic::*, value, Value};

let obj_a = Object::new("MyObjA")
    .field(Field::new("a", TypeRef::named_nn(TypeRef::INT), |_| {
        FieldFuture::new(async { Ok(Some(Value::from(100))) })
    }))
    .field(Field::new("b", TypeRef::named_nn(TypeRef::INT), |_| {
        FieldFuture::new(async { Ok(Some(Value::from(200))) })
    }));

let obj_b = Object::new("MyObjB")
    .field(Field::new("c", TypeRef::named_nn(TypeRef::INT), |_| {
        FieldFuture::new(async { Ok(Some(Value::from(300))) })
    }))
    .field(Field::new("d", TypeRef::named_nn(TypeRef::INT), |_| {
        FieldFuture::new(async { Ok(Some(Value::from(400))) })
    }));

let union = Union::new("MyUnion")
    .possible_type(obj_a.type_name())
    .possible_type(obj_b.type_name());

let query = Object::new("Query")
    .field(Field::new("valueA", TypeRef::named_nn(union.type_name()), |_| {
        FieldFuture::new(async {
            Ok(Some(FieldValue::with_type(FieldValue::NULL, "MyObjA")))
        })
    }))
    .field(Field::new("valueB", TypeRef::named_nn(union.type_name()), |_| {
        FieldFuture::new(async {
            Ok(Some(FieldValue::with_type(FieldValue::NULL, "MyObjB")))
        })
    }));


let schema = Schema::build(query.type_name(), None, None)
    .register(obj_a)
    .register(obj_b)
    .register(union)
    .register(query)
    .finish()?;

let query = r#"
    {
        valueA { ... on MyObjA { a b } ... on MyObjB { c d } }
        valueB { ... on MyObjA { a b } ... on MyObjB { c d } }
    }
"#;

assert_eq!(
    schema.execute(query).await.into_result().unwrap().data,
    value!({
        "valueA": {
            "a": 100,
            "b": 200,
        },
        "valueB": {
            "c": 300,
            "d": 400,
        }
    })
);

Implementations§

Create a GraphQL union type

Set the description

Indicate that an enum is not accessible from a supergraph when using Apollo Federation

Reference: https://www.apollographql.com/docs/federation/federated-types/federated-directives/#inaccessible

Arbitrary string metadata that will be propagated to the supergraph when using Apollo Federation. This attribute is repeatable

Reference: https://www.apollographql.com/docs/federation/federated-types/federated-directives/#applying-metadata

Add a possible type to the union that must be an object

Returns the type name

Trait Implementations§

Formats the value using the given formatter. Read more
Converts to this type from the input type.

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

Returns the argument unchanged.

Calls U::from(self).

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

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.