Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add guard for delete mutation #163

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/sqlite/tests/guard_mutation_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ async fn entity_guard_mutation() {
assert_eq!(response.errors.len(), 1);

assert_eq!(response.errors[0].message, "Entity guard triggered.");

let response = schema
.execute(
r#"
mutation FilmCategoryDelete {
filmCategoryDelete(filter: { filmId: { eq: 2 } })
}
"#,
)
.await;

assert_eq!(response.errors.len(), 1);

assert_eq!(response.errors[0].message, "Entity guard triggered.");
}

#[tokio::test]
Expand Down
16 changes: 15 additions & 1 deletion src/mutation/entity_delete_mutation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use sea_orm::{

use crate::{
get_filter_conditions, BuilderContext, EntityObjectBuilder, EntityQueryFieldBuilder,
FilterInputBuilder,
FilterInputBuilder, GuardAction,
};

/// The configuration structure of EntityDeleteMutationBuilder
Expand Down Expand Up @@ -66,11 +66,25 @@ impl EntityDeleteMutationBuilder {

let context = self.context;

let guard = self.context.guards.entity_guards.get(&object_name);

Field::new(
self.type_name::<T>(),
TypeRef::named_nn(TypeRef::INT),
move |ctx| {
FieldFuture::new(async move {
let guard_flag = if let Some(guard) = guard {
(*guard)(&ctx)
} else {
GuardAction::Allow
};

if let GuardAction::Block(reason) = guard_flag {
return Err::<Option<_>, async_graphql::Error>(async_graphql::Error::new(
reason.unwrap_or("Entity guard triggered.".into()),
));
}

let db = ctx.data::<DatabaseConnection>()?;

let filters = ctx.args.get(&context.entity_delete_mutation.filter_field);
Expand Down
Loading