Struct async_graphql::dynamic::InterfaceField
source · pub struct InterfaceField { /* private fields */ }
dynamic-schema
only.Expand description
A GraphQL interface field type
Examples
use async_graphql::{dynamic::*, value, Value};
let obj_a = Object::new("MyObjA")
.implement("MyInterface")
.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")
.implement("MyInterface")
.field(Field::new("a", TypeRef::named_nn(TypeRef::INT), |_| {
FieldFuture::new(async { Ok(Some(Value::from(300))) })
}))
.field(Field::new("c", TypeRef::named_nn(TypeRef::INT), |_| {
FieldFuture::new(async { Ok(Some(Value::from(400))) })
}));
let interface = Interface::new("MyInterface").field(InterfaceField::new("a", TypeRef::named_nn(TypeRef::INT)));
let query = Object::new("Query")
.field(Field::new("valueA", TypeRef::named_nn(interface.type_name()), |_| {
FieldFuture::new(async {
Ok(Some(FieldValue::with_type(FieldValue::NULL, "MyObjA")))
})
}))
.field(Field::new("valueB", TypeRef::named_nn(interface.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(interface)
.register(query)
.finish()?;
let query = r#"
fragment A on MyObjA { b }
fragment B on MyObjB { c }
{
valueA { a ...A ...B }
valueB { a ...A ...B }
}
"#;
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
"valueA": {
"a": 100,
"b": 200,
},
"valueB": {
"a": 300,
"c": 400,
}
})
);
Implementations§
source§impl InterfaceField
impl InterfaceField
sourcepub fn new(name: impl Into<String>, ty: impl Into<TypeRef>) -> Self
pub fn new(name: impl Into<String>, ty: impl Into<TypeRef>) -> Self
Create a GraphQL interface field type
sourcepub fn description(self, description: impl Into<String>) -> Self
pub fn description(self, description: impl Into<String>) -> Self
Set the description
sourcepub fn deprecation(self, reason: Option<&str>) -> Self
pub fn deprecation(self, reason: Option<&str>) -> Self
Set the description
sourcepub fn external(self) -> Self
pub fn external(self) -> Self
Mark a field as owned by another service. This allows service A to use fields from service B while also knowing at runtime the types of that field.
sourcepub fn requires(self, fields: impl Into<String>) -> Self
pub fn requires(self, fields: impl Into<String>) -> Self
Annotate the required input fieldset from a base type for a resolver. It is used to develop a query plan where the required fields may not be needed by the client, but the service may need additional information from other services.
sourcepub fn provides(self, fields: impl Into<String>) -> Self
pub fn provides(self, fields: impl Into<String>) -> Self
Annotate the expected returned fieldset from a field on a base type that is guaranteed to be selectable by the gateway.
Indicate that an object type’s field is allowed to be resolved by multiple subgraphs
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 override_from(self, name: impl Into<String>) -> Self
pub fn override_from(self, name: impl Into<String>) -> Self
Indicate that an object type’s field is allowed to be resolved by multiple subgraphs
sourcepub fn argument(self, input_value: InputValue) -> Self
pub fn argument(self, input_value: InputValue) -> Self
Add an argument to the field