Skip to content

Commit 1dc8b73

Browse files
committed
Added starwars relay example
1 parent 221889c commit 1dc8b73

File tree

2 files changed

+168
-0
lines changed

2 files changed

+168
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
query {
2+
empire {
3+
id
4+
name
5+
ships(first:2) {
6+
edges {
7+
node {
8+
id
9+
name
10+
}
11+
}
12+
}
13+
}
14+
node(id:"U2hpcDo4") {
15+
id
16+
... on Ship {
17+
name
18+
}
19+
}
20+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
import graphene
2+
from graphene import relay, resolve_only_args
3+
4+
class Ship(relay.Node):
5+
'''A ship in the Star Wars saga'''
6+
name = graphene.String(description='The name of the ship.')
7+
8+
@classmethod
9+
def get_node(cls, id, info):
10+
return get_ship(id)
11+
12+
class Faction(relay.Node):
13+
'''A faction in the Star Wars saga'''
14+
name = graphene.String(description='The name of the faction.')
15+
ships = relay.ConnectionField(
16+
Ship, description='The ships used by the faction.')
17+
18+
@resolve_only_args
19+
def resolve_ships(self, **args):
20+
# Transform the instance ship_ids into real instances
21+
return [get_ship(ship_id) for ship_id in self.ships]
22+
23+
@classmethod
24+
def get_node(cls, id, info):
25+
return get_faction(id)
26+
27+
class IntroduceShip(relay.ClientIDMutation):
28+
class Input:
29+
ship_name = graphene.String(required=True)
30+
faction_id = graphene.String(required=True)
31+
32+
ship = graphene.Field(Ship)
33+
faction = graphene.Field(Faction)
34+
35+
@classmethod
36+
def mutate_and_get_payload(cls, input, info):
37+
ship_name = input.get('ship_name')
38+
faction_id = input.get('faction_id')
39+
ship = create_ship(ship_name, faction_id)
40+
faction = get_faction(faction_id)
41+
return IntroduceShip(ship=ship, faction=faction)
42+
43+
44+
class Query(graphene.ObjectType):
45+
rebels = graphene.Field(Faction)
46+
empire = graphene.Field(Faction)
47+
node = relay.NodeField()
48+
49+
@resolve_only_args
50+
def resolve_rebels(self):
51+
return get_rebels()
52+
53+
@resolve_only_args
54+
def resolve_empire(self):
55+
return get_empire()
56+
57+
58+
class Mutation(graphene.ObjectType):
59+
introduce_ship = graphene.Field(IntroduceShip)
60+
61+
schema = graphene.Schema(name='Starwars Relay Schema')
62+
schema.query = Query
63+
schema.mutation = Mutation
64+
65+
xwing = Ship(
66+
id='1',
67+
name='X-Wing',
68+
)
69+
ywing = Ship(
70+
id='2',
71+
name='Y-Wing',
72+
)
73+
awing = Ship(
74+
id='3',
75+
name='A-Wing',
76+
)
77+
78+
# Yeah, technically it's Corellian. But it flew in the service of the rebels,
79+
# so for the purposes of this demo it's a rebel ship.
80+
falcon = Ship(
81+
id='4',
82+
name='Millenium Falcon',
83+
)
84+
homeOne = Ship(
85+
id='5',
86+
name='Home One',
87+
)
88+
tieFighter = Ship(
89+
id='6',
90+
name='TIE Fighter',
91+
)
92+
tieInterceptor = Ship(
93+
id='7',
94+
name='TIE Interceptor',
95+
)
96+
executor = Ship(
97+
id='8',
98+
name='Executor',
99+
)
100+
rebels = Faction(
101+
id='1',
102+
name='Alliance to Restore the Republic',
103+
ships=['1', '2', '3', '4', '5']
104+
)
105+
empire = Faction(
106+
id='2',
107+
name='Galactic Empire',
108+
ships=['6', '7', '8']
109+
)
110+
data = {
111+
'Faction': {
112+
'1': rebels,
113+
'2': empire
114+
},
115+
'Ship': {
116+
'1': xwing,
117+
'2': ywing,
118+
'3': awing,
119+
'4': falcon,
120+
'5': homeOne,
121+
'6': tieFighter,
122+
'7': tieInterceptor,
123+
'8': executor
124+
}
125+
}
126+
127+
def create_ship(ship_name, faction_id):
128+
from .schema import Ship
129+
next_ship = len(data['Ship'].keys()) + 1
130+
new_ship = Ship(
131+
id=str(next_ship),
132+
name=ship_name
133+
)
134+
data['Ship'][new_ship.id] = new_ship
135+
data['Faction'][faction_id].ships.append(new_ship.id)
136+
return new_ship
137+
138+
def get_ship(_id):
139+
return data['Ship'][_id]
140+
141+
def get_faction(_id):
142+
return data['Faction'][_id]
143+
144+
def get_rebels():
145+
return get_faction('1')
146+
147+
def get_empire():
148+
return get_faction('2')

0 commit comments

Comments
 (0)