Graphene:如何为不同的解析器共享InputObjectType?

时间:2018-07-26 08:23:14

标签: python graphql graphene-python

当所有解析器都等效时,我不想多次定义输入变量。我该如何实现?

import graphene


class GeoInput(graphene.InputObjectType):
    lat = graphene.Float(required=True)
    lng = graphene.Float(required=True)

    @property
    def latlng(self):
        return "({},{})".format(self.lat, self.lng)


class Address(graphene.ObjectType):
    latlng = graphene.String()


class Test(graphene.ObjectType):
    test_string = graphene.String()


class Query(graphene.ObjectType):
    address = graphene.Field(Address, geo=GeoInput(required=True))
    test = graphene.Field(Test, geo=GeoInput(required=True))

    def resolve_address(self, info, geo):
        return Address(latlng=geo.latlng)

    def resolve_test(self, info, geo):
        return Test(test_string="({},{})".format(geo.lat, geo.lng))

schema = graphene.Schema(query=Query)
query = """
    query something{
      address(geo: {lat:32.2, lng:12}) {
        latlng
      }
      test(geo: {lat:32.2, lng:12}) {
        testString
      }
    }
"""

if __name__ == "__main__":
    result = schema.execute(query)
print(result.data["address"]["latlng"],
      result.data["test"]["testString"])

对于整个查询类,有没有办法像初始化变量那样?

0 个答案:

没有答案