Skip to content

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

Add params to assign attribute errors

Krzysztof Świątek917929b10ef0
diff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.py
index 23daba2578..4b0e24e247 100644
--- a/saleor/graphql/core/types/common.py
+++ b/saleor/graphql/core/types/common.py
@@ -73,6 +73,14 @@ class ProductError(Error):
code = ProductErrorCode(description="The error code.", required=True)
+class ProductAttributeError(ProductError):
+ attributes = graphene.List(
+ graphene.NonNull(graphene.ID),
+ description="List of attributes IDs which causes the error.",
+ required=False,
+ )
+
+
class BulkProductError(ProductError):
index = graphene.Int(
description="Index of an input list item that caused the error."
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.py
index 69e93d5554..daf8a1f6e9 100644
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@ -10,7 +10,7 @@ from ....core.permissions import ProductPermissions
from ....product import AttributeInputType, models
from ....product.error_codes import ProductErrorCode
from ...core.mutations import BaseMutation, ModelDeleteMutation, ModelMutation
-from ...core.types.common import ProductError
+from ...core.types.common import ProductAttributeError, ProductError
from ...core.utils import (
from_global_id_strict_type,
validate_slug_and_generate_if_needed,
@@ -300,7 +300,7 @@ class AttributeAssign(BaseMutation):
class Meta:
description = "Assign attributes to a given product type."
- error_type_class = ProductError
+ error_type_class = ProductAttributeError
error_type_field = "product_errors"
@classmethod
@@ -382,11 +382,27 @@ class AttributeAssign(BaseMutation):
}
)
+ @classmethod
+ def handle_typed_errors(cls, errors: list, **extra):
+ typed_errors = [
+ cls._meta.error_type_class( # type: ignore
+ field=e.field,
+ message=e.message,
+ code=code,
+ attributes=params.get("attributes") if params else None,
+ )
+ for e, code, params in errors
+ ]
+ extra.update({cls._meta.error_type_field: typed_errors}) # type: ignore
+ return cls(errors=[e[0] for e in errors], **extra) # type: ignore
+
@classmethod
def clean_operations(cls, product_type, product_attrs_pks, variant_attrs_pks):
"""Ensure the attributes are not already assigned to the product type."""
attrs_pk = product_attrs_pks + variant_attrs_pks
- attributes = models.Attribute.objects.filter(id__in=attrs_pk).values_list("pk", flat=True)
+ attributes = models.Attribute.objects.filter(id__in=attrs_pk).values_list(
+ "pk", flat=True
+ )
if len(attrs_pk) != len(attributes):
invalid_attrs = set(attrs_pk) - set(attributes)
raise ValidationError(
@@ -394,7 +410,7 @@ class AttributeAssign(BaseMutation):
"operations": ValidationError(
f"Attribute doesn't exist.",
code=ProductErrorCode.NOT_FOUND.value,
- params={"attributes": list(invalid_attrs)}
+ params={"attributes": list(invalid_attrs)},
)
}
)
diff --git a/saleor/graphql/product/resolvers.py b/saleor/graphql/product/resolvers.py
index e48fc6f61f..e82145826c 100644
--- a/saleor/graphql/product/resolvers.py
+++ b/saleor/graphql/product/resolvers.py
@@ -13,8 +13,8 @@ from ..utils import (
filter_by_period,
filter_by_query_param,
get_database_id,
- get_user_or_service_account_from_context,
get_nodes,
+ get_user_or_service_account_from_context,
sort_queryset,
)
from .filters import (
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 64edfb5a78..6de0ee7f52 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -214,7 +214,7 @@ type Attribute implements Node & ObjectWithMetadata {
type AttributeAssign {
errors: [Error!]!
productType: ProductType
- productErrors: [ProductError!]!
+ productErrors: [ProductAttributeError!]!
}
input AttributeAssignInput {
@@ -3174,6 +3174,13 @@ type Product implements Node & ObjectWithMetadata {
translation(languageCode: LanguageCodeEnum!): ProductTranslation
}
+type ProductAttributeError {
+ field: String
+ message: String
+ code: ProductErrorCode!
+ attributes: [ID!]
+}
+
type ProductBulkDelete {
errors: [Error!]!
count: Int!