Add SimpleObject support to Interface
This commit is contained in:
sunli 2020-04-21 15:27:43 +08:00
parent 98cdfd4c42
commit 64e68c949b
6 changed files with 68 additions and 8 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "async-graphql" name = "async-graphql"
version = "1.9.5" version = "1.9.6"
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.5" } async-graphql-derive = { path = "async-graphql-derive", version = "1.9.6" }
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.5" version = "1.0.6"
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.5" } async-graphql = { path = "..", version = "1.9.6" }
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.5" version = "1.9.6"
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

@ -26,6 +26,7 @@ pub fn generate(object_args: &args::Object, input: &mut DeriveInput) -> Result<T
.map(|s| quote! { Some(#s) }) .map(|s| quote! { Some(#s) })
.unwrap_or_else(|| quote! {None}); .unwrap_or_else(|| quote! {None});
let mut getters = Vec::new();
let mut resolvers = Vec::new(); let mut resolvers = Vec::new();
let mut schema_fields = Vec::new(); let mut schema_fields = Vec::new();
let fields = match &mut s.fields { let fields = match &mut s.fields {
@ -60,6 +61,7 @@ pub fn generate(object_args: &args::Object, input: &mut DeriveInput) -> Result<T
Some(provides) => quote! { Some(#provides) }, Some(provides) => quote! { Some(#provides) },
None => quote! { None }, None => quote! { None },
}; };
let vis = &item.vis;
let ty = &item.ty; let ty = &item.ty;
let cache_control = { let cache_control = {
@ -88,6 +90,13 @@ pub fn generate(object_args: &args::Object, input: &mut DeriveInput) -> Result<T
}); });
let ident = &item.ident; let ident = &item.ident;
getters.push(quote! {
#[inline]
#vis async fn #ident(&self) -> #ty {
self.#ident.clone()
}
});
resolvers.push(quote! { resolvers.push(quote! {
if field.name.as_str() == #field_name { if field.name.as_str() == #field_name {
let ctx_obj = ctx.with_selection_set(&field.selection_set); let ctx_obj = ctx.with_selection_set(&field.selection_set);
@ -121,7 +130,11 @@ pub fn generate(object_args: &args::Object, input: &mut DeriveInput) -> Result<T
let expanded = quote! { let expanded = quote! {
#input #input
impl #generics #crate_name::Type for #ident { impl #generics #ident {
#(#getters)*
}
impl #generics #crate_name::Type for #ident #generics {
fn type_name() -> std::borrow::Cow<'static, str> { fn type_name() -> std::borrow::Cow<'static, str> {
std::borrow::Cow::Borrowed(#gql_typename) std::borrow::Cow::Borrowed(#gql_typename)
} }

View File

@ -1,6 +1,6 @@
[package] [package]
name = "async-graphql-warp" name = "async-graphql-warp"
version = "1.0.6" version = "1.0.7"
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.5" } async-graphql = { path = "..", version = "1.9.6" }
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

@ -1,5 +1,52 @@
use async_graphql::*; use async_graphql::*;
#[async_std::test]
pub async fn test_interface_simple_object() {
#[async_graphql::SimpleObject]
pub struct MyObj {
#[field]
pub id: i32,
#[field]
pub title: String,
}
#[async_graphql::Interface(field(name = "id", type = "i32"))]
pub struct Node(MyObj);
struct Query;
#[Object]
impl Query {
#[field]
async fn node(&self) -> Node {
MyObj {
id: 33,
title: "haha".to_string(),
}
.into()
}
}
let query = format!(
r#"{{
node {{
... on Node {{
id
}}
}}
}}"#
);
let schema = Schema::new(Query, EmptyMutation, EmptySubscription);
assert_eq!(
schema.execute(&query).await.unwrap().data,
serde_json::json!({
"node": {
"id": 33,
}
})
);
}
#[async_std::test] #[async_std::test]
pub async fn test_multiple_interfaces() { pub async fn test_multiple_interfaces() {
struct MyObj; struct MyObj;