Skip to content

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

Return GraphQL error when invalid value is passed as Decimal

Marcin Gębala5b0f0153d0f9
diff --git a/saleor/graphql/core/types/common.py b/saleor/graphql/core/types/common.py
index 4cf8fe368d..5348338bce 100644
--- a/saleor/graphql/core/types/common.py
+++ b/saleor/graphql/core/types/common.py
@@ -8,25 +8,33 @@ from graphql.language import ast
from ..connection import CountableConnection
-# FIXME: not yet merged https://github.com/graphql-python/graphene/pull/726
+# FIXME: Decimal scalar implementation has been taken from Graphene repo.
+# Remove it when a new version including this implementation is released.
class Decimal(Scalar):
- """The `Decimal` scalar type represents a python Decimal."""
+ """
+ The `Decimal` scalar type represents a python Decimal.
+ """
@staticmethod
def serialize(dec):
- assert isinstance(dec, decimal.Decimal), (
- 'Received not compatible Decimal "{}"'.format(repr(dec)))
+ if isinstance(dec, str):
+ dec = decimal.Decimal(dec)
+ assert isinstance(dec, decimal.Decimal), 'Received not compatible Decimal "{}"'.format(
+ repr(dec))
return str(dec)
- @staticmethod
- def parse_value(value):
- return decimal.Decimal(value)
-
@classmethod
def parse_literal(cls, node):
if isinstance(node, ast.StringValue):
return cls.parse_value(node.value)
+ @staticmethod
+ def parse_value(value):
+ try:
+ return decimal.Decimal(value)
+ except decimal.DecimalException:
+ return None
+
class CountryDisplay(graphene.ObjectType):
code = graphene.String(description='Country code.', required=True)