graphene django将models.BigInteger()转换为graphene.Integer()

graphene django将models.BigInteger()转换为graphene.Integer(),django,graphql,graphene-python,Django,Graphql,Graphene Python,我用的是石墨烯django 我试图从models.biginger字段中检索数据,但在graphiQL中进行查询时,出现了错误 { "errors": [ { "message": "Int cannot represent non 32-bit signed integer value: 2554208328" } ], "data": { "match": null } } 有人知道我如何强迫石墨烯提供数据吗?我最终使用了自定义标量。如果

我用的是石墨烯django

我试图从models.biginger字段中检索数据,但在graphiQL中进行查询时,出现了错误

{
  "errors": [
    {
      "message": "Int cannot represent non 32-bit signed integer value: 2554208328"
    }
  ],
  "data": {
    "match": null
  }
}

有人知道我如何强迫石墨烯提供数据吗?

我最终使用了自定义标量。如果它能在graphene django中自动转换成更好的标量,那就更好了,但下面是我如何解决这个问题的。我用自定义的BigInt标量编写了一个converter.py文件,如果我们有一个大于MAX_int的数字,它使用浮点而不是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

我最终使用了一个自定义标量。如果它能在graphene django中自动转换成更好的标量,那就更好了,但下面是我如何解决这个问题的。我用自定义的BigInt标量编写了一个converter.py文件,如果我们有一个大于MAX_int的数字,它使用浮点而不是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

您可以注册新的转换器:

    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本身使用的方式。

您可以注册新转换器:

    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本身使用的方式