Fix field guards not working on `ComplexObject`. #767

This commit is contained in:
Sunli 2021-12-26 10:42:36 +08:00
parent ed9cb80e94
commit 9f51772ad9
3 changed files with 48 additions and 7 deletions

View File

@ -4,7 +4,10 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [3.0.18] 2021-12-26
- Federation's `_Entity` should not be sent if empty as it's in conflict with [GraphQL Union type validation](https://spec.graphql.org/draft/#sec-Unions.Type-Validation) [#765](https://github.com/async-graphql/async-graphql/pull/765).
- Fix field guards not working on `ComplexObject`. [#767](https://github.com/async-graphql/async-graphql/issues/767)
## [3.0.17] 2021-12-16

View File

@ -348,13 +348,6 @@ pub fn generate(
None => None,
};
let guard = guard.map(|guard| {
quote! {
#guard.check(ctx).await
.map_err(|err| err.into_server_error(ctx.item.pos))?;
}
});
resolvers.push(quote! {
#(#cfg_attrs)*
if ctx.item.node.name.node == #field_name {
@ -391,5 +384,6 @@ pub fn generate(
}
}
};
Ok(expanded.into())
}

View File

@ -345,3 +345,47 @@ pub async fn test_guard_use_params() {
}]
);
}
#[tokio::test]
pub async fn test_guard_on_complex_object() {
#[derive(SimpleObject)]
#[graphql(complex)]
struct Query {
value1: i32,
}
#[ComplexObject]
impl Query {
#[graphql(guard = "RoleGuard::new(Role::Admin)")]
async fn value2(&self) -> i32 {
100
}
}
let schema = Schema::new(Query { value1: 10 }, EmptyMutation, EmptySubscription);
let query = "{ value2 }";
assert_eq!(
schema
.execute(Request::new(query).data(Role::Admin))
.await
.data,
value!({"value2": 100})
);
let query = "{ value2 }";
assert_eq!(
schema
.execute(Request::new(query).data(Role::Guest))
.await
.into_result()
.unwrap_err(),
vec![ServerError {
message: "Forbidden".to_string(),
source: None,
locations: vec![Pos { line: 1, column: 3 }],
path: vec![PathSegment::Field("value2".to_owned())],
extensions: None,
}]
);
}