This commit is contained in:
sunli 2020-05-02 11:03:04 +08:00
parent ee50ae9be6
commit 81e2143015
7 changed files with 123 additions and 29 deletions

View File

@ -1,6 +1,6 @@
[package]
name = "async-graphql"
version = "1.10.0"
version = "1.10.1"
authors = ["sunli <scott_s829@163.com>"]
edition = "2018"
description = "The GraphQL server library implemented by rust"
@ -17,7 +17,7 @@ readme = "README.md"
default = ["bson", "uuid", "url", "chrono-tz"]
[dependencies]
async-graphql-derive = { path = "async-graphql-derive", version = "1.10.0" }
async-graphql-derive = { path = "async-graphql-derive", version = "1.10.1" }
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.2.0"
version = "1.2.1"
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.10.0" }
async-graphql = { path = "..", version = "1.10.1" }
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.10.0"
version = "1.10.1"
authors = ["sunli <scott_s829@163.com>"]
edition = "2018"
description = "Macros for async-graphql"

View File

@ -136,7 +136,7 @@ fn parse_nested_validator(
let name = &nv.path;
if let Lit::Str(value) = &nv.lit {
let expr = syn::parse_str::<Expr>(&value.value())?;
params.push(quote! { #name: #expr });
params.push(quote! { #name: #expr.into() });
} else {
return Err(Error::new_spanned(
&nv.lit,
@ -197,7 +197,7 @@ pub fn parse_guards(crate_name: &TokenStream, args: &MetaList) -> Result<Option<
let name = &nv.path;
if let Lit::Str(value) = &nv.lit {
let expr = syn::parse_str::<Expr>(&value.value())?;
params.push(quote! { #name: #expr });
params.push(quote! { #name: #expr.into() });
} else {
return Err(Error::new_spanned(
&nv.lit,

View File

@ -1,6 +1,6 @@
[package]
name = "async-graphql-tide"
version = "1.1.0"
version = "1.1.1"
authors = ["vkill <vkill.net@gmail.com>"]
edition = "2018"
description = "async-graphql for tide"
@ -13,7 +13,7 @@ keywords = ["futures", "async", "graphql"]
categories = ["network-programming", "asynchronous"]
[dependencies]
async-graphql = { path = "..", version = "1.10.0" }
async-graphql = { path = "..", version = "1.10.1" }
tide = "0.8"
[dev-dependencies]

View File

@ -1,6 +1,6 @@
[package]
name = "async-graphql-warp"
version = "1.2.0"
version = "1.2.1"
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.10.0" }
async-graphql = { path = "..", version = "1.10.1" }
warp = "0.2.2"
futures = "0.3.0"
bytes = "0.5.4"

View File

@ -3,29 +3,46 @@ use async_graphql::*;
use futures::{Stream, StreamExt};
use std::sync::Arc;
#[async_std::test]
pub async fn test_enum_type() {
#[derive(Eq, PartialEq, Copy, Clone)]
enum Role {
Admin,
Guest,
}
#[derive(Eq, PartialEq, Copy, Clone)]
enum Role {
Admin,
Guest,
}
struct RoleGuard {
role: Role,
}
struct RoleGuard {
role: Role,
}
#[async_trait::async_trait]
impl Guard for RoleGuard {
async fn check(&self, ctx: &Context<'_>) -> FieldResult<()> {
if *ctx.data::<Role>() == self.role {
Ok(())
} else {
Err("Forbidden".into())
}
#[async_trait::async_trait]
impl Guard for RoleGuard {
async fn check(&self, ctx: &Context<'_>) -> FieldResult<()> {
if ctx.data_opt::<Role>() == Some(&self.role) {
Ok(())
} else {
Err("Forbidden".into())
}
}
}
struct Username(String);
struct UserGuard {
username: String,
}
#[async_trait::async_trait]
impl Guard for UserGuard {
async fn check(&self, ctx: &Context<'_>) -> FieldResult<()> {
if ctx.data_opt::<Username>().map(|name| &name.0).as_deref() == Some(&self.username) {
Ok(())
} else {
Err("Forbidden".into())
}
}
}
#[async_std::test]
pub async fn test_guard() {
#[SimpleObject]
struct MyObj {
#[field(guard(RoleGuard(role = "Role::Admin")))]
@ -360,3 +377,80 @@ pub async fn test_enum_type() {
}
);
}
#[async_std::test]
pub async fn test_multiple_guards() {
#[SimpleObject]
struct Query {
#[field(guard(RoleGuard(role = "Role::Admin"), UserGuard(username = r#""test""#)))]
value: i32,
}
let schema = Schema::new(Query { value: 10 }, EmptyMutation, EmptySubscription);
let query = "{ value }";
assert_eq!(
QueryBuilder::new(query)
.data(Role::Admin)
.data(Username("test".to_string()))
.execute(&schema)
.await
.unwrap()
.data,
serde_json::json!({"value": 10})
);
let query = "{ value }";
assert_eq!(
QueryBuilder::new(query)
.data(Role::Guest)
.data(Username("test".to_string()))
.execute(&schema)
.await
.unwrap_err(),
Error::Query {
pos: Pos { line: 1, column: 3 },
path: Some(serde_json::json!(["value"])),
err: QueryError::FieldError {
err: "Forbidden".to_string(),
extended_error: None,
},
}
);
let query = "{ value }";
assert_eq!(
QueryBuilder::new(query)
.data(Role::Admin)
.data(Username("test1".to_string()))
.execute(&schema)
.await
.unwrap_err(),
Error::Query {
pos: Pos { line: 1, column: 3 },
path: Some(serde_json::json!(["value"])),
err: QueryError::FieldError {
err: "Forbidden".to_string(),
extended_error: None,
},
}
);
let query = "{ value }";
assert_eq!(
QueryBuilder::new(query)
.data(Role::Guest)
.data(Username("test1".to_string()))
.execute(&schema)
.await
.unwrap_err(),
Error::Query {
pos: Pos { line: 1, column: 3 },
path: Some(serde_json::json!(["value"])),
err: QueryError::FieldError {
err: "Forbidden".to_string(),
extended_error: None,
},
}
);
}