This commit is contained in:
sunli 2020-04-08 09:05:54 +08:00
parent 66c22c52e6
commit a813ce72a3
9 changed files with 116 additions and 9 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "async-graphql"
version = "1.7.7"
version = "1.7.8"
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.7.7" }
async-graphql-derive = { path = "async-graphql-derive", version = "1.7.8" }
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 = "0.7.7"
version = "0.7.8"
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.7.7" }
async-graphql = { path = "..", version = "1.7.8" }
actix-web = "2.0.0"
actix-multipart = "0.2.0"
actix-web-actors = "2.0.0"

View File

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

View File

@ -6,7 +6,7 @@ use proc_macro2::{Ident, Span};
use quote::quote;
use syn::{Data, DeriveInput, Error, Meta, NestedMeta, Result};
const DERIVES: &[&str] = &["Copy", "Clone", "Eq", "PartialEq", "Debug"];
const DERIVES: &[&str] = &["Copy", "Clone", "Eq", "PartialEq"];
pub fn generate(enum_args: &args::Enum, input: &DeriveInput) -> Result<TokenStream> {
let crate_name = get_crate_name(enum_args.internal);
@ -86,6 +86,11 @@ pub fn generate(enum_args: &args::Enum, input: &DeriveInput) -> Result<TokenStre
}
let item_ident = &variant.ident;
let item_attrs = variant
.attrs
.iter()
.filter(|attr| !attr.path.is_ident("item"))
.collect::<Vec<_>>();
let mut item_args = args::EnumItem::parse(&variant.attrs)?;
let gql_item_name = item_args
.name
@ -101,7 +106,7 @@ pub fn generate(enum_args: &args::Enum, input: &DeriveInput) -> Result<TokenStre
.as_ref()
.map(|s| quote! { Some(#s) })
.unwrap_or_else(|| quote! {None});
enum_items.push(&variant.ident);
enum_items.push(quote! { #(#item_attrs)* #item_ident});
items.push(quote! {
#crate_name::EnumItem {
name: #gql_item_name,

View File

@ -6,6 +6,7 @@ use async_graphql_derive::{Enum, Object};
internal,
desc = "A Directive can be adjacent to many parts of the GraphQL language, a __DirectiveLocation describes one such possible adjacencies."
)]
#[derive(Debug)]
#[allow(non_camel_case_types)]
pub enum __DirectiveLocation {
#[item(desc = "Location adjacent to a query operation.")]

View File

@ -80,6 +80,7 @@ where
}
#[allow(missing_docs)]
#[allow(clippy::type_complexity)]
pub struct SubscriptionStream<Query, Mutation, Subscription, T: SubscriptionTransport> {
schema: Schema<Query, Mutation, Subscription>,
transport: T,

View File

@ -9,8 +9,7 @@ use std::collections::HashMap;
use std::pin::Pin;
use std::sync::Mutex;
static SUBSCRIBERS: Lazy<Mutex<HashMap<TypeId, Box<dyn Any + Send>>>> =
Lazy::new(|| Default::default());
static SUBSCRIBERS: Lazy<Mutex<HashMap<TypeId, Box<dyn Any + Send>>>> = Lazy::new(Default::default);
struct Senders<T>(Slab<UnboundedSender<T>>);

View File

@ -52,3 +52,26 @@ pub async fn test_enum_type() {
})
);
}
#[async_std::test]
pub async fn test_enum_derive_and_item_attributes() {
use serde::Deserialize;
#[async_graphql::Enum]
#[derive(Deserialize, PartialEq, Debug)]
enum Test {
#[serde(alias = "Other")]
Real,
}
#[derive(Deserialize, PartialEq, Debug)]
#[allow(dead_code)]
struct TestStruct {
value: Test,
}
assert_eq!(
serde_json::from_str::<TestStruct>(r#"{ "value" : "Other" }"#).unwrap(),
TestStruct { value: Test::Real }
);
}

View File

@ -70,3 +70,81 @@ pub async fn test_subscription() {
assert!(stream.next().await.is_none());
}
}
#[async_std::test]
pub async fn test_simple_broker() {
struct QueryRoot;
#[SimpleObject]
#[derive(Clone)]
struct Event1 {
#[field]
value: i32,
}
#[SimpleObject]
#[derive(Clone)]
struct Event2 {
#[field]
value: i32,
}
#[Object]
impl QueryRoot {}
struct SubscriptionRoot;
#[Subscription]
impl SubscriptionRoot {
#[field]
async fn events1(&self) -> impl Stream<Item = Event1> {
SimpleBroker::<Event1>::subscribe()
}
#[field]
async fn events2(&self) -> impl Stream<Item = Event2> {
SimpleBroker::<Event2>::subscribe()
}
}
let schema = Schema::new(QueryRoot, EmptyMutation, SubscriptionRoot);
let mut stream1 = schema
.create_subscription_stream(
"subscription { events1 { value } }",
None,
Default::default(),
)
.await
.unwrap();
let mut stream2 = schema
.create_subscription_stream(
"subscription { events2 { value } }",
None,
Default::default(),
)
.await
.unwrap();
SimpleBroker::publish(Event1 { value: 10 });
SimpleBroker::publish(Event2 { value: 88 });
SimpleBroker::publish(Event1 { value: 15 });
SimpleBroker::publish(Event2 { value: 99 });
assert_eq!(
stream1.next().await,
Some(serde_json::json!({ "events1": {"value": 10} }))
);
assert_eq!(
stream1.next().await,
Some(serde_json::json!({ "events1": {"value": 15} }))
);
assert_eq!(
stream2.next().await,
Some(serde_json::json!({ "events2": {"value": 88} }))
);
assert_eq!(
stream2.next().await,
Some(serde_json::json!({ "events2": {"value": 99} }))
);
}