Struct async_graphql::dynamic::Union
source · pub struct Union { /* private fields */ }
Available on crate feature
dynamic-schema
only.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§
source§impl Union
impl Union
sourcepub fn description(self, description: impl Into<String>) -> Self
pub fn description(self, description: impl Into<String>) -> Self
Set the description
sourcepub fn inaccessible(self) -> Self
pub fn inaccessible(self) -> Self
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
sourcepub fn possible_type(self, ty: impl Into<String>) -> Self
pub fn possible_type(self, ty: impl Into<String>) -> Self
Add a possible type to the union that must be an object