Skip to content

Commit

Permalink
Add custom signals that are fired every time a mutation completes
Browse files Browse the repository at this point in the history
Signed-off-by: Tormod Haugland <tormod.haugland@gmail.com>
  • Loading branch information
tOgg1 committed Sep 16, 2024
1 parent 2c00ce7 commit 756cc1d
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 9 deletions.
4 changes: 4 additions & 0 deletions graphene_django_cud/mutations/batch_create.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from graphene_django_cud.consts import USE_ID_SUFFIXES_FOR_FK_SETTINGS_KEY, USE_ID_SUFFIXES_FOR_M2M_SETTINGS_KEY
from graphene_django_cud.mutations.core import DjangoCudBase, DjangoCudBaseOptions
from graphene_django_cud.registry import get_type_meta_registry
from graphene_django_cud.signals import post_batch_create_mutation
from graphene_django_cud.util import get_input_fields_for_model, apply_field_name_mappings


Expand Down Expand Up @@ -256,4 +257,7 @@ def mutate(cls, root, info, input):

return_data = {cls._meta.return_field_name: created_objs}
cls.after_mutate(root, info, input, created_objs, return_data)

post_batch_create_mutation.send(sender=Model, instances=created_objs)

return cls(**return_data)
2 changes: 2 additions & 0 deletions graphene_django_cud/mutations/batch_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from graphql_relay import to_global_id

from graphene_django_cud.mutations.core import DjangoCudBase
from graphene_django_cud.signals import post_batch_delete_mutation


class DjangoBatchDeleteMutationOptions(MutationOptions):
Expand Down Expand Up @@ -131,6 +132,7 @@ def mutate(cls, root, info, ids):
deletion_count, _ = qs_to_delete.delete()

cls.after_mutate(root, info, ids, deletion_count, deleted_ids)
post_batch_delete_mutation.send(sender=Model, ids=ids, deletion_count=deletion_count, deleted_ids=deleted_ids)

return cls(
deletion_count=deletion_count,
Expand Down
4 changes: 4 additions & 0 deletions graphene_django_cud/mutations/batch_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from graphene_django_cud.consts import USE_ID_SUFFIXES_FOR_M2M_SETTINGS_KEY, USE_ID_SUFFIXES_FOR_FK_SETTINGS_KEY
from graphene_django_cud.mutations.core import DjangoCudBase, DjangoCudBaseOptions
from graphene_django_cud.registry import get_type_meta_registry
from graphene_django_cud.signals import post_batch_update_mutation
from graphene_django_cud.util import get_input_fields_for_model, apply_field_name_mappings


Expand Down Expand Up @@ -272,4 +273,7 @@ def mutate(cls, root, info, input):

return_data = {cls._meta.return_field_name: updated_objs}
cls.after_mutate(root, info, input, updated_objs, return_data)

post_batch_update_mutation.send(sender=Model, instances=updated_objs)

return cls(**return_data)
3 changes: 3 additions & 0 deletions graphene_django_cud/mutations/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from graphene_django_cud.consts import USE_ID_SUFFIXES_FOR_FK_SETTINGS_KEY, USE_ID_SUFFIXES_FOR_M2M_SETTINGS_KEY
from graphene_django_cud.mutations.core import DjangoCudBase, DjangoCudBaseOptions
from graphene_django_cud.registry import get_type_meta_registry
from graphene_django_cud.signals import post_create_mutation
from graphene_django_cud.util import get_input_fields_for_model, apply_field_name_mappings


Expand Down Expand Up @@ -240,4 +241,6 @@ def mutate(cls, root, info, input):
return_data = {cls._meta.return_field_name: obj}
cls.after_mutate(root, info, input, obj, return_data)

post_create_mutation.send(sender=Model, instance=obj)

return cls(**return_data)
22 changes: 13 additions & 9 deletions graphene_django_cud/mutations/delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from graphql_relay import to_global_id

from graphene_django_cud.mutations.core import DjangoCudBase
from graphene_django_cud.signals import post_delete_mutation


class DjangoDeleteMutationOptions(MutationOptions):
Expand All @@ -26,15 +27,15 @@ class Meta:

@classmethod
def __init_subclass_with_meta__(
cls,
_meta=None,
model=None,
permissions=None,
login_required=None,
only_fields=(),
exclude_fields=(),
return_field_name=None,
**kwargs,
cls,
_meta=None,
model=None,
permissions=None,
login_required=None,
only_fields=(),
exclude_fields=(),
return_field_name=None,
**kwargs,
):
registry = get_global_registry()
model_type = registry.get_type_for_model(model)
Expand Down Expand Up @@ -131,6 +132,9 @@ def mutate(cls, root, info, id):
raw_id = obj.pk
cls.perform_delete(obj)
cls.after_mutate(root, info, id, True)

post_delete_mutation.send(sender=cls._meta.model, id=return_id, raw_id=raw_id, deleted_input_id=id)

return cls(
found=True,
deleted_raw_id=raw_id,
Expand Down
2 changes: 2 additions & 0 deletions graphene_django_cud/mutations/filter_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from graphql_relay import to_global_id

from graphene_django_cud.mutations.core import DjangoCudBase
from graphene_django_cud.signals import post_filter_update_mutation, post_filter_delete_mutation
from graphene_django_cud.util import get_filter_fields_input_args


Expand Down Expand Up @@ -167,5 +168,6 @@ def mutate(cls, root, info, input):
deletion_count, _ = filter_qs.delete()

cls.after_mutate(root, info, input, deletion_count, ids)
post_filter_delete_mutation.send(sender=Model, ids=ids)

return cls(deletion_count=deletion_count, deleted_ids=ids)
3 changes: 3 additions & 0 deletions graphene_django_cud/mutations/filter_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from graphql import GraphQLError

from graphene_django_cud.mutations.core import DjangoCudBase, meta_registry
from graphene_django_cud.signals import post_filter_update_mutation
from graphene_django_cud.util import (
get_filter_fields_input_args,
get_input_fields_for_model,
Expand Down Expand Up @@ -240,4 +241,6 @@ def mutate(cls, root, info, filter, data):
filter_qs.update(**data)

cls.after_mutate(root, info, filter, data, filter_qs)
post_filter_update_mutation.send(sender=Model, instances=filter_qs, data=data)

return cls(updated_objects=filter_qs, updated_count=filter_qs.count())
3 changes: 3 additions & 0 deletions graphene_django_cud/mutations/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from graphene_django_cud.consts import USE_ID_SUFFIXES_FOR_FK_SETTINGS_KEY, USE_ID_SUFFIXES_FOR_M2M_SETTINGS_KEY
from graphene_django_cud.mutations.core import DjangoCudBaseOptions, DjangoCudBase
from graphene_django_cud.registry import get_type_meta_registry
from graphene_django_cud.signals import post_update_mutation
from graphene_django_cud.util import get_input_fields_for_model, to_snake_case, apply_field_name_mappings


Expand Down Expand Up @@ -254,4 +255,6 @@ def mutate(cls, root, info, input, id):
return_data = {cls._meta.return_field_name: obj}
cls.after_mutate(root, info, id, input, obj, return_data)

post_update_mutation.send(sender=Model, instance=obj)

return cls(**return_data)
12 changes: 12 additions & 0 deletions graphene_django_cud/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.dispatch import Signal

post_create_mutation = Signal()
post_update_mutation = Signal()
post_delete_mutation = Signal()

post_batch_create_mutation = Signal()
post_batch_update_mutation = Signal()
post_batch_delete_mutation = Signal()

post_filter_update_mutation = Signal()
post_filter_delete_mutation = Signal()

0 comments on commit 756cc1d

Please sign in to comment.