File tree 2 files changed +48
-1
lines changed
2 files changed +48
-1
lines changed Original file line number Diff line number Diff line change @@ -82,6 +82,33 @@ def parse_literal(ast):
82
82
return num
83
83
84
84
85
+ class BigInt (Scalar ):
86
+ """
87
+ The `BigInt` scalar type represents non-fractional whole numeric values.
88
+ `BigInt` is not constrained to 32-bit like the `Int` type and thus is a less
89
+ compatible type.
90
+ """
91
+
92
+ @staticmethod
93
+ def coerce_int (value ):
94
+ try :
95
+ num = int (value )
96
+ except ValueError :
97
+ try :
98
+ num = int (float (value ))
99
+ except ValueError :
100
+ return None
101
+ return num
102
+
103
+ serialize = coerce_int
104
+ parse_value = coerce_int
105
+
106
+ @staticmethod
107
+ def parse_literal (ast ):
108
+ if isinstance (ast , IntValueNode ):
109
+ return int (ast .value )
110
+
111
+
85
112
class Float (Scalar ):
86
113
"""
87
114
The `Float` scalar type represents signed double-precision fractional
Original file line number Diff line number Diff line change 1
- from ..scalars import Scalar
1
+ from ..scalars import Scalar , Int , BigInt
2
+ from graphql .language .ast import IntValueNode
2
3
3
4
4
5
def test_scalar ():
@@ -7,3 +8,22 @@ class JSONScalar(Scalar):
7
8
8
9
assert JSONScalar ._meta .name == "JSONScalar"
9
10
assert JSONScalar ._meta .description == "Documentation"
11
+
12
+
13
+ def test_ints ():
14
+ assert Int .parse_value (2 ** 31 - 1 ) is not None
15
+ assert Int .parse_value ("2.0" ) is not None
16
+ assert Int .parse_value (2 ** 31 ) is None
17
+
18
+ assert Int .parse_literal (IntValueNode (value = str (2 ** 31 - 1 ))) == 2 ** 31 - 1
19
+ assert Int .parse_literal (IntValueNode (value = str (2 ** 31 ))) is None
20
+
21
+ assert Int .parse_value (- (2 ** 31 )) is not None
22
+ assert Int .parse_value (- (2 ** 31 ) - 1 ) is None
23
+
24
+ assert BigInt .parse_value (2 ** 31 ) is not None
25
+ assert BigInt .parse_value ("2.0" ) is not None
26
+ assert BigInt .parse_value (- (2 ** 31 ) - 1 ) is not None
27
+
28
+ assert BigInt .parse_literal (IntValueNode (value = str (2 ** 31 - 1 ))) == 2 ** 31 - 1
29
+ assert BigInt .parse_literal (IntValueNode (value = str (2 ** 31 ))) == 2 ** 31
You can’t perform that action at this time.
0 commit comments