[−][src]Derive Macro async_graphql::GQLUnion
#[derive(GQLUnion)] { // Attributes available to this derive: #[graphql] }
Define a GraphQL union
Macro parameters
Attribute | description | Type | Optional |
---|---|---|---|
name | Object name | string | Y |
desc | Object description | string | Y |
Define a union
Define TypeA, TypeB, ... as MyUnion
use async_graphql::*; #[derive(GQLSimpleObject)] struct TypeA { value_a: i32, } #[derive(GQLSimpleObject)] struct TypeB { value_b: i32 } #[derive(GQLUnion)] enum MyUnion { TypeA(TypeA), TypeB(TypeB), } struct QueryRoot; #[GQLObject] impl QueryRoot { async fn all_data(&self) -> Vec<MyUnion> { vec![TypeA { value_a: 10 }.into(), TypeB { value_b: 20 }.into()] } } async_std::task::block_on(async move { 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.into_result().unwrap().data; assert_eq!(res, serde_json::json!({ "allData": [ { "valueA": 10 }, { "valueB": 20 }, ] })); });