Skip to content

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

Add category mutations

Marcin Gębala64d28e6355a9
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index a2b929ee4a..0ecd330820 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -38,4 +38,10 @@ class Query(graphene.ObjectType):
return resolve_attributes(in_category, info)
-schema = graphene.Schema(Query)
+class Mutations(graphene.ObjectType):
+ category_create = CategoryCreate.Field()
+ category_delete = CategoryDelete.Field()
+ category_update = CategoryUpdate.Field()
+
+
+schema = graphene.Schema(Query, Mutations)
diff --git a/saleor/graphql/product/mutations.py b/saleor/graphql/product/mutations.py
new file mode 100644
index 0000000000..27a040a74b
--- /dev/null
+++ b/saleor/graphql/product/mutations.py
@@ -0,0 +1,73 @@
+import graphene
+
+from ...dashboard.category.forms import CategoryForm
+from ...product.models import Category
+from .types import CategoryType
+
+
+class Mutation(graphene.Mutation):
+ errors = graphene.List(graphene.String)
+
+ def mutate(self, info):
+ raise NotImplementedError
+
+
+class CategoryCreateInput(graphene.InputObjectType):
+ name = graphene.String()
+ description = graphene.String()
+ parent = graphene.Int()
+
+
+class CategoryCreate(Mutation):
+ category = graphene.Field(CategoryType)
+
+ class Arguments:
+ input = CategoryCreateInput()
+
+ def mutate(self, info, input):
+ category = Category()
+ form = CategoryForm(input, instance=category, parent_pk=input.parent)
+ if form.is_valid():
+ category = form.save()
+ errors = []
+ else:
+ errors = form.errors
+ return CategoryCreate(category=category, errors=errors)
+
+
+class CategoryUpdate(Mutation):
+ category = graphene.Field(CategoryType)
+
+ class Arguments:
+ input = CategoryCreateInput()
+ pk = graphene.Int()
+
+ def mutate(self, info, input, pk):
+ try:
+ category = Category.objects.get(pk=pk)
+ except Category.DoesNotExist:
+ errors = ['not-found']
+ category = None
+ else:
+ form = CategoryForm(input, instance=category, parent_pk=input.parent)
+ if form.is_valid():
+ category = form.save()
+ errors = []
+ else:
+ errors = form.errors
+ return CategoryUpdate(category=category, errors=errors)
+
+
+class CategoryDelete(Mutation):
+ class Arguments:
+ pk = graphene.Int()
+
+ def mutate(self, info, pk):
+ try:
+ category = Category.objects.get(pk=pk)
+ except Category.DoesNotExist:
+ errors = ['not-found']
+ else:
+ category.delete()
+ errors = []
+ return CategoryDelete(errors=errors)