7
7
from .models import (
8
8
Ship as ShipModel , Faction as FactionModel , Character as CharacterModel )
9
9
from .data import (
10
- getFaction ,
11
- getShip ,
12
- getShips ,
13
- getRebels ,
14
- getEmpire ,
10
+ get_faction ,
11
+ get_ship ,
12
+ get_ships ,
13
+ get_rebels ,
14
+ get_empire ,
15
+ create_ship
15
16
)
16
17
17
18
schema = graphene .Schema (name = 'Starwars Django Relay Schema' )
@@ -23,7 +24,7 @@ class Meta:
23
24
24
25
@classmethod
25
26
def get_node (cls , id ):
26
- return Ship (getShip (id ))
27
+ return Ship (get_ship (id ))
27
28
28
29
29
30
@schema .register
@@ -38,7 +39,24 @@ class Meta:
38
39
39
40
@classmethod
40
41
def get_node (cls , id ):
41
- return Faction (getFaction (id ))
42
+ return Faction (get_faction (id ))
43
+
44
+
45
+ class IntroduceShip (relay .ClientIDMutation ):
46
+ class Input :
47
+ ship_name = graphene .StringField (required = True )
48
+ faction_id = graphene .StringField (required = True )
49
+
50
+ ship = graphene .Field (Ship )
51
+ faction = graphene .Field (Faction )
52
+
53
+ @classmethod
54
+ def mutate_and_get_payload (cls , input , info ):
55
+ ship_name = input .get ('ship_name' )
56
+ faction_id = input .get ('faction_id' )
57
+ ship = create_ship (ship_name , faction_id )
58
+ faction = get_faction (faction_id )
59
+ return IntroduceShip (ship = ship , faction = faction )
42
60
43
61
44
62
class Query (graphene .ObjectType ):
@@ -49,15 +67,20 @@ class Query(graphene.ObjectType):
49
67
50
68
@resolve_only_args
51
69
def resolve_ships (self ):
52
- return [Ship (s ) for s in getShips ()]
70
+ return [Ship (s ) for s in get_ships ()]
53
71
54
72
@resolve_only_args
55
73
def resolve_rebels (self ):
56
- return Faction (getRebels ())
74
+ return Faction (get_rebels ())
57
75
58
76
@resolve_only_args
59
77
def resolve_empire (self ):
60
- return Faction (getEmpire ())
78
+ return Faction (get_empire ())
79
+
80
+
81
+ class Mutation (graphene .ObjectType ):
82
+ introduce_ship = graphene .Field (IntroduceShip )
61
83
62
84
63
85
schema .query = Query
86
+ schema .mutation = Mutation
0 commit comments