forked from graphql-python/graphene
-
Notifications
You must be signed in to change notification settings - Fork 0
/
complex_example.py
69 lines (47 loc) · 1.44 KB
/
complex_example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 Query(graphene.ObjectType):
address = graphene.Field(Address, geo=GeoInput(required=True))
def resolve_address(self, info, geo):
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)
class Mutation(graphene.ObjectType):
create_address = CreateAddress.Field()
schema = graphene.Schema(query=Query, mutation=Mutation)
query = """
query something{
address(geo: {lat:32.2, lng:12}) {
latlng
}
}
"""
mutation = """
mutation addAddress{
createAddress(geo: {lat:32.2, lng:12}) {
latlng
}
}
"""
def test_query():
result = schema.execute(query)
assert not result.errors
assert result.data == {"address": {"latlng": "(32.2,12.0)"}}
def test_mutation():
result = schema.execute(mutation)
assert not result.errors
assert result.data == {"createAddress": {"latlng": "(32.2,12.0)"}}
if __name__ == "__main__":
result = schema.execute(query)
print(result.data["address"]["latlng"])