2017-08-10 1 views
0

私はgraphene-djangoを使用しています。graphene-djangoはmodels.BigInteger()をgraphene.Integer()に変換します。

私はmodels.BigInteger()フィールドからデータを取得しようとしているが、私はgraphiQLでクエリを作るとき、私は、エラー

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

は私が私のデータを与えるために、グラフェンを強制することができますどのように誰もが知ってもらいますか?

答えて

0

私はカスタムスカラーを使用しました。これが自動的にグラフェン - ジャンゴの中でより良いスカラーに変換されていればより良いでしょうが、ここでこれを修正しました。私たちは、その後

# 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 = {} 
MAX_INTよりも大きい数値がある場合は、int型ではなくfloatを使用するカスタムのBigIntスカラー、とconverter.pyファイルを書きました
関連する問題