Struct async_graphql::dynamic::FieldValue
source · pub struct FieldValue<'a>(_);
dynamic-schema
only.Expand description
A value returned from the resolver function
Implementations
sourceimpl<'a> FieldValue<'a>
impl<'a> FieldValue<'a>
sourcepub const NULL: FieldValue<'a> = _
pub const NULL: FieldValue<'a> = _
A null value equivalent to FieldValue::Value(Value::Null)
sourcepub const NONE: Option<FieldValue<'a>> = None
pub const NONE: Option<FieldValue<'a>> = None
A none value equivalent to None::<FieldValue>
It is more convenient to use when your resolver needs to return None
.
Examples
use async_graphql::dynamic::*;
let query =
Object::new("Query").field(Field::new("value", TypeRef::named(TypeRef::INT), |ctx| {
FieldFuture::new(async move { Ok(FieldValue::NONE) })
}));
sourcepub const fn none() -> Option<FieldValue<'a>>
pub const fn none() -> Option<FieldValue<'a>>
Returns a None::<FieldValue>
meaning the resolver no results.
sourcepub fn borrowed_any(obj: &'a impl Any + Send + Sync) -> Self
pub fn borrowed_any(obj: &'a impl Any + Send + Sync) -> Self
Create a FieldValue from owned any value
sourcepub fn list<I, T>(values: I) -> Selfwhere
I: IntoIterator<Item = T>,
T: Into<FieldValue<'a>>,
pub fn list<I, T>(values: I) -> Selfwhere
I: IntoIterator<Item = T>,
T: Into<FieldValue<'a>>,
Create a FieldValue from list
sourcepub fn with_type(self, ty: impl Into<Cow<'static, str>>) -> Self
pub fn with_type(self, ty: impl Into<Cow<'static, str>>) -> Self
Create a FieldValue and specify its type, which must be an object
NOTE: Fields of type Interface
or Union
must return
FieldValue::WithType
.
Examples
use async_graphql::{dynamic::*, value, Value};
struct MyObjData {
a: i32,
}
let my_obj = Object::new("MyObj").field(Field::new(
"a",
TypeRef::named_nn(TypeRef::INT),
|ctx| FieldFuture::new(async move {
let data = ctx.parent_value.try_downcast_ref::<MyObjData>()?;
Ok(Some(Value::from(data.a)))
}),
));
let my_union = Union::new("MyUnion").possible_type(my_obj.type_name());
let query = Object::new("Query").field(Field::new(
"obj",
TypeRef::named_nn(my_union.type_name()),
|_| FieldFuture::new(async move {
Ok(Some(FieldValue::owned_any(MyObjData { a: 10 }).with_type("MyObj")))
}),
));
let schema = Schema::build("Query", None, None)
.register(my_obj)
.register(my_union)
.register(query)
.finish()
.unwrap();
assert_eq!(
schema
.execute("{ obj { ... on MyObj { a } } }")
.await
.into_result()
.unwrap()
.data,
value!({ "obj": { "a": 10 } })
);
sourcepub fn as_value(&self) -> Option<&Value>
pub fn as_value(&self) -> Option<&Value>
If the FieldValue is a value, returns the associated
Value. Returns None
otherwise.
sourcepub fn try_to_value(&self) -> Result<&Value>
pub fn try_to_value(&self) -> Result<&Value>
Like as_value
, but returns Result
.
sourcepub fn as_list(&self) -> Option<&[FieldValue<'_>]>
pub fn as_list(&self) -> Option<&[FieldValue<'_>]>
If the FieldValue is a list, returns the associated
vector. Returns None
otherwise.
sourcepub fn try_to_list(&self) -> Result<&[FieldValue<'_>]>
pub fn try_to_list(&self) -> Result<&[FieldValue<'_>]>
Like as_list
, but returns Result
.
sourcepub fn downcast_ref<T: Any>(&self) -> Option<&T>
pub fn downcast_ref<T: Any>(&self) -> Option<&T>
If the FieldValue is a any, returns the associated
vector. Returns None
otherwise.
sourcepub fn try_downcast_ref<T: Any>(&self) -> Result<&T>
pub fn try_downcast_ref<T: Any>(&self) -> Result<&T>
Like downcast_ref
, but returns Result
.