Add a `ref` attribute to the field attribute of `SimpleObject`.
This commit is contained in:
sunli 2020-04-21 15:40:19 +08:00
parent 64e68c949b
commit 041eb7b1b7
8 changed files with 78 additions and 13 deletions

View File

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

View File

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

View File

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

View File

@ -214,6 +214,7 @@ pub struct Field {
pub external: bool,
pub provides: Option<String>,
pub requires: Option<String>,
pub is_ref: bool,
}
impl Field {
@ -226,6 +227,7 @@ impl Field {
let mut external = false;
let mut provides = None;
let mut requires = None;
let mut is_ref = false;
for attr in attrs {
match attr.parse_meta()? {
@ -239,6 +241,9 @@ impl Field {
NestedMeta::Meta(Meta::Path(p)) if p.is_ident("external") => {
external = true;
}
NestedMeta::Meta(Meta::Path(p)) if p.is_ident("ref") => {
is_ref = true;
}
NestedMeta::Meta(Meta::NameValue(nv)) => {
if nv.path.is_ident("name") {
if let syn::Lit::Str(lit) = &nv.lit {
@ -309,6 +314,7 @@ impl Field {
external,
provides,
requires,
is_ref,
}))
} else {
Ok(None)

View File

@ -90,12 +90,22 @@ pub fn generate(object_args: &args::Object, input: &mut DeriveInput) -> Result<T
});
let ident = &item.ident;
getters.push(quote! {
#[inline]
#vis async fn #ident(&self) -> #ty {
self.#ident.clone()
}
});
if field.is_ref {
getters.push(quote! {
#[inline]
#vis async fn #ident(&self) -> &#ty {
&self.#ident
}
});
} else {
getters.push(quote! {
#[inline]
#vis async fn #ident(&self) -> #ty {
self.#ident.clone()
}
});
}
resolvers.push(quote! {
if field.name.as_str() == #field_name {

View File

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

View File

@ -309,6 +309,7 @@ pub use async_graphql_derive::SimpleObject;
/// | name | Item name | string | Y |
/// | desc | Item description | string | Y |
/// | deprecation | Item deprecation reason | string | Y |
/// | ref | The resolver function returns a borrowing value | bool | Y |
///
/// # Examples
///
@ -409,6 +410,7 @@ pub use async_graphql_derive::InputObject;
/// |-------------|---------------------------|----------|----------|
/// | name | Object name | string | Y |
/// | desc | Object description | string | Y |
/// | implements | The interface implements another interface | String | Y |
///
/// # Field parameters
///

View File

@ -47,6 +47,53 @@ pub async fn test_interface_simple_object() {
);
}
#[async_std::test]
pub async fn test_interface_simple_object2() {
#[async_graphql::SimpleObject]
pub struct MyObj {
#[field(ref)]
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]
pub async fn test_multiple_interfaces() {
struct MyObj;