@@ -5,6 +5,10 @@ class GeoInput(graphene.InputObjectType):
5
5
lat = graphene .Float (required = True )
6
6
lng = graphene .Float (required = True )
7
7
8
+ @property
9
+ def latlng (self ):
10
+ return "({},{})" .format (self .lat , self .lng )
11
+
8
12
9
13
class Address (graphene .ObjectType ):
10
14
latlng = graphene .String ()
@@ -14,17 +18,39 @@ class Query(graphene.ObjectType):
14
18
address = graphene .Field (Address , geo = GeoInput (required = True ))
15
19
16
20
def resolve_address (self , info , geo ):
17
- return Address (latlng = "({},{})" .format (geo .get ('lat' ), geo .get ('lng' )))
21
+ return Address (latlng = geo .latlng )
22
+
23
+
24
+ class CreateAddress (graphene .Mutation ):
25
+
26
+ class Arguments :
27
+ geo = GeoInput (required = True )
28
+
29
+ Output = Address
30
+
31
+ def mutate (self , info , geo ):
32
+ return Address (latlng = geo .latlng )
18
33
19
34
20
- schema = graphene .Schema (query = Query )
35
+ class Mutation (graphene .ObjectType ):
36
+ create_address = CreateAddress .Field ()
37
+
38
+
39
+ schema = graphene .Schema (query = Query , mutation = Mutation )
21
40
query = '''
22
41
query something{
23
42
address(geo: {lat:32.2, lng:12}) {
24
43
latlng
25
44
}
26
45
}
27
46
'''
47
+ mutation = '''
48
+ mutation addAddress{
49
+ createAddress(geo: {lat:32.2, lng:12}) {
50
+ latlng
51
+ }
52
+ }
53
+ '''
28
54
29
55
30
56
def test_query ():
@@ -37,6 +63,16 @@ def test_query():
37
63
}
38
64
39
65
66
+ def test_mutation ():
67
+ result = schema .execute (mutation )
68
+ assert not result .errors
69
+ assert result .data == {
70
+ 'createAddress' : {
71
+ 'latlng' : "(32.2,12.0)" ,
72
+ }
73
+ }
74
+
75
+
40
76
if __name__ == '__main__' :
41
77
result = schema .execute (query )
42
78
print (result .data ['address' ]['latlng' ])
0 commit comments