v1.10.1
This commit is contained in:
parent
ee50ae9be6
commit
81e2143015
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "async-graphql"
|
name = "async-graphql"
|
||||||
version = "1.10.0"
|
version = "1.10.1"
|
||||||
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"
|
||||||
|
@ -17,7 +17,7 @@ readme = "README.md"
|
||||||
default = ["bson", "uuid", "url", "chrono-tz"]
|
default = ["bson", "uuid", "url", "chrono-tz"]
|
||||||
|
|
||||||
[dependencies]
|
[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"
|
graphql-parser = "=0.2.3"
|
||||||
anyhow = "1.0.26"
|
anyhow = "1.0.26"
|
||||||
thiserror = "1.0.11"
|
thiserror = "1.0.11"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "async-graphql-actix-web"
|
name = "async-graphql-actix-web"
|
||||||
version = "1.2.0"
|
version = "1.2.1"
|
||||||
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.10.0" }
|
async-graphql = { path = "..", version = "1.10.1" }
|
||||||
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"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "async-graphql-derive"
|
name = "async-graphql-derive"
|
||||||
version = "1.10.0"
|
version = "1.10.1"
|
||||||
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"
|
||||||
|
|
|
@ -136,7 +136,7 @@ fn parse_nested_validator(
|
||||||
let name = &nv.path;
|
let name = &nv.path;
|
||||||
if let Lit::Str(value) = &nv.lit {
|
if let Lit::Str(value) = &nv.lit {
|
||||||
let expr = syn::parse_str::<Expr>(&value.value())?;
|
let expr = syn::parse_str::<Expr>(&value.value())?;
|
||||||
params.push(quote! { #name: #expr });
|
params.push(quote! { #name: #expr.into() });
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::new_spanned(
|
return Err(Error::new_spanned(
|
||||||
&nv.lit,
|
&nv.lit,
|
||||||
|
@ -197,7 +197,7 @@ pub fn parse_guards(crate_name: &TokenStream, args: &MetaList) -> Result<Option<
|
||||||
let name = &nv.path;
|
let name = &nv.path;
|
||||||
if let Lit::Str(value) = &nv.lit {
|
if let Lit::Str(value) = &nv.lit {
|
||||||
let expr = syn::parse_str::<Expr>(&value.value())?;
|
let expr = syn::parse_str::<Expr>(&value.value())?;
|
||||||
params.push(quote! { #name: #expr });
|
params.push(quote! { #name: #expr.into() });
|
||||||
} else {
|
} else {
|
||||||
return Err(Error::new_spanned(
|
return Err(Error::new_spanned(
|
||||||
&nv.lit,
|
&nv.lit,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "async-graphql-tide"
|
name = "async-graphql-tide"
|
||||||
version = "1.1.0"
|
version = "1.1.1"
|
||||||
authors = ["vkill <vkill.net@gmail.com>"]
|
authors = ["vkill <vkill.net@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "async-graphql for tide"
|
description = "async-graphql for tide"
|
||||||
|
@ -13,7 +13,7 @@ keywords = ["futures", "async", "graphql"]
|
||||||
categories = ["network-programming", "asynchronous"]
|
categories = ["network-programming", "asynchronous"]
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
async-graphql = { path = "..", version = "1.10.0" }
|
async-graphql = { path = "..", version = "1.10.1" }
|
||||||
tide = "0.8"
|
tide = "0.8"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "async-graphql-warp"
|
name = "async-graphql-warp"
|
||||||
version = "1.2.0"
|
version = "1.2.1"
|
||||||
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.10.0" }
|
async-graphql = { path = "..", version = "1.10.1" }
|
||||||
warp = "0.2.2"
|
warp = "0.2.2"
|
||||||
futures = "0.3.0"
|
futures = "0.3.0"
|
||||||
bytes = "0.5.4"
|
bytes = "0.5.4"
|
||||||
|
|
130
tests/guard.rs
130
tests/guard.rs
|
@ -3,29 +3,46 @@ use async_graphql::*;
|
||||||
use futures::{Stream, StreamExt};
|
use futures::{Stream, StreamExt};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
#[async_std::test]
|
#[derive(Eq, PartialEq, Copy, Clone)]
|
||||||
pub async fn test_enum_type() {
|
enum Role {
|
||||||
#[derive(Eq, PartialEq, Copy, Clone)]
|
Admin,
|
||||||
enum Role {
|
Guest,
|
||||||
Admin,
|
}
|
||||||
Guest,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct RoleGuard {
|
struct RoleGuard {
|
||||||
role: Role,
|
role: Role,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
#[async_trait::async_trait]
|
||||||
impl Guard for RoleGuard {
|
impl Guard for RoleGuard {
|
||||||
async fn check(&self, ctx: &Context<'_>) -> FieldResult<()> {
|
async fn check(&self, ctx: &Context<'_>) -> FieldResult<()> {
|
||||||
if *ctx.data::<Role>() == self.role {
|
if ctx.data_opt::<Role>() == Some(&self.role) {
|
||||||
Ok(())
|
Ok(())
|
||||||
} else {
|
} else {
|
||||||
Err("Forbidden".into())
|
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]
|
#[SimpleObject]
|
||||||
struct MyObj {
|
struct MyObj {
|
||||||
#[field(guard(RoleGuard(role = "Role::Admin")))]
|
#[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,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user