Skip to content

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

Use default ProductTypeKind.NORMAL in productTypeCreate mutation (#9207)

Marcin Gębala77e8a3db94d0
diff --git a/saleor/graphql/product/mutations/products.py b/saleor/graphql/product/mutations/products.py
index 4fdf66a066..10992c015d 100644
--- a/saleor/graphql/product/mutations/products.py
+++ b/saleor/graphql/product/mutations/products.py
@@ -19,7 +19,7 @@ from ....order import OrderStatus
from ....order import events as order_events
from ....order import models as order_models
from ....order.tasks import recalculate_orders_task
-from ....product import ProductMediaTypes, models
+from ....product import ProductMediaTypes, ProductTypeKind, models
from ....product.error_codes import CollectionErrorCode, ProductErrorCode
from ....product.search import (
update_product_search_document,
@@ -1223,9 +1223,18 @@ class ProductTypeCreate(ModelMutation):
error_type_class = ProductError
error_type_field = "product_errors"
+ @classmethod
+ def clean_product_kind(cls, _instance, data):
+ # Fallback to ProductTypeKind.NORMAL if kind is not provided in the input.
+ # This method can be dropped when we separate inputs for `productTypeCreate`
+ # and `productTypeUpdate` - now they reuse the input class and all fields are
+ # optional, while `kind` is required in the model.
+ return data.get("kind") or ProductTypeKind.NORMAL
+
@classmethod
def clean_input(cls, info, instance, data):
cleaned_input = super().clean_input(info, instance, data)
+ cleaned_input["kind"] = cls.clean_product_kind(instance, cleaned_input)
weight = cleaned_input.get("weight")
if weight and weight.value < 0:
@@ -1301,6 +1310,10 @@ class ProductTypeUpdate(ProductTypeCreate):
error_type_class = ProductError
error_type_field = "product_errors"
+ @classmethod
+ def clean_product_kind(cls, instance, data):
+ return data.get("kind", instance.kind)
+
@classmethod
def save(cls, info, instance, cleaned_input):
variant_attr = cleaned_input.get("variant_attributes")
diff --git a/saleor/graphql/product/tests/test_product.py b/saleor/graphql/product/tests/test_product.py
index fee7399557..3d705e00e9 100644
--- a/saleor/graphql/product/tests/test_product.py
+++ b/saleor/graphql/product/tests/test_product.py
@@ -8244,6 +8244,22 @@ def test_product_type_create_mutation(
assert tax_code == "wine"
+def test_product_type_create_mutation_optional_kind(
+ staff_api_client, permission_manage_product_types_and_attributes
+):
+ variables = {"name": "Default Kind Test"}
+ response = staff_api_client.post_graphql(
+ PRODUCT_TYPE_CREATE_MUTATION,
+ variables,
+ permissions=[permission_manage_product_types_and_attributes],
+ )
+ content = get_graphql_content(response)
+ assert (
+ content["data"]["productTypeCreate"]["productType"]["kind"]
+ == ProductTypeKindEnum.NORMAL.name
+ )
+
+
def test_create_gift_card_product_type(
staff_api_client,
product_type,
@@ -8940,19 +8956,14 @@ def test_update_product_type_with_negative_weight(
assert error["code"] == ProductErrorCode.INVALID.name
-def test_update_product_type_type(
+def test_update_product_type_kind(
staff_api_client,
product_type,
permission_manage_product_types_and_attributes,
):
query = """
mutation($id: ID!, $kind: ProductTypeKindEnum) {
- productTypeUpdate(
- id: $id
- input: {
- kind: $kind
- }
- ) {
+ productTypeUpdate(id: $id, input: { kind: $kind }) {
productType{
name
kind
@@ -8972,7 +8983,9 @@ def test_update_product_type_type(
variables = {"kind": kind, "id": node_id}
response = staff_api_client.post_graphql(
- query, variables, permissions=[permission_manage_product_types_and_attributes]
+ query,
+ variables,
+ permissions=[permission_manage_product_types_and_attributes],
)
content = get_graphql_content(response)
data = content["data"]["productTypeUpdate"]
@@ -8981,6 +8994,44 @@ def test_update_product_type_type(
assert data["productType"]["kind"] == kind
+def test_update_product_type_kind_omitted(
+ staff_api_client,
+ product_type,
+ permission_manage_product_types_and_attributes,
+):
+ query = """
+ mutation($id: ID!, $name: String) {
+ productTypeUpdate(id: $id, input: { name: $name }) {
+ productType{
+ name
+ kind
+ }
+ errors {
+ field
+ message
+ code
+ }
+ }
+ }
+ """
+ assert product_type.kind == ProductTypeKindEnum.NORMAL.value
+ name = "New name"
+ node_id = graphene.Node.to_global_id("ProductType", product_type.id)
+ variables = {"id": node_id, "name": name}
+
+ response = staff_api_client.post_graphql(
+ query,
+ variables,
+ permissions=[permission_manage_product_types_and_attributes],
+ )
+ content = get_graphql_content(response)
+ data = content["data"]["productTypeUpdate"]
+ errors = data["errors"]
+ assert not errors
+ assert data["productType"]["kind"] == ProductTypeKindEnum.NORMAL.name
+ assert data["productType"]["name"] == name
+
+
PRODUCT_TYPE_DELETE_MUTATION = """
mutation deleteProductType($id: ID!) {
productTypeDelete(id: $id) {