我用的是石墨烯-姜戈。
我试图从models.BigInteger()字段中检索数据,但是当我在graphiQL中进行查询时,我得到了以下错误
{
"errors": [
{
"message": "Int cannot represent non 32-bit signed integer value: 2554208328"
}
],
"data": {
"match": null
}
}有人知道我怎样才能强迫石墨烯给我数据吗?
发布于 2017-08-12 00:48:38
我最终使用了一个自定义标量。如果在graphene-django中自动转换为更好的标量会更好,但下面是我如何解决这个问题的。我用定制的BigInt标量编写了一个converter.py文件,如果我们有一个大于MAX_INT的数字,它将使用浮点型而不是整型
# converter.py
from graphene.types import Scalar
from graphql.language import ast
from graphene.types.scalars import MIN_INT, MAX_INT
class BigInt(Scalar):
"""
BigInt is an extension of the regular Int field
that supports Integers bigger than a signed
32-bit integer.
"""
@staticmethod
def big_to_float(value):
num = int(value)
if num > MAX_INT or num < MIN_INT:
return float(int(num))
return num
serialize = big_to_float
parse_value = big_to_float
@staticmethod
def parse_literal(node):
if isinstance(node, ast.IntValue):
num = int(node.value)
if num > MAX_INT or num < MIN_INT:
return float(int(num))
return num然后
# schema.py
from .converter import BigInt
class MatchType(DjangoObjectType):
game_id = graphene.Field(BigInt)
class Meta:
model = Match
interfaces = (graphene.Node, )
filter_fields = {}发布于 2018-08-05 21:29:33
您可以注册新的转换器:
import graphene
from graphene_django.converter import convert_django_field
@convert_django_field.register(models.BigIntegerField)
def convert_bigint_to_float(field, registry=None):
return graphene.Float(description=field.help_text, required=not field.null)这就是它在石墨烯中的使用方式-django本身。
发布于 2021-06-29 17:36:42
这是来自石墨烯的源代码(但我猜还没有发布):https://github.com/graphql-python/graphene/blob/485b1ed325287fd721b13aac8b4ec872d6295c6a/graphene/types/scalars.py#L85
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)我认为另一个答案中的代码实际上可能更好,但我想链接这个以防万一
https://stackoverflow.com/questions/45624042
复制相似问题