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::INT, |_| {
FieldFuture::new(async { Ok(Some(Value::from(100))) })
}))
.field(Field::new("b", TypeRef::INT, |_| {
FieldFuture::new(async { Ok(Some(Value::from(200))) })
}));
let obj_b = Object::new("MyObjB")
.field(Field::new("c", TypeRef::INT, |_| {
FieldFuture::new(async { Ok(Some(Value::from(300))) })
}))
.field(Field::new("d", 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", union.type_ref(), |_| {
FieldFuture::new(async {
Ok(Some(FieldValue::with_type(FieldValue::NULL, "MyObjA")))
})
}))
.field(Field::new("valueB", union.type_ref(), |_| {
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
sourceimpl 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 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
sourcepub fn type_ref(&self) -> NamedTypeRefBuilder
pub fn type_ref(&self) -> NamedTypeRefBuilder
Returns the type reference
Trait Implementations
Auto Trait Implementations
impl RefUnwindSafe for Union
impl Send for Union
impl Sync for Union
impl Unpin for Union
impl UnwindSafe for Union
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more