[][src]Attribute Macro async_graphql::Union

#[Union]

Define a GraphQL union

Macro parameters

AttributedescriptionTypeOptional
nameObject namestringY
descObject descriptionstringY

Define a union

Define TypeA, TypeB, ... as MyUnion

use async_graphql::*;

#[SimpleObject]
struct TypeA {
    value_a: i32,
}

#[SimpleObject]
struct TypeB {
    value_b: i32
}

#[Union]
enum MyUnion {
    TypeA(TypeA),
    TypeB(TypeB),
}

struct QueryRoot;

#[Object]
impl QueryRoot {
    async fn all_data(&self) -> Vec<MyUnion> {
        vec![TypeA { value_a: 10 }.into(), TypeB { value_b: 20 }.into()]
    }
}

#[async_std::main]
async fn main() {
    let schema = Schema::build(QueryRoot, EmptyMutation, EmptySubscription).data("hello".to_string()).finish();
    let res = schema.execute(r#"
    {
        allData {
            ... on TypeA {
                valueA
            }
            ... on TypeB {
                valueB
            }
        }
    }"#).await.unwrap().data;
    assert_eq!(res, serde_json::json!({
        "allData": [
            { "valueA": 10 },
            { "valueB": 20 },
        ]
    }));
}