graphene/examples/starwars/data.py

98 lines
1.9 KiB
Python
Raw Normal View History

2015-11-03 06:28:06 +03:00
human_data = {}
droid_data = {}
2015-10-27 09:54:51 +03:00
def setup():
from .schema import Human, Droid
2015-11-03 06:28:06 +03:00
global human_data, droid_data
2015-10-27 09:54:51 +03:00
luke = Human(
id='1000',
name='Luke Skywalker',
friends=['1002', '1003', '2000', '2001'],
2016-06-05 00:05:31 +03:00
appears_in=[4, 5, 6],
2015-10-27 09:54:51 +03:00
home_planet='Tatooine',
)
vader = Human(
id='1001',
name='Darth Vader',
friends=['1004'],
2016-06-05 00:05:31 +03:00
appears_in=[4, 5, 6],
2015-10-27 09:54:51 +03:00
home_planet='Tatooine',
)
han = Human(
id='1002',
name='Han Solo',
friends=['1000', '1003', '2001'],
2016-06-05 00:05:31 +03:00
appears_in=[4, 5, 6],
2015-10-27 09:54:51 +03:00
home_planet=None,
)
leia = Human(
id='1003',
name='Leia Organa',
friends=['1000', '1002', '2000', '2001'],
2016-06-05 00:05:31 +03:00
appears_in=[4, 5, 6],
2015-10-27 09:54:51 +03:00
home_planet='Alderaan',
)
tarkin = Human(
id='1004',
name='Wilhuff Tarkin',
friends=['1001'],
2016-06-05 00:05:31 +03:00
appears_in=[4],
2015-10-27 09:54:51 +03:00
home_planet=None,
)
2015-11-03 06:28:06 +03:00
human_data = {
2015-10-27 09:54:51 +03:00
'1000': luke,
'1001': vader,
'1002': han,
'1003': leia,
'1004': tarkin,
}
2016-06-04 21:21:33 +03:00
c3po = Droid(
2015-10-27 09:54:51 +03:00
id='2000',
name='C-3PO',
friends=['1000', '1002', '1003', '2001'],
2016-06-05 00:05:31 +03:00
appears_in=[4, 5, 6],
2015-10-27 09:54:51 +03:00
primary_function='Protocol',
)
2016-06-04 21:21:33 +03:00
r2d2 = Droid(
2015-10-27 09:54:51 +03:00
id='2001',
name='R2-D2',
friends=['1000', '1002', '1003'],
2016-06-05 00:05:31 +03:00
appears_in=[4, 5, 6],
2015-10-27 09:54:51 +03:00
primary_function='Astromech',
)
2015-11-03 06:28:06 +03:00
droid_data = {
2016-06-04 21:21:33 +03:00
'2000': c3po,
'2001': r2d2,
2015-10-27 09:54:51 +03:00
}
2015-09-24 12:11:50 +03:00
2015-10-03 08:17:51 +03:00
2015-11-03 06:28:06 +03:00
def get_character(id):
return human_data.get(id) or droid_data.get(id)
2015-09-24 12:11:50 +03:00
2015-11-03 06:28:06 +03:00
def get_friends(character):
return map(get_character, character.friends)
2015-09-24 12:11:50 +03:00
2015-11-03 06:28:06 +03:00
def get_hero(episode):
2015-09-24 12:11:50 +03:00
if episode == 5:
2015-11-03 06:28:06 +03:00
return human_data['1000']
return droid_data['2001']
2015-09-24 12:11:50 +03:00
2015-11-03 06:28:06 +03:00
def get_human(id):
return human_data.get(id)
2015-09-24 12:11:50 +03:00
2015-11-03 06:28:06 +03:00
def get_droid(id):
return droid_data.get(id)