Implements Cow<T> for OutputValueType. #189

This commit is contained in:
Sunli 2020-11-08 09:22:50 +08:00
parent f6b6241f52
commit 8370001272
4 changed files with 107 additions and 5 deletions

View File

@ -55,7 +55,7 @@ pub trait OutputValueType: Type {
) -> ServerResult<Value>;
}
impl<T: Type + Send + Sync> Type for &T {
impl<T: Type + Send + Sync + ?Sized> Type for &T {
fn type_name() -> Cow<'static, str> {
T::type_name()
}
@ -66,7 +66,7 @@ impl<T: Type + Send + Sync> Type for &T {
}
#[async_trait::async_trait]
impl<T: OutputValueType + Send + Sync> OutputValueType for &T {
impl<T: OutputValueType + Send + Sync + ?Sized> OutputValueType for &T {
#[allow(clippy::trivially_copy_pass_by_ref)]
async fn resolve(
&self,

101
src/types/external/cow.rs vendored Normal file
View File

@ -0,0 +1,101 @@
use std::borrow::Cow;
use crate::{
registry, ContextSelectionSet, OutputValueType, Positioned, ServerResult, Type, Value,
};
use async_graphql_parser::types::Field;
impl<'a, T> Type for Cow<'a, T>
where
T: Type + ToOwned + ?Sized + Send + Sync,
{
fn type_name() -> Cow<'static, str> {
T::type_name()
}
fn create_type_info(registry: &mut registry::Registry) -> String {
<T as Type>::create_type_info(registry)
}
}
#[async_trait::async_trait]
impl<'a, T> OutputValueType for Cow<'a, T>
where
T: OutputValueType + ToOwned + ?Sized + Send + Sync,
<T as ToOwned>::Owned: Send + Sync,
{
async fn resolve(
&self,
ctx: &ContextSelectionSet<'_>,
field: &Positioned<Field>,
) -> ServerResult<Value> {
self.as_ref().resolve(ctx, field).await
}
}
#[cfg(test)]
mod test {
use crate::*;
use std::borrow::Cow;
#[async_std::test]
async fn test_cow_type() {
struct Query11 {
obj: MyObj,
}
#[derive(SimpleObject, Clone)]
#[graphql(internal)]
struct MyObj {
a: i32,
b: i32,
}
#[Object(internal)]
impl Query11 {
async fn value1(&self) -> Cow<'_, str> {
Cow::Borrowed("abc")
}
async fn value2(&self) -> Cow<'_, str> {
Cow::Owned("def".to_string())
}
async fn obj1(&self) -> Cow<'_, MyObj> {
Cow::Borrowed(&self.obj)
}
async fn obj2(&self) -> Cow<'_, MyObj> {
Cow::Owned(MyObj { a: 300, b: 400 })
}
}
let query = r#"{
value1
value2
obj1 {
a b
}
obj2 {
a b
}
}"#;
let schema = Schema::new(
Query11 {
obj: MyObj { a: 100, b: 200 },
},
EmptyMutation,
EmptySubscription,
);
assert_eq!(
schema.execute(query).await.into_result().unwrap().data,
value!({
"value1": "abc",
"value2": "def",
"obj1": {"a": 100, "b": 200},
"obj2": {"a": 300, "b": 400},
})
);
}
}

View File

@ -2,6 +2,7 @@
mod bool;
mod char;
mod cow;
mod floats;
mod integers;
mod json_object;

View File

@ -25,7 +25,7 @@ impl ScalarType for String {
}
}
impl<'a> Type for &'a str {
impl Type for str {
fn type_name() -> Cow<'static, str> {
Cow::Borrowed("String")
}
@ -36,12 +36,12 @@ impl<'a> Type for &'a str {
}
#[async_trait::async_trait]
impl<'a> OutputValueType for &'a str {
impl OutputValueType for str {
async fn resolve(
&self,
_: &ContextSelectionSet<'_>,
_field: &Positioned<Field>,
) -> ServerResult<Value> {
Ok(Value::String((*self).to_string()))
Ok(Value::String(self.to_string()))
}
}