mirror of
https://github.com/graphql-python/graphene.git
synced 2025-07-22 05:49:59 +03:00
add BigInt type
This commit is contained in:
parent
b685e109f5
commit
9e03fede1a
|
@ -82,6 +82,33 @@ class Int(Scalar):
|
|||
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
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
from ..scalars import Scalar
|
||||
from ..scalars import Scalar, Int, BigInt
|
||||
|
||||
|
||||
def test_scalar():
|
||||
|
@ -7,3 +7,14 @@ def test_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 ** 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 ** 31 - 1) is not None
|
||||
|
|
Loading…
Reference in New Issue
Block a user