File tree Expand file tree Collapse file tree 2 files changed +48
-1
lines changed Expand file tree Collapse file tree 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):
8282 return num
8383
8484
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+
85112class Float (Scalar ):
86113 """
87114 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
23
34
45def test_scalar ():
@@ -7,3 +8,22 @@ class JSONScalar(Scalar):
78
89 assert JSONScalar ._meta .name == "JSONScalar"
910 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