graphene/examples/complex_example.py

70 lines
1.4 KiB
Python
Raw Permalink Normal View History

2015-11-23 04:29:30 +03:00
import graphene
class GeoInput(graphene.InputObjectType):
lat = graphene.Float(required=True)
lng = graphene.Float(required=True)
2017-08-30 05:53:24 +03:00
@property
def latlng(self):
return "({},{})".format(self.lat, self.lng)
2015-11-23 04:29:30 +03:00
class Address(graphene.ObjectType):
latlng = graphene.String()
class Query(graphene.ObjectType):
2017-07-27 12:51:25 +03:00
address = graphene.Field(Address, geo=GeoInput(required=True))
2015-11-23 04:29:30 +03:00
2017-07-27 12:51:25 +03:00
def resolve_address(self, info, geo):
2017-08-30 05:53:24 +03:00
return Address(latlng=geo.latlng)
class CreateAddress(graphene.Mutation):
class Arguments:
geo = GeoInput(required=True)
Output = Address
def mutate(self, info, geo):
return Address(latlng=geo.latlng)
2015-11-23 04:29:30 +03:00
2017-08-30 05:53:24 +03:00
class Mutation(graphene.ObjectType):
create_address = CreateAddress.Field()
schema = graphene.Schema(query=Query, mutation=Mutation)
query = """
2015-11-23 04:29:30 +03:00
query something{
address(geo: {lat:32.2, lng:12}) {
latlng
}
}
"""
mutation = """
2017-08-30 05:53:24 +03:00
mutation addAddress{
createAddress(geo: {lat:32.2, lng:12}) {
latlng
}
}
"""
2015-11-23 04:29:30 +03:00
def test_query():
result = schema.execute(query)
assert not result.errors
assert result.data == {"address": {"latlng": "(32.2,12.0)"}}
2017-08-30 05:53:24 +03:00
def test_mutation():
result = schema.execute(mutation)
assert not result.errors
assert result.data == {"createAddress": {"latlng": "(32.2,12.0)"}}
2017-08-30 05:53:24 +03:00
if __name__ == "__main__":
result = schema.execute(query)
print(result.data["address"]["latlng"])