Merge branch 'master' into interface-test

This commit is contained in:
Sunli 2020-04-22 13:39:07 +08:00 committed by GitHub
commit 7e481b5bde
6 changed files with 114 additions and 68 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "async-graphql" name = "async-graphql"
version = "1.9.8" version = "1.9.9"
authors = ["sunli <scott_s829@163.com>"] authors = ["sunli <scott_s829@163.com>"]
edition = "2018" edition = "2018"
description = "The GraphQL server library implemented by rust" description = "The GraphQL server library implemented by rust"
@ -18,7 +18,7 @@ default = ["bson", "uuid", "url", "chrono-tz", "validators"]
validators = ["regex"] validators = ["regex"]
[dependencies] [dependencies]
async-graphql-derive = { path = "async-graphql-derive", version = "1.9.8" } async-graphql-derive = { path = "async-graphql-derive", version = "1.9.9" }
graphql-parser = "=0.2.3" graphql-parser = "=0.2.3"
anyhow = "1.0.26" anyhow = "1.0.26"
thiserror = "1.0.11" thiserror = "1.0.11"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "async-graphql-actix-web" name = "async-graphql-actix-web"
version = "1.0.8" version = "1.0.9"
authors = ["sunli <scott_s829@163.com>"] authors = ["sunli <scott_s829@163.com>"]
edition = "2018" edition = "2018"
description = "async-graphql for actix-web" description = "async-graphql for actix-web"
@ -13,7 +13,7 @@ keywords = ["futures", "async", "graphql"]
categories = ["network-programming", "asynchronous"] categories = ["network-programming", "asynchronous"]
[dependencies] [dependencies]
async-graphql = { path = "..", version = "1.9.8" } async-graphql = { path = "..", version = "1.9.9" }
actix-web = "2.0.0" actix-web = "2.0.0"
actix-web-actors = "2.0.0" actix-web-actors = "2.0.0"
actix = "0.9.0" actix = "0.9.0"

View File

@ -1,6 +1,6 @@
[package] [package]
name = "async-graphql-derive" name = "async-graphql-derive"
version = "1.9.8" version = "1.9.9"
authors = ["sunli <scott_s829@163.com>"] authors = ["sunli <scott_s829@163.com>"]
edition = "2018" edition = "2018"
description = "Macros for async-graphql" description = "Macros for async-graphql"

View File

@ -223,7 +223,7 @@ pub fn generate(interface_args: &args::Interface, input: &DeriveInput) -> Result
OutputType::Result(_, _) => { OutputType::Result(_, _) => {
quote! { quote! {
self.#method_name(#(#use_params),*).await. self.#method_name(#(#use_params),*).await.
map_err(|err| err.with_position(field.position))? map_err(|err| err.into_error_with_path(field.position, ctx.path_node.as_ref().unwrap().to_json()))?
} }
} }
}; };

View File

@ -1,6 +1,6 @@
[package] [package]
name = "async-graphql-warp" name = "async-graphql-warp"
version = "1.0.9" version = "1.0.10"
authors = ["sunli <scott_s829@163.com>"] authors = ["sunli <scott_s829@163.com>"]
edition = "2018" edition = "2018"
description = "async-graphql for warp" description = "async-graphql for warp"
@ -13,7 +13,7 @@ keywords = ["futures", "async", "graphql"]
categories = ["network-programming", "asynchronous"] categories = ["network-programming", "asynchronous"]
[dependencies] [dependencies]
async-graphql = { path = "..", version = "1.9.8" } async-graphql = { path = "..", version = "1.9.9" }
warp = "0.2.2" warp = "0.2.2"
futures = "0.3.0" futures = "0.3.0"
bytes = "0.5.4" bytes = "0.5.4"

View File

@ -3,15 +3,15 @@ use async_graphql::*;
#[async_std::test] #[async_std::test]
pub async fn test_interface_simple_object() { pub async fn test_interface_simple_object() {
#[async_graphql::SimpleObject] #[async_graphql::SimpleObject]
pub struct MyObj { struct MyObj {
#[field] #[field]
pub id: i32, id: i32,
#[field] #[field]
pub title: String, title: String,
} }
#[async_graphql::Interface(field(name = "id", type = "i32"))] #[async_graphql::Interface(field(name = "id", type = "i32"))]
pub struct Node(MyObj); struct Node(MyObj);
struct Query; struct Query;
@ -50,15 +50,15 @@ pub async fn test_interface_simple_object() {
#[async_std::test] #[async_std::test]
pub async fn test_interface_simple_object2() { pub async fn test_interface_simple_object2() {
#[async_graphql::SimpleObject] #[async_graphql::SimpleObject]
pub struct MyObj { struct MyObj {
#[field(ref)] #[field(ref)]
pub id: i32, id: i32,
#[field] #[field]
pub title: String, title: String,
} }
#[async_graphql::Interface(field(name = "id", type = "&i32"))] #[async_graphql::Interface(field(name = "id", type = "&i32"))]
pub struct Node(MyObj); struct Node(MyObj);
struct Query; struct Query;
@ -163,81 +163,127 @@ pub async fn test_multiple_interfaces() {
} }
#[async_std::test] #[async_std::test]
pub async fn test_multiple_objects_in_multiple_interfaces() { pub async fn test_multiple_objects_in_multiple_interfaces() {
struct MyObjOne; struct MyObjOne;
#[async_graphql::Object]
impl MyObjOne {
#[field]
async fn value_a(&self) -> i32 {
1
}
#[field]
async fn value_b(&self) -> i32 {
2
}
#[field]
async fn value_c(&self) -> i32 {
3
}
}
struct MyObjTwo;
#[async_graphql::Object]
impl MyObjTwo {
#[field]
async fn value_a(&self) -> i32 {
1
}
}
#[async_graphql::Interface(field(name = "value_a", type = "i32"))]
struct InterfaceA(MyObjOne, MyObjTwo);
#[async_graphql::Interface(field(name = "value_b", type = "i32"))]
struct InterfaceB(MyObjOne);
struct Query;
#[Object]
impl Query {
#[field]
async fn my_obj(&self) -> Vec<InterfaceA> {
vec![MyObjOne.into(), MyObjTwo.into()]
}
}
let schema = Schema::build(Query, EmptyMutation, EmptySubscription)
.register_type::<InterfaceB>() // `InterfaceA` is not directly referenced, so manual registration is required.
.finish();
let query = format!(
r#"{{
myObj {{
... on InterfaceA {{
valueA
}}
... on InterfaceB {{
valueB
}}
... on MyObjOne {{
valueC
}}
}}
}}"#
);
assert_eq!(
schema.execute(&query).await.unwrap().data,
serde_json::json!({
"myObj": [{
"valueA": 1,
"valueB": 2,
"valueC": 3,
}, {
"valueA": 1
}]
})
);
}
#[async_std::test]
pub async fn test_interface_field_result() {
struct MyObj;
#[async_graphql::Object] #[async_graphql::Object]
impl MyObjOne { impl MyObj {
#[field] #[field]
async fn value_a(&self) -> i32 { async fn value(&self) -> FieldResult<i32> {
1 Ok(10)
}
#[field]
async fn value_b(&self) -> i32 {
2
}
#[field]
async fn value_c(&self) -> i32 {
3
} }
} }
struct MyObjTwo; #[async_graphql::Interface(field(name = "value", type = "FieldResult<i32>"))]
struct Node(MyObj);
#[async_graphql::Object]
impl MyObjTwo {
#[field]
async fn value_a(&self) -> i32 {
1
}
}
#[async_graphql::Interface(field(name = "value_a", type = "i32"))]
struct InterfaceA(MyObjOne, MyObjTwo);
#[async_graphql::Interface(field(name = "value_b", type = "i32"))]
struct InterfaceB(MyObjOne);
struct Query; struct Query;
#[Object] #[Object]
impl Query { impl Query {
#[field] #[field]
async fn my_obj(&self) -> Vec<InterfaceA> { async fn node(&self) -> Node {
vec![MyObjOne.into(), MyObjTwo.into()] MyObj.into()
} }
} }
let schema = Schema::build(Query, EmptyMutation, EmptySubscription)
.register_type::<InterfaceB>() // `InterfaceA` is not directly referenced, so manual registration is required.
.finish();
let query = format!( let query = format!(
r#"{{ r#"{{
myObj {{ node {{
... on InterfaceA {{ ... on Node {{
valueA value
}} }}
... on InterfaceB {{
valueB
}}
... on MyObjOne {{
valueC
}}
}} }}
}}"# }}"#
); );
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!( assert_eq!(
schema.execute(&query).await.unwrap().data, schema.execute(&query).await.unwrap().data,
serde_json::json!({ serde_json::json!({
"myObj": [{ "node": {
"valueA": 1, "value": 10,
"valueB": 2, }
"valueC": 3,
}, {
"valueA": 1
}]
}) })
); );
} }