Debian Codegraph @

tests/star_wars_data.py

raw
1 """This defines a basic set of data for our Star Wars Schema.
2
3 This data is hard coded for the sake of the demo, but you could imagine fetching this
4 data from a backend service rather than from hardcoded JSON objects in a more complex
5 demo.
6 """
7
8 from typing import Awaitable, Collection, Dict, Iterator, Optional
9
10 __all__ = ["get_droid", "get_friends", "get_hero", "get_human", "get_secret_backstory"]
11
12 # These are classes which correspond to the schema.
13 # They represent the shape of the data visited during field resolution.
14
15
16 class Character:
17 id: str
18 name: str
19 friends: Collection[str]
20 appearsIn: Collection[str]
21
22
23 # noinspection PyPep8Naming
24 class Human(Character):
25 type = "Human"
26 homePlanet: str
27
28 # noinspection PyShadowingBuiltins
29 def __init__(self, id, name, friends, appearsIn, homePlanet):
30 self.id, self.name = id, name
31 self.friends, self.appearsIn = friends, appearsIn
32 self.homePlanet = homePlanet
33
34
35 # noinspection PyPep8Naming
36 class Droid(Character):
37 type = "Droid"
38 primaryFunction: str
39
40 # noinspection PyShadowingBuiltins
41 def __init__(self, id, name, friends, appearsIn, primaryFunction):
42 self.id, self.name = id, name
43 self.friends, self.appearsIn = friends, appearsIn
44 self.primaryFunction = primaryFunction
45
46
47 luke = Human(
48 id="1000",
49 name="Luke Skywalker",
50 friends=["1002", "1003", "2000", "2001"],
51 appearsIn=[4, 5, 6],
52 homePlanet="Tatooine",
53 )
54
55 vader = Human(
56 id="1001",
57 name="Darth Vader",
58 friends=["1004"],
59 appearsIn=[4, 5, 6],
60 homePlanet="Tatooine",
61 )
62
63 han = Human(
64 id="1002",
65 name="Han Solo",
66 friends=["1000", "1003", "2001"],
67 appearsIn=[4, 5, 6],
68 homePlanet=None,
69 )
70
71 leia = Human(
72 id="1003",
73 name="Leia Organa",
74 friends=["1000", "1002", "2000", "2001"],
75 appearsIn=[4, 5, 6],
76 homePlanet="Alderaan",
77 )
78
79 tarkin = Human(
80 id="1004", name="Wilhuff Tarkin", friends=["1001"], appearsIn=[4], homePlanet=None
81 )
82
83 human_data: Dict[str, Human] = {
84 "1000": luke,
85 "1001": vader,
86 "1002": han,
87 "1003": leia,
88 "1004": tarkin,
89 }
90
91 threepio = Droid(
92 id="2000",
93 name="C-3PO",
94 friends=["1000", "1002", "1003", "2001"],
95 appearsIn=[4, 5, 6],
96 primaryFunction="Protocol",
97 )
98
99 artoo = Droid(
100 id="2001",
101 name="R2-D2",
102 friends=["1000", "1002", "1003"],
103 appearsIn=[4, 5, 6],
104 primaryFunction="Astromech",
105 )
106
107 droid_data: Dict[str, Droid] = {"2000": threepio, "2001": artoo}
108
109
110 # noinspection PyShadowingBuiltins
111 async def get_character(id: str) -> Optional[Character]:
112 """Helper function to get a character by ID."""
113 # We use an async function just to illustrate that GraphQL-core supports it.
114 return human_data.get(id) or droid_data.get(id)
115
116
117 def get_friends(character: Character) -> Iterator[Awaitable[Optional[Character]]]:
118 """Allows us to query for a character's friends."""
119 # Notice that GraphQL-core accepts iterators of awaitables.
120 return map(get_character, character.friends)
121
122
123 def get_hero(episode: int) -> Character:
124 """Allows us to fetch the undisputed hero of the trilogy, R2-D2."""
125 if episode == 5:
126 # Luke is the hero of Episode V.
127 return luke
128 # Artoo is the hero otherwise.
129 return artoo
130
131
132 # noinspection PyShadowingBuiltins
133 def get_human(id: str) -> Optional[Human]:
134 """Allows us to query for the human with the given id."""
135 return human_data.get(id)
136
137
138 # noinspection PyShadowingBuiltins
139 def get_droid(id: str) -> Optional[Droid]:
140 """Allows us to query for the droid with the given id."""
141 return droid_data.get(id)
142
143
144 # noinspection PyUnusedLocal
145 def get_secret_backstory(character: Character) -> str:
146 """Raise an error when attempting to get the secret backstory."""
147 raise RuntimeError("secretBackstory is secret.")