Skip to content

Commit d728b84

Browse files
authored
Merge pull request #726 from picturedots/issue-703
Issue 703 -- support for Decimal type
2 parents d28dc68 + 00cc978 commit d728b84

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

graphene/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
Date,
1919
DateTime,
2020
Time,
21+
Decimal,
2122
JSONString,
2223
UUID,
2324
List,
@@ -65,6 +66,7 @@
6566
"Date",
6667
"DateTime",
6768
"Time",
69+
"Decimal",
6870
"JSONString",
6971
"UUID",
7072
"List",

graphene/types/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .mutation import Mutation
77
from .scalars import Scalar, String, ID, Int, Float, Boolean
88
from .datetime import Date, DateTime, Time
9+
from .decimal import Decimal
910
from .json import JSONString
1011
from .uuid import UUID
1112
from .schema import Schema
@@ -40,6 +41,7 @@
4041
"Date",
4142
"DateTime",
4243
"Time",
44+
"Decimal",
4345
"JSONString",
4446
"UUID",
4547
"Boolean",

graphene/types/decimal.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from __future__ import absolute_import
2+
3+
from decimal import Decimal as _Decimal
4+
5+
from graphql.language import ast
6+
7+
from .scalars import Scalar
8+
9+
10+
class Decimal(Scalar):
11+
"""
12+
The `Decimal` scalar type represents a python Decimal.
13+
"""
14+
15+
@staticmethod
16+
def serialize(dec):
17+
if isinstance(dec, str):
18+
dec = _Decimal(dec)
19+
assert isinstance(dec, _Decimal), 'Received not compatible Decimal "{}"'.format(
20+
repr(dec)
21+
)
22+
return str(dec)
23+
24+
@classmethod
25+
def parse_literal(cls, node):
26+
if isinstance(node, ast.StringValue):
27+
return cls.parse_value(node.value)
28+
29+
@staticmethod
30+
def parse_value(value):
31+
try:
32+
return _Decimal(value)
33+
except ValueError:
34+
return None

graphene/types/tests/test_decimal.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import decimal
2+
3+
from ..decimal import Decimal
4+
from ..objecttype import ObjectType
5+
from ..schema import Schema
6+
7+
8+
class Query(ObjectType):
9+
decimal = Decimal(input=Decimal())
10+
11+
def resolve_decimal(self, info, input):
12+
return input
13+
14+
15+
schema = Schema(query=Query)
16+
17+
18+
def test_decimal_string_query():
19+
decimal_value = decimal.Decimal("1969.1974")
20+
result = schema.execute("""{ decimal(input: "%s") }""" % decimal_value)
21+
assert not result.errors
22+
assert result.data == {"decimal": str(decimal_value)}
23+
assert decimal.Decimal(result.data["decimal"]) == decimal_value
24+
25+
26+
def test_decimal_string_query_variable():
27+
decimal_value = decimal.Decimal("1969.1974")
28+
29+
result = schema.execute(
30+
"""query Test($decimal: Decimal){ decimal(input: $decimal) }""",
31+
variable_values={"decimal": decimal_value},
32+
)
33+
assert not result.errors
34+
assert result.data == {"decimal": str(decimal_value)}
35+
assert decimal.Decimal(result.data["decimal"]) == decimal_value
36+
37+
38+
def test_bad_decimal_query():
39+
not_a_decimal = "Nobody expects the Spanish Inquisition!"
40+
41+
result = schema.execute("""{ decimal(input: "%s") }""" % not_a_decimal)
42+
assert len(result.errors) == 1
43+
assert result.data is None

0 commit comments

Comments
 (0)