Implements InputType
for Box<T>
and Arc<T>
. #382
This commit is contained in:
parent
5a29f74062
commit
099415fd42
116
src/base.rs
116
src/base.rs
|
@ -1,11 +1,13 @@
|
|||
use std::borrow::Cow;
|
||||
use std::sync::Arc;
|
||||
|
||||
use async_graphql_value::ConstValue;
|
||||
|
||||
use crate::parser::types::Field;
|
||||
use crate::registry::Registry;
|
||||
use crate::{
|
||||
registry, ContainerType, ContextSelectionSet, InputValueResult, Positioned, Result,
|
||||
ServerResult, Value,
|
||||
registry, ContainerType, ContextSelectionSet, InputValueError, InputValueResult, Positioned,
|
||||
Result, ServerResult, Value,
|
||||
};
|
||||
|
||||
#[doc(hidden)]
|
||||
|
@ -78,50 +80,6 @@ impl<T: OutputType + Send + Sync + ?Sized> OutputType for &T {
|
|||
}
|
||||
}
|
||||
|
||||
impl<T: Type + Send + Sync + ?Sized> Type for Box<T> {
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
T::type_name()
|
||||
}
|
||||
|
||||
fn create_type_info(registry: &mut Registry) -> String {
|
||||
T::create_type_info(registry)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: OutputType + Send + Sync + ?Sized> OutputType for Box<T> {
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
async fn resolve(
|
||||
&self,
|
||||
ctx: &ContextSelectionSet<'_>,
|
||||
field: &Positioned<Field>,
|
||||
) -> ServerResult<Value> {
|
||||
T::resolve(&**self, ctx, field).await
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Type + Send + Sync + ?Sized> Type for Arc<T> {
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
T::type_name()
|
||||
}
|
||||
|
||||
fn create_type_info(registry: &mut Registry) -> String {
|
||||
T::create_type_info(registry)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: OutputType + Send + Sync + ?Sized> OutputType for Arc<T> {
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
async fn resolve(
|
||||
&self,
|
||||
ctx: &ContextSelectionSet<'_>,
|
||||
field: &Positioned<Field>,
|
||||
) -> ServerResult<Value> {
|
||||
T::resolve(&**self, ctx, field).await
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Type> Type for Result<T> {
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
T::type_name()
|
||||
|
@ -164,3 +122,69 @@ pub trait UnionType: ContainerType {}
|
|||
|
||||
/// A GraphQL input object.
|
||||
pub trait InputObjectType: InputType {}
|
||||
|
||||
impl<T: Type + Send + Sync + ?Sized> Type for Box<T> {
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
T::type_name()
|
||||
}
|
||||
|
||||
fn create_type_info(registry: &mut Registry) -> String {
|
||||
T::create_type_info(registry)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: OutputType + Send + Sync + ?Sized> OutputType for Box<T> {
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
async fn resolve(
|
||||
&self,
|
||||
ctx: &ContextSelectionSet<'_>,
|
||||
field: &Positioned<Field>,
|
||||
) -> ServerResult<Value> {
|
||||
T::resolve(&**self, ctx, field).await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: InputType + Send + Sync> InputType for Box<T> {
|
||||
fn parse(value: Option<ConstValue>) -> InputValueResult<Self> {
|
||||
T::parse(value).map(Box::new).map_err(InputValueError::map)
|
||||
}
|
||||
|
||||
fn to_value(&self) -> ConstValue {
|
||||
T::to_value(&self)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Type + Send + Sync + ?Sized> Type for Arc<T> {
|
||||
fn type_name() -> Cow<'static, str> {
|
||||
T::type_name()
|
||||
}
|
||||
|
||||
fn create_type_info(registry: &mut Registry) -> String {
|
||||
T::create_type_info(registry)
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: OutputType + Send + Sync + ?Sized> OutputType for Arc<T> {
|
||||
#[allow(clippy::trivially_copy_pass_by_ref)]
|
||||
async fn resolve(
|
||||
&self,
|
||||
ctx: &ContextSelectionSet<'_>,
|
||||
field: &Positioned<Field>,
|
||||
) -> ServerResult<Value> {
|
||||
T::resolve(&**self, ctx, field).await
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl<T: InputType + Send + Sync> InputType for Arc<T> {
|
||||
fn parse(value: Option<ConstValue>) -> InputValueResult<Self> {
|
||||
T::parse(value).map(Arc::new).map_err(InputValueError::map)
|
||||
}
|
||||
|
||||
fn to_value(&self) -> ConstValue {
|
||||
T::to_value(&self)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -125,6 +125,13 @@ impl<T: InputType> InputValueError<T> {
|
|||
}
|
||||
}
|
||||
|
||||
pub(crate) fn map<Q>(self) -> InputValueError<Q> {
|
||||
InputValueError {
|
||||
message: self.message,
|
||||
phantom: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
/// The expected input type did not match the actual input type.
|
||||
#[must_use]
|
||||
pub fn expected_type(actual: Value) -> Self {
|
||||
|
|
|
@ -318,3 +318,34 @@ pub async fn test_input_object_skip_field() {
|
|||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[async_std::test]
|
||||
pub async fn test_box_input_object() {
|
||||
#[derive(InputObject)]
|
||||
struct MyInput {
|
||||
value: i32,
|
||||
input: Option<Box<MyInput>>,
|
||||
}
|
||||
|
||||
struct Root;
|
||||
|
||||
#[Object]
|
||||
impl Root {
|
||||
async fn q(&self, input: MyInput) -> i32 {
|
||||
input.value
|
||||
+ input.input.as_ref().unwrap().value
|
||||
+ input.input.as_ref().unwrap().input.as_ref().unwrap().value
|
||||
}
|
||||
}
|
||||
|
||||
let schema = Schema::new(Root, EmptyMutation, EmptySubscription);
|
||||
let query = r#"{
|
||||
q(input: {value: 100, input: { value: 200, input: { value: 300 } } })
|
||||
}"#;
|
||||
assert_eq!(
|
||||
schema.execute(query).await.into_result().unwrap().data,
|
||||
value!({
|
||||
"q": 600
|
||||
})
|
||||
);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user