Skip to content

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

Move from_global_id_strict_type to the GraphQL utilities

NyanKiyoshi70e7aa528fce
diff --git a/saleor/graphql/checkout/mutations.py b/saleor/graphql/checkout/mutations.py
index e6de42c8b8..648dfe83b9 100644
--- a/saleor/graphql/checkout/mutations.py
+++ b/saleor/graphql/checkout/mutations.py
@@ -24,6 +24,7 @@ from ...shipping.models import ShippingMethod as ShippingMethodModel
from ..account.i18n import I18nMixin
from ..account.types import AddressInput, User
from ..core.mutations import BaseMutation, ModelMutation
+from ..core.utils import from_global_id_strict_type
from ..order.types import Order
from ..product.types import ProductVariant
from ..shipping.types import ShippingMethod
@@ -421,7 +422,7 @@ class CheckoutShippingMethodUpdate(BaseMutation):
@classmethod
def perform_mutation(cls, _root, info, checkout_id, shipping_method_id):
- checkout_id = cls.from_global_id_strict_type(
+ checkout_id = from_global_id_strict_type(
info, checkout_id, only_type=Checkout, field='checkout_id')
checkout = models.Checkout.objects.prefetch_related(
'lines__variant__product__collections').get(pk=checkout_id)
diff --git a/saleor/graphql/core/mutations.py b/saleor/graphql/core/mutations.py
index c58021ac27..829d4b1cf9 100644
--- a/saleor/graphql/core/mutations.py
+++ b/saleor/graphql/core/mutations.py
@@ -122,17 +122,6 @@ class BaseMutation(graphene.Mutation):
raise ValidationError({field: str(e)})
return instances
- @classmethod
- def from_global_id_strict_type(
- cls, info, global_id, only_type, field='id'):
- """Resolve a node global id with a strict given type required."""
- _type, _id = graphene.Node.from_global_id(global_id)
- graphene_type = info.schema.get_type(_type).graphene_type
- if graphene_type != only_type:
- raise ValidationError({
- field: "Couldn't resolve to a node: %s" % global_id})
- return _id
-
@classmethod
def clean_instance(cls, instance):
"""Clean the instance that was created using the input data.
diff --git a/saleor/graphql/core/utils.py b/saleor/graphql/core/utils.py
index 8bdb906aa0..11c7103e0d 100644
--- a/saleor/graphql/core/utils.py
+++ b/saleor/graphql/core/utils.py
@@ -1,3 +1,4 @@
+import graphene
from django.core.exceptions import ValidationError
@@ -26,3 +27,13 @@ def validate_image_file(file, field_name):
"""Validate if the file is an image."""
if not file.content_type.startswith('image/'):
raise ValidationError({field_name: 'Invalid file type'})
+
+
+def from_global_id_strict_type(info, global_id, only_type, field='id'):
+ """Resolve a node global id with a strict given type required."""
+ _type, _id = graphene.Node.from_global_id(global_id)
+ graphene_type = info.schema.get_type(_type).graphene_type
+ if graphene_type != only_type:
+ raise ValidationError({
+ field: "Couldn't resolve to a node: %s" % global_id})
+ return _id
diff --git a/saleor/graphql/payment/mutations.py b/saleor/graphql/payment/mutations.py
index dfdb964254..d7cc820b9c 100644
--- a/saleor/graphql/payment/mutations.py
+++ b/saleor/graphql/payment/mutations.py
@@ -14,6 +14,7 @@ from ..account.types import AddressInput
from ..checkout.types import Checkout
from ..core.mutations import BaseMutation
from ..core.scalars import Decimal
+from ..core.utils import from_global_id_strict_type
from .enums import PaymentGatewayEnum
from .types import Payment
@@ -54,7 +55,7 @@ class CheckoutPaymentCreate(BaseMutation, I18nMixin):
@classmethod
def perform_mutation(cls, _root, info, checkout_id, **data):
- checkout_id = cls.from_global_id_strict_type(
+ checkout_id = from_global_id_strict_type(
info, checkout_id, only_type=Checkout, field='checkout_id')
checkout = models.Checkout.objects.prefetch_related(
'lines__variant__product__collections').get(pk=checkout_id)