Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Abstract some logic to parent class and helper functions

Adam Janike48c6471c7a5
diff --git a/saleor/dashboard/graphql/mutations.py b/saleor/dashboard/graphql/mutations.py
index 65a2a2938e..b32a41f2ad 100644
--- a/saleor/dashboard/graphql/mutations.py
+++ b/saleor/dashboard/graphql/mutations.py
@@ -8,6 +8,7 @@ from graphene_django.registry import get_global_registry
from ...graphql.core.types import Error
from ...graphql.utils import get_node
+from .utils import get_model_name, get_model_type_and_fields
registry = get_global_registry()
@@ -31,19 +32,23 @@ def convert_form_errors(form):
class BaseMutation(graphene.Mutation):
errors = graphene.List(Error)
+ return_field_name = None
class Meta:
abstract = True
+ @classmethod
+ def _update_mutation_arguments_and_fields(cls, arguments, fields):
+ cls._meta.arguments.update(arguments)
+ cls._meta.fields.update(fields)
+
class ModelFormMutationOptions(MutationOptions):
form_class = None
- return_field_name = None
class ModelDeleteMutationOptions(MutationOptions):
model = None
- return_field_name = None
class ModelDeleteMutation(BaseMutation):
@@ -59,33 +64,31 @@ class ModelDeleteMutation(BaseMutation):
'model is required for ModelDeleteMutation')
_meta = ModelDeleteMutationOptions(cls)
- model_type = registry.get_type_for_model(model)
+
if not return_field_name:
- model_name = model.__name__
- return_field_name = model_name[:1].lower() + model_name[1:]
+ return_field_name = get_model_name(model)
if arguments is None:
arguments = {}
arguments.update({'id': graphene.ID()})
- fields = {return_field_name: graphene.Field(model_type)}
+ model_type, fields = get_model_type_and_fields(registry, model, return_field_name)
- _meta.model_type = model_type
_meta.model = model
_meta.return_field_name = return_field_name
+ _meta.model_type = model_type
super().__init_subclass_with_meta__(_meta=_meta, **options)
# Update mutation's arguments and fields
- cls._meta.arguments.update(arguments)
- cls._meta.fields.update(fields)
+ cls._update_mutation_arguments_and_fields(arguments=arguments, fields=fields)
@classmethod
def mutate(cls, root, info, id, **kwargs):
model_type = cls._meta.model_type
instance = get_node(info, id, only_type=model_type)
instance.delete()
- field = cls._meta.return_field_name
- return_kwargs = {field: instance}
- return cls(**return_kwargs)
+ field_name = cls._meta.return_field_name
+ kwargs = {field_name: instance}
+ return cls(**kwargs)
class ModelFormMutation(BaseMutation):
@@ -103,26 +106,19 @@ class ModelFormMutation(BaseMutation):
_meta = ModelFormMutationOptions(cls)
model = form_class._meta.model
- model_type = registry.get_type_for_model(model)
if not return_field_name:
- model_name = model.__name__
- return_field_name = model_name[:1].lower() + model_name[1:]
-
+ return_field_name = get_model_name(model)
+ _, fields = get_model_type_and_fields(registry, model, return_field_name)
# get mutation arguments based on model form
arguments = convert_form_fields(form_class)
- # get mutation output field for model instance
- fields = {return_field_name: graphene.Field(model_type)}
-
_meta.form_class = form_class
_meta.model = model
_meta.return_field_name = return_field_name
super().__init_subclass_with_meta__(_meta=_meta, **options)
- # Update mutation's arguments and fields
- cls._meta.arguments.update(arguments)
- cls._meta.fields.update(fields)
+ cls._update_mutation_arguments_and_fields(arguments=arguments, fields=fields)
@classmethod
def get_form_kwargs(cls, root, info, **input):
diff --git a/saleor/dashboard/graphql/utils.py b/saleor/dashboard/graphql/utils.py
new file mode 100644
index 0000000000..306483a48e
--- /dev/null
+++ b/saleor/dashboard/graphql/utils.py
@@ -0,0 +1,13 @@
+import graphene
+
+
+def get_model_name(model):
+ model_name = model.__name__
+ return model_name[:1].lower() + model_name[1:]
+
+
+def get_model_type_and_fields(registry, model, return_field_name):
+ model_type = registry.get_type_for_model(model)
+ # get mutation output field for model instance
+ fields = {return_field_name: graphene.Field(model_type)}
+ return model_type, fields