allow Decimal type to return None for empty string

This commit is contained in:
Austin Snoeyink 2021-08-25 10:06:43 -04:00
parent f039af2810
commit 3d61af85e9
2 changed files with 10 additions and 1 deletions

View File

@ -28,6 +28,8 @@ class Decimal(Scalar):
@staticmethod @staticmethod
def parse_value(value): def parse_value(value):
if value == "":
return None
try: try:
return _Decimal(value) return _Decimal(value)
except ValueError: except ValueError:

View File

@ -6,7 +6,7 @@ from ..schema import Schema
class Query(ObjectType): class Query(ObjectType):
decimal = Decimal(input=Decimal()) decimal = Decimal(input=Decimal(required=False))
def resolve_decimal(self, info, input): def resolve_decimal(self, info, input):
return input return input
@ -49,3 +49,10 @@ def test_decimal_string_query_integer():
assert not result.errors assert not result.errors
assert result.data == {"decimal": str(decimal_value)} assert result.data == {"decimal": str(decimal_value)}
assert decimal.Decimal(result.data["decimal"]) == decimal_value assert decimal.Decimal(result.data["decimal"]) == decimal_value
def test_parse_decimal_empty_string():
"""Parsing an empty string should return None"""
result = schema.execute("""{ decimal(input: \"\") }""")
assert not result.errors
assert result.data == {"decimal": None}