pub struct FieldValue<'a>(/* private fields */);
dynamic-schema
only.Expand description
A value returned from the resolver function
Implementations§
source§impl<'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 owned_any<T: Any + Send + Sync>(obj: T) -> Self
pub fn owned_any<T: Any + Send + Sync>(obj: T) -> Self
Create a FieldValue from owned any value
sourcepub fn boxed_any<T: Any + Send + Sync>(obj: Box<T>) -> Self
pub fn boxed_any<T: Any + Send + Sync>(obj: Box<T>) -> Self
Create a FieldValue from unsized any value
sourcepub fn borrowed_any<T: Any + Send + Sync>(obj: &'a T) -> Self
pub fn borrowed_any<T: Any + Send + Sync>(obj: &'a T) -> Self
Create a FieldValue from owned any value
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
.
Trait Implementations§
source§impl<'a> Debug for FieldValue<'a>
impl<'a> Debug for FieldValue<'a>
source§impl<'a> From<()> for FieldValue<'a>
impl<'a> From<()> for FieldValue<'a>
source§impl<'a> From<ConstValue> for FieldValue<'a>
impl<'a> From<ConstValue> for FieldValue<'a>
Auto Trait Implementations§
impl<'a> !Freeze for FieldValue<'a>
impl<'a> !RefUnwindSafe for FieldValue<'a>
impl<'a> Send for FieldValue<'a>
impl<'a> Sync for FieldValue<'a>
impl<'a> Unpin for FieldValue<'a>
impl<'a> !UnwindSafe for FieldValue<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> FmtForward for T
impl<T> FmtForward for T
source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.source§impl<T> FutureExt for T
impl<T> FutureExt for T
source§fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
fn with_context(self, otel_cx: Context) -> WithContext<Self> ⓘ
source§fn with_current_context(self) -> WithContext<Self> ⓘ
fn with_current_context(self) -> WithContext<Self> ⓘ
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moresource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.source§impl<T> Tap for T
impl<T> Tap for T
source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moresource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moresource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moresource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moresource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moresource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.