Skip to content

How to share InputObjectType for different resolver? #803

Closed
@ptrhck

Description

@ptrhck

I would like to not define the input variables multiple times when they are equivalent for all resolver. How can I achieve that?

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"])

Is there any way doing it for the whole query class, like initializing variables?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions