Skip to content

Commit 97a8112

Browse files
committed
Added mutations in examples
1 parent a682ef8 commit 97a8112

File tree

6 files changed

+246
-47
lines changed

6 files changed

+246
-47
lines changed

examples/starwars_django/data.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -85,31 +85,30 @@ def initialize():
8585
executor.save()
8686

8787

88-
def createShip(shipName, factionId):
89-
nextShip = len(data['Ship'].keys())+1
90-
newShip = Ship(
91-
id=str(nextShip),
92-
name=shipName
88+
def create_ship(ship_name, faction_id):
89+
new_ship = Ship(
90+
name=ship_name,
91+
faction_id=faction_id
9392
)
94-
newShip.save()
95-
return newShip
93+
new_ship.save()
94+
return new_ship
9695

9796

98-
def getShip(_id):
97+
def get_ship(_id):
9998
return Ship.objects.get(id=_id)
10099

101100

102-
def getShips():
101+
def get_ships():
103102
return Ship.objects.all()
104103

105104

106-
def getFaction(_id):
105+
def get_faction(_id):
107106
return Faction.objects.get(id=_id)
108107

109108

110-
def getRebels():
111-
return getFaction(1)
109+
def get_rebels():
110+
return get_faction(1)
112111

113112

114-
def getEmpire():
115-
return getFaction(2)
113+
def get_empire():
114+
return get_faction(2)

examples/starwars_django/schema.py

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
from .models import (
88
Ship as ShipModel, Faction as FactionModel, Character as CharacterModel)
99
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
1516
)
1617

1718
schema = graphene.Schema(name='Starwars Django Relay Schema')
@@ -23,7 +24,7 @@ class Meta:
2324

2425
@classmethod
2526
def get_node(cls, id):
26-
return Ship(getShip(id))
27+
return Ship(get_ship(id))
2728

2829

2930
@schema.register
@@ -38,7 +39,24 @@ class Meta:
3839

3940
@classmethod
4041
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)
4260

4361

4462
class Query(graphene.ObjectType):
@@ -49,15 +67,20 @@ class Query(graphene.ObjectType):
4967

5068
@resolve_only_args
5169
def resolve_ships(self):
52-
return [Ship(s) for s in getShips()]
70+
return [Ship(s) for s in get_ships()]
5371

5472
@resolve_only_args
5573
def resolve_rebels(self):
56-
return Faction(getRebels())
74+
return Faction(get_rebels())
5775

5876
@resolve_only_args
5977
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)
6183

6284

6385
schema.query = Query
86+
schema.mutation = Mutation
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import pytest
2+
from ..schema import schema
3+
from ..data import initialize
4+
5+
pytestmark = pytest.mark.django_db
6+
7+
8+
def test_mutations():
9+
initialize()
10+
11+
query = '''
12+
mutation MyMutation {
13+
introduceShip(input:{clientMutationId:"abc", shipName: "Peter", factionId: "1"}) {
14+
ship {
15+
id
16+
name
17+
}
18+
faction {
19+
name
20+
ships {
21+
edges {
22+
node {
23+
id
24+
name
25+
}
26+
}
27+
}
28+
}
29+
}
30+
}
31+
'''
32+
expected = {
33+
'introduceShip': {
34+
'ship': {
35+
'id': 'U2hpcDo5',
36+
'name': 'Peter'
37+
},
38+
'faction': {
39+
'name': 'Alliance to Restore the Republic',
40+
'ships': {
41+
'edges': [{
42+
'node': {
43+
'id': 'U2hpcDox',
44+
'name': 'X-Wing'
45+
}
46+
}, {
47+
'node': {
48+
'id': 'U2hpcDoy',
49+
'name': 'Y-Wing'
50+
}
51+
}, {
52+
'node': {
53+
'id': 'U2hpcDoz',
54+
'name': 'A-Wing'
55+
}
56+
}, {
57+
'node': {
58+
'id': 'U2hpcDo0',
59+
'name': 'Millenium Falcon'
60+
}
61+
}, {
62+
'node': {
63+
'id': 'U2hpcDo1',
64+
'name': 'Home One'
65+
}
66+
}, {
67+
'node': {
68+
'id': 'U2hpcDo5',
69+
'name': 'Peter'
70+
}
71+
}]
72+
},
73+
}
74+
}
75+
}
76+
result = schema.execute(query)
77+
assert not result.errors
78+
assert result.data == expected

examples/starwars_relay/data.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -77,28 +77,29 @@ def setup():
7777
}
7878

7979

80-
def createShip(shipName, factionId):
81-
nextShip = len(data['Ship'].keys())+1
82-
newShip = Ship(
83-
id=str(nextShip),
84-
name=shipName
80+
def create_ship(ship_name, faction_id):
81+
from .schema import Ship
82+
next_ship = len(data['Ship'].keys()) + 1
83+
new_ship = Ship(
84+
id=str(next_ship),
85+
name=ship_name
8586
)
86-
data['Ship'][newShip.id] = newShip
87-
data['Faction'][factionId].ships.append(newShip.id)
88-
return newShip
87+
data['Ship'][new_ship.id] = new_ship
88+
data['Faction'][faction_id].ships.append(new_ship.id)
89+
return new_ship
8990

9091

91-
def getShip(_id):
92+
def get_ship(_id):
9293
return data['Ship'][_id]
9394

9495

95-
def getFaction(_id):
96+
def get_faction(_id):
9697
return data['Faction'][_id]
9798

9899

99-
def getRebels():
100-
return getFaction('1')
100+
def get_rebels():
101+
return get_faction('1')
101102

102103

103-
def getEmpire():
104-
return getFaction('2')
104+
def get_empire():
105+
return get_faction('2')

examples/starwars_relay/schema.py

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22
from graphene import resolve_only_args, relay
33

44
from .data import (
5-
getFaction,
6-
getShip,
7-
getRebels,
8-
getEmpire,
5+
get_faction,
6+
get_ship,
7+
get_rebels,
8+
get_empire,
9+
create_ship,
910
)
1011

1112
schema = graphene.Schema(name='Starwars Relay Schema')
@@ -17,7 +18,7 @@ class Ship(relay.Node):
1718

1819
@classmethod
1920
def get_node(cls, id):
20-
return getShip(id)
21+
return get_ship(id)
2122

2223

2324
class Faction(relay.Node):
@@ -29,11 +30,28 @@ class Faction(relay.Node):
2930
@resolve_only_args
3031
def resolve_ships(self, **args):
3132
# Transform the instance ship_ids into real instances
32-
return [getShip(ship_id) for ship_id in self.ships]
33+
return [get_ship(ship_id) for ship_id in self.ships]
3334

3435
@classmethod
3536
def get_node(cls, id):
36-
return getFaction(id)
37+
return get_faction(id)
38+
39+
40+
class IntroduceShip(relay.ClientIDMutation):
41+
class Input:
42+
ship_name = graphene.StringField(required=True)
43+
faction_id = graphene.StringField(required=True)
44+
45+
ship = graphene.Field(Ship)
46+
faction = graphene.Field(Faction)
47+
48+
@classmethod
49+
def mutate_and_get_payload(cls, input, info):
50+
ship_name = input.get('ship_name')
51+
faction_id = input.get('faction_id')
52+
ship = create_ship(ship_name, faction_id)
53+
faction = get_faction(faction_id)
54+
return IntroduceShip(ship=ship, faction=faction)
3755

3856

3957
class Query(graphene.ObjectType):
@@ -43,11 +61,16 @@ class Query(graphene.ObjectType):
4361

4462
@resolve_only_args
4563
def resolve_rebels(self):
46-
return getRebels()
64+
return get_rebels()
4765

4866
@resolve_only_args
4967
def resolve_empire(self):
50-
return getEmpire()
68+
return get_empire()
69+
70+
71+
class Mutation(graphene.ObjectType):
72+
introduce_ship = graphene.Field(IntroduceShip)
5173

5274

5375
schema.query = Query
76+
schema.mutation = Mutation
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
from ..schema import schema
2+
from ..data import setup
3+
4+
setup()
5+
6+
7+
def test_mutations():
8+
query = '''
9+
mutation MyMutation {
10+
introduceShip(input:{clientMutationId:"abc", shipName: "Peter", factionId: "1"}) {
11+
ship {
12+
id
13+
name
14+
}
15+
faction {
16+
name
17+
ships {
18+
edges {
19+
node {
20+
id
21+
name
22+
}
23+
}
24+
}
25+
}
26+
}
27+
}
28+
'''
29+
expected = {
30+
'introduceShip': {
31+
'ship': {
32+
'id': 'U2hpcDo5',
33+
'name': 'Peter'
34+
},
35+
'faction': {
36+
'name': 'Alliance to Restore the Republic',
37+
'ships': {
38+
'edges': [{
39+
'node': {
40+
'id': 'U2hpcDox',
41+
'name': 'X-Wing'
42+
}
43+
}, {
44+
'node': {
45+
'id': 'U2hpcDoy',
46+
'name': 'Y-Wing'
47+
}
48+
}, {
49+
'node': {
50+
'id': 'U2hpcDoz',
51+
'name': 'A-Wing'
52+
}
53+
}, {
54+
'node': {
55+
'id': 'U2hpcDo0',
56+
'name': 'Millenium Falcon'
57+
}
58+
}, {
59+
'node': {
60+
'id': 'U2hpcDo1',
61+
'name': 'Home One'
62+
}
63+
}, {
64+
'node': {
65+
'id': 'U2hpcDo5',
66+
'name': 'Peter'
67+
}
68+
}]
69+
},
70+
}
71+
}
72+
}
73+
result = schema.execute(query)
74+
assert not result.errors
75+
assert result.data == expected

0 commit comments

Comments
 (0)