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):
|
2020-03-15 03:32:44 +03:00
|
|
|
return f"({self.lat},{self.lng})"
|
2017-08-30 05:53:24 +03:00
|
|
|
|
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
|
|
|
|
2020-03-15 21:52:56 +03:00
|
|
|
def resolve_address(root, 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
|
|
|
|
|
2020-03-15 21:52:56 +03:00
|
|
|
def mutate(root, info, geo):
|
2017-08-30 05:53:24 +03:00
|
|
|
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)
|
2018-07-06 22:09:23 +03:00
|
|
|
query = """
|
2015-11-23 04:29:30 +03:00
|
|
|
query something{
|
|
|
|
address(geo: {lat:32.2, lng:12}) {
|
|
|
|
latlng
|
|
|
|
}
|
|
|
|
}
|
2018-07-06 22:09:23 +03:00
|
|
|
"""
|
|
|
|
mutation = """
|
2017-08-30 05:53:24 +03:00
|
|
|
mutation addAddress{
|
|
|
|
createAddress(geo: {lat:32.2, lng:12}) {
|
|
|
|
latlng
|
|
|
|
}
|
|
|
|
}
|
2018-07-06 22:09:23 +03:00
|
|
|
"""
|
2015-11-23 04:29:30 +03:00
|
|
|
|
2016-06-05 01:22:10 +03:00
|
|
|
|
|
|
|
def test_query():
|
|
|
|
result = schema.execute(query)
|
|
|
|
assert not result.errors
|
2018-07-06 22:09:23 +03:00
|
|
|
assert result.data == {"address": {"latlng": "(32.2,12.0)"}}
|
2016-06-05 01:22:10 +03:00
|
|
|
|
|
|
|
|
2017-08-30 05:53:24 +03:00
|
|
|
def test_mutation():
|
|
|
|
result = schema.execute(mutation)
|
|
|
|
assert not result.errors
|
2018-07-06 22:09:23 +03:00
|
|
|
assert result.data == {"createAddress": {"latlng": "(32.2,12.0)"}}
|
2017-08-30 05:53:24 +03:00
|
|
|
|
|
|
|
|
2018-07-06 22:09:23 +03:00
|
|
|
if __name__ == "__main__":
|
2016-06-05 01:22:10 +03:00
|
|
|
result = schema.execute(query)
|
2018-07-06 22:09:23 +03:00
|
|
|
print(result.data["address"]["latlng"])
|