Enum async_graphql::dynamic::FieldValue
source · pub enum FieldValue<'a> {
Value(Value),
BorrowedAny(&'a (dyn Any + Send + Sync)),
OwnedAny(Box<dyn Any + Send + Sync>),
List(Vec<FieldValue<'a>>),
WithType {
value: Box<FieldValue<'a>>,
ty: Cow<'static, str>,
},
}dynamic-schema only.Expand description
A value returned from the resolver function
Variants
Value(Value)
Const value
BorrowedAny(&'a (dyn Any + Send + Sync))
Borrowed any value
OwnedAny(Box<dyn Any + Send + Sync>)
Owned any value
List(Vec<FieldValue<'a>>)
A list
WithType
A typed Field value
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::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(
value: impl Into<FieldValue<'a>>,
ty: impl Into<Cow<'static, str>>
) -> Self
pub fn with_type(
value: impl Into<FieldValue<'a>>,
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::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",
my_union.type_ref(),
|_| FieldFuture::new(async move {
Ok(Some(FieldValue::with_type(
FieldValue::owned_any(MyObjData { a: 10 }),
"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 FieldValue::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 FieldValue::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 [FieldValue::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.