Skip to content

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

Add ProductType update mutation

akjanik67bab04f9353
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 1f8307e3e8..b5d7059510 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -12,7 +12,7 @@ from .product.filters import ProductFilterSet
from .product.mutations import (
CategoryCreateMutation, CategoryDelete, CategoryUpdateMutation,
ProductCreateMutation, ProductDeleteMutation, ProductUpdateMutation,
- ProductTypeCreateMutation)
+ ProductTypeCreateMutation, ProductTypeUpdateMutation)
from .product.resolvers import (
resolve_attributes, resolve_categories, resolve_products,
resolve_product_types)
@@ -103,6 +103,7 @@ class Mutations(graphene.ObjectType):
product_update = ProductUpdateMutation.Field()
product_type_create = ProductTypeCreateMutation.Field()
+ product_type_update = ProductTypeUpdateMutation.Field()
schema = graphene.Schema(Query, Mutations)
diff --git a/saleor/graphql/product/mutations.py b/saleor/graphql/product/mutations.py
index 1f585d3541..f2e457f8b4 100644
--- a/saleor/graphql/product/mutations.py
+++ b/saleor/graphql/product/mutations.py
@@ -167,33 +167,62 @@ class ProductUpdateMutation(
kwargs['data']['category'] = instance.category.id
return kwargs
-class AttributeID(InputObjectType):
- id = graphene.ID()
class ProductTypeCreateMutation(StaffMemberRequiredMixin, ModelFormMutation):
permissions = 'product.edit_properties'
- # class Arguments:
- # product_attributes = graphene.List(graphene.ID)
-
class Meta:
description = 'Creates a new product type.'
form_class = ProductTypeForm
- # exclude = ['product_attributes']
@classmethod
def get_form_kwargs(cls, root, info, **input):
product_attributes = input.pop('product_attributes', None)
- product_attributes = {
- get_node(info, pr_att_id, only_type=ProductAttribute)
- for pr_att_id in product_attributes}
+ if product_attributes:
+ product_attributes = {
+ get_node(info, pr_att_id, only_type=ProductAttribute)
+ for pr_att_id in product_attributes}
variant_attributes = input.pop('variant_attributes', None)
- variant_attributes = {
- get_node(info, pr_att_id, only_type=ProductAttribute)
- for pr_att_id in variant_attributes}
+ if variant_attributes:
+ variant_attributes = {
+ get_node(info, pr_att_id, only_type=ProductAttribute)
+ for pr_att_id in variant_attributes}
+
+ kwargs = super().get_form_kwargs(root, info, **input)
+ kwargs['data']['product_attributes'] = product_attributes
+ kwargs['data']['variant_attributes'] = variant_attributes
+ return kwargs
+
+class ProductTypeUpdateMutation(
+ StaffMemberRequiredMixin, ModelFormUpdateMutation):
+ permissions = 'product.edit_properties'
+
+ class Meta:
+ description = 'Update an existing product type.'
+ form_class = ProductTypeForm
+ @classmethod
+ def get_form_kwargs(cls, root, info, **input):
kwargs = super().get_form_kwargs(root, info, **input)
+ product_attributes = input.pop('product_attributes', None)
+ if product_attributes is not None:
+ product_attributes = {
+ get_node(info, pr_att_id, only_type=ProductAttribute)
+ for pr_att_id in product_attributes}
+ else:
+ product_attributes = kwargs.get(
+ 'instance').product_attributes.all()
kwargs['data']['product_attributes'] = product_attributes
+
+ variant_attributes = input.pop('variant_attributes', None)
+ if variant_attributes is not None:
+ variant_attributes = {
+ get_node(info, pr_att_id, only_type=ProductAttribute)
+ for pr_att_id in variant_attributes}
+ else:
+ variant_attributes = kwargs.get(
+ 'instance').variant_attributes.all()
kwargs['data']['variant_attributes'] = variant_attributes
+
return kwargs