Skip to content

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

Performance fix for input_type resolver on AttributeValue (#7396)

Tomasz Szymański2c573fa90db9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 62f4ac0c59..21c94b90f9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -100,6 +100,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Fix invoice generation - #7376 by @tomaszszymanski129
- Allow defining only one field in translations - #7363 by @IKarbowiak
- Trigger `checkout_updated` hook for checkout meta mutations - #7392 by @maarcingebala
+- Optimize `inputType` resolver on `AttributeValue` type - 7396 by @tomaszszymanski129
- Allow filtering pages by ids - #7393 by @IKarbowiak
### Breaking
diff --git a/saleor/graphql/attribute/types.py b/saleor/graphql/attribute/types.py
index 7c6d2a9dfe..d82b88d5f9 100644
--- a/saleor/graphql/attribute/types.py
+++ b/saleor/graphql/attribute/types.py
@@ -2,16 +2,16 @@ import re
import graphene
-from ...attribute import AttributeInputType, models
+from ...attribute import AttributeInputType, AttributeType, models
+from ...core.exceptions import PermissionDenied
+from ...core.permissions import PagePermissions, ProductPermissions
from ...core.tracing import traced_resolver
+from ...graphql.utils import get_user_or_app_from_context
from ..core.connection import CountableDjangoObjectType
from ..core.enums import MeasurementUnitsEnum
from ..core.types import File
from ..core.types.common import IntRangeInput
-from ..decorators import (
- check_attribute_required_permissions,
- check_attribute_value_required_permissions,
-)
+from ..decorators import check_attribute_required_permissions
from ..meta.types import ObjectWithMetadata
from ..translations.fields import TranslationField
from ..translations.types import AttributeTranslation, AttributeValueTranslation
@@ -46,10 +46,22 @@ class AttributeValue(CountableDjangoObjectType):
model = models.AttributeValue
@staticmethod
- @traced_resolver
- @check_attribute_value_required_permissions()
- def resolve_input_type(root: models.AttributeValue, *_args):
- return root.input_type
+ def resolve_input_type(root: models.AttributeValue, info, *_args):
+ def _resolve_input_type(attribute):
+ requester = get_user_or_app_from_context(info.context)
+ if attribute.type == AttributeType.PAGE_TYPE:
+ if requester.has_perm(PagePermissions.MANAGE_PAGES):
+ return attribute.input_type
+ raise PermissionDenied()
+ elif requester.has_perm(ProductPermissions.MANAGE_PRODUCTS):
+ return attribute.input_type
+ raise PermissionDenied()
+
+ return (
+ AttributesByAttributeId(info.context)
+ .load(root.attribute_id)
+ .then(_resolve_input_type)
+ )
@staticmethod
@traced_resolver
diff --git a/saleor/graphql/decorators.py b/saleor/graphql/decorators.py
index e0764921a1..55a85b1f89 100644
--- a/saleor/graphql/decorators.py
+++ b/saleor/graphql/decorators.py
@@ -110,18 +110,3 @@ def check_attribute_required_permissions():
return _permission_required((ProductPermissions.MANAGE_PRODUCTS,), context)
return account_passes_test_for_attribute(check_perms)
-
-
-def check_attribute_value_required_permissions():
- """Check attribute value permissions depending on the corresponding attribute type.
-
- As an value's attribute can belong to the product or to the page,
- different permissions need to be checked.
- """
-
- def check_perms(context, attribute_value):
- if attribute_value.attribute.type == AttributeType.PAGE_TYPE:
- return _permission_required((PagePermissions.MANAGE_PAGES,), context)
- return _permission_required((ProductPermissions.MANAGE_PRODUCTS,), context)
-
- return account_passes_test_for_attribute(check_perms)