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

Merged
merged 8 commits into from
Dec 9, 2024
Merged
Changes from 1 commit
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
Next Next commit
Add guard for delete mutation
YiNNx committed Dec 18, 2023
commit 535de1e09c420ddb90f070c421b63710d0b5ec89
14 changes: 14 additions & 0 deletions examples/sqlite/tests/guard_mutation_tests.rs
Original file line number Diff line number Diff line change
@@ -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]
16 changes: 15 additions & 1 deletion src/mutation/entity_delete_mutation.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ use sea_orm::{

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

/// The configuration structure of EntityDeleteMutationBuilder
@@ -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);