fix(Decimal): parse integers as decimal. (#1295)

This commit is contained in:
Jason Kraus 2021-01-06 01:54:45 -08:00 committed by GitHub
parent e0d4bec2d8
commit e5eeb9d831
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 3 deletions

View File

@ -2,7 +2,7 @@ from __future__ import absolute_import
from decimal import Decimal as _Decimal
from graphql.language.ast import StringValueNode
from graphql.language.ast import StringValueNode, IntValueNode
from .scalars import Scalar
@ -23,7 +23,7 @@ class Decimal(Scalar):
@classmethod
def parse_literal(cls, node):
if isinstance(node, StringValueNode):
if isinstance(node, (StringValueNode, IntValueNode)):
return cls.parse_value(node.value)
@staticmethod

View File

@ -41,3 +41,11 @@ def test_bad_decimal_query():
result = schema.execute("""{ decimal(input: "%s") }""" % not_a_decimal)
assert len(result.errors) == 1
assert result.data is None
def test_decimal_string_query_integer():
decimal_value = 1
result = schema.execute("""{ decimal(input: %s) }""" % decimal_value)
assert not result.errors
assert result.data == {"decimal": str(decimal_value)}
assert decimal.Decimal(result.data["decimal"]) == decimal_value

View File

@ -8,7 +8,7 @@ deps =
setenv =
PYTHONPATH = .:{envdir}
commands =
py{36,37}: pytest --cov=graphene graphene examples {posargs}
py{36,37,38}: pytest --cov=graphene graphene examples {posargs}
[testenv:pre-commit]
basepython=python3.7