Skip to content

Commit

Permalink
Merge 26adbf8 into b685e10
Browse files Browse the repository at this point in the history
  • Loading branch information
pizzapanther committed Aug 27, 2020
2 parents b685e10 + 26adbf8 commit ec2574c
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
27 changes: 27 additions & 0 deletions graphene/types/scalars.py
Expand Up @@ -82,6 +82,33 @@ def parse_literal(ast):
return num


class BigInt(Scalar):
"""
The `BigInt` scalar type represents non-fractional whole numeric values.
`BigInt` is not constrained to 32-bit like the `Int` type and thus is a less
compatible type.
"""

@staticmethod
def coerce_int(value):
try:
num = int(value)
except ValueError:
try:
num = int(float(value))
except ValueError:
return None
return num

serialize = coerce_int
parse_value = coerce_int

@staticmethod
def parse_literal(ast):
if isinstance(ast, IntValueNode):
return int(ast.value)


class Float(Scalar):
"""
The `Float` scalar type represents signed double-precision fractional
Expand Down
22 changes: 21 additions & 1 deletion graphene/types/tests/test_scalar.py
@@ -1,4 +1,5 @@
from ..scalars import Scalar
from ..scalars import Scalar, Int, BigInt
from graphql.language.ast import IntValueNode


def test_scalar():
Expand All @@ -7,3 +8,22 @@ class JSONScalar(Scalar):

assert JSONScalar._meta.name == "JSONScalar"
assert JSONScalar._meta.description == "Documentation"


def test_ints():
assert Int.parse_value(2 ** 31 - 1) is not None
assert Int.parse_value("2.0") is not None
assert Int.parse_value(2 ** 31) is None

assert Int.parse_literal(IntValueNode(value=str(2 ** 31 - 1))) == 2 ** 31 - 1
assert Int.parse_literal(IntValueNode(value=str(2 ** 31))) is None

assert Int.parse_value(-(2 ** 31)) is not None
assert Int.parse_value(-(2 ** 31) - 1) is None

assert BigInt.parse_value(2 ** 31) is not None
assert BigInt.parse_value("2.0") is not None
assert BigInt.parse_value(-(2 ** 31) - 1) is not None

assert BigInt.parse_literal(IntValueNode(value=str(2 ** 31 - 1))) == 2 ** 31 - 1
assert BigInt.parse_literal(IntValueNode(value=str(2 ** 31))) == 2 ** 31

0 comments on commit ec2574c

Please sign in to comment.