Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Merge branch 'master' of github.com:mirumee/saleor into SALEOR-2542-error-decode-id
Damian W47294a3adc14
diff --cc saleor/graphql/account/resolvers.pyindex 1c179cd0cd,4c4338f98c..bc37d801ed--- a/saleor/graphql/account/resolvers.py+++ b/saleor/graphql/account/resolvers.py@@@ -9,7 -10,6 +9,7 @@@ from ...core.exceptions import Permissifrom ...core.permissions import AccountPermissionsfrom ...payment import gatewayfrom ...payment.utils import fetch_customer_id- from ..core.utils import from_global_id_strict_type++from ..core.utils import from_global_id_or_errorfrom ..utils import format_permissions_for_display, get_user_or_app_from_contextfrom ..utils.filters import filter_by_query_paramfrom .types import AddressValidationData, ChoiceValue@@@ -55,7 -55,7 +55,7 @@@ def resolve_user(info, id=None, email=Nif requester:filter_kwargs = {}if id:- filter_kwargs["pk"] = from_global_id_strict_type(id, only_type="User")- _model, filter_kwargs["pk"] = graphene.Node.from_global_id(id)++ _model, filter_kwargs["pk"] = from_global_id_or_error(id)if email:filter_kwargs["email"] = emailif requester.has_perms(@@@ -151,7 -151,7 +151,7 @@@ def prepare_graphql_payment_sources_typdef resolve_address(info, id):user = info.context.userapp = info.context.app- address_pk = from_global_id_strict_type(id, only_type="Address")- _model, address_pk = graphene.Node.from_global_id(id)++ _model, address_pk = from_global_id_or_error(id)if app and app.has_perm(AccountPermissions.MANAGE_USERS):return models.Address.objects.filter(pk=address_pk).first()if user and not user.is_anonymous:diff --cc saleor/graphql/core/utils/__init__.pyindex cd25a3895e,201edcf482..f9c33f366d--- a/saleor/graphql/core/utils/__init__.py+++ b/saleor/graphql/core/utils/__init__.py@@@ -119,13 -119,3 +119,24 @@@ def validate_required_string_field(cleaelse:raise ValidationError(f"{field_name.capitalize()} is required.")return cleaned_input++- def from_global_id_or_error(id):- """Resolve id from global or raise ValidationError."""++def from_global_id_or_error(++ id: str, only_type: Union[ObjectType, str] = None, field: str = "id"++):++ """Resolve id from global or raise ValidationError.++++ Optional validate object type.++ """+ try:- return graphene.Node.from_global_id(id)++ _type, _id = graphene.Node.from_global_id(id)+ except (binascii.Error, UnicodeDecodeError):+ raise ValidationError(- {"id": ValidationError(f"Couldn't resolve id: {id}.", code="not_found")}++ {field: ValidationError(f"Couldn't resolve id: {id}.", code="not_found")}++ )++++ if only_type and str(_type) != str(only_type):++ raise ValidationError(++ {field: ValidationError(f"Must receive a {only_type} id.", code="invalid")}+ )++ return _type, _iddiff --cc saleor/graphql/meta/mutations.pyindex 7a6d559147,b58b066610..a29eae4215--- a/saleor/graphql/meta/mutations.py+++ b/saleor/graphql/meta/mutations.py@@@ -12,8 -12,7 +12,8 @@@ from ...shipping import models as shippfrom ..channel import ChannelContextfrom ..core.mutations import BaseMutationfrom ..core.types.common import MetadataError+from ..core.utils import from_global_id_or_error- from .extra_methods import MODEL_EXTRA_METHODS+ from .extra_methods import MODEL_EXTRA_METHODS, MODEL_EXTRA_PREFETCHfrom .permissions import PRIVATE_META_PERMISSION_MAP, PUBLIC_META_PERMISSION_MAPfrom .types import ObjectWithMetadata@@@ -113,8 -113,11 +114,11 @@@ class BaseMetadataMutation(BaseMutation@classmethoddef perform_model_extra_actions(cls, root, info, **data):"""Run extra metadata method based on mutating model."""- type_name, _ = graphene.Node.from_global_id(data["id"])+ type_name, _ = from_global_id_or_error(data["id"])if MODEL_EXTRA_METHODS.get(type_name):+ prefetch_method = MODEL_EXTRA_PREFETCH.get(type_name)+ if prefetch_method:+ data["qs"] = prefetch_method()instance = cls.get_instance(info, **data)MODEL_EXTRA_METHODS[type_name](instance, info, **data)