Debian Codegraph @

tests/star_wars_schema.py

raw
1 """Star Wars GraphQL schema
2
3 This is designed to be an end-to-end test, demonstrating the full GraphQL stack.
4
5 We will create a GraphQL schema that describes the major characters in the original
6 Star Wars trilogy.
7
8 NOTE: This may contain spoilers for the original Star Wars trilogy.
9
10 Using our shorthand to describe type systems, the type system for our Star Wars example
11 is::
12
13 enum Episode { NEW_HOPE, EMPIRE, JEDI }
14
15 interface Character {
16 id: String!
17 name: String
18 friends: [Character]
19 appearsIn: [Episode]
20 }
21
22 type Human implements Character {
23 id: String!
24 name: String
25 friends: [Character]
26 appearsIn: [Episode]
27 homePlanet: String
28 }
29
30 type Droid implements Character {
31 id: String!
32 name: String
33 friends: [Character]
34 appearsIn: [Episode]
35 primaryFunction: String
36 }
37
38 type Query {
39 hero(episode: Episode): Character
40 human(id: String!): Human
41 droid(id: String!): Droid
42 }
43 """
44
45 from graphql.type import (
46 GraphQLArgument,
47 GraphQLEnumType,
48 GraphQLEnumValue,
49 GraphQLField,
50 GraphQLInterfaceType,
51 GraphQLList,
52 GraphQLNonNull,
53 GraphQLObjectType,
54 GraphQLSchema,
55 GraphQLString,
56 )
57 from tests.star_wars_data import (
58 get_droid,
59 get_friends,
60 get_hero,
61 get_human,
62 get_secret_backstory,
63 )
64
65 __all__ = ["star_wars_schema"]
66
67 # We begin by setting up our schema.
68
69 # The original trilogy consists of three movies.
70 #
71 # This implements the following type system shorthand:
72 # enum Episode { NEW_HOPE, EMPIRE, JEDI }
73
74 episode_enum = GraphQLEnumType(
75 "Episode",
76 {
77 "NEW_HOPE": GraphQLEnumValue(4, description="Released in 1977."),
78 "EMPIRE": GraphQLEnumValue(5, description="Released in 1980."),
79 "JEDI": GraphQLEnumValue(6, description="Released in 1983."),
80 },
81 description="One of the films in the Star Wars Trilogy",
82 )
83
84 # Characters in the Star Wars trilogy are either humans or droids.
85 #
86 # This implements the following type system shorthand:
87 # interface Character {
88 # id: String!
89 # name: String
90 # friends: [Character]
91 # appearsIn: [Episode]
92 # secretBackstory: String
93
94 human_type: GraphQLObjectType
95 droid_type: GraphQLObjectType
96
97 character_interface: GraphQLInterfaceType = GraphQLInterfaceType(
98 "Character",
99 lambda: {
100 "id": GraphQLField(
101 GraphQLNonNull(GraphQLString), description="The id of the character."
102 ),
103 "name": GraphQLField(GraphQLString, description="The name of the character."),
104 "friends": GraphQLField(
105 GraphQLList(character_interface),
106 description="The friends of the character,"
107 " or an empty list if they have none.",
108 ),
109 "appearsIn": GraphQLField(
110 GraphQLList(episode_enum), description="Which movies they appear in."
111 ),
112 "secretBackstory": GraphQLField(
113 GraphQLString, description="All secrets about their past."
114 ),
115 },
116 resolve_type=lambda character, _info, _type: {
117 "Human": human_type.name,
118 "Droid": droid_type.name,
119 }[character.type],
120 description="A character in the Star Wars Trilogy",
121 )
122
123 # We define our human type, which implements the character interface.
124 #
125 # This implements the following type system shorthand:
126 # type Human : Character {
127 # id: String!
128 # name: String
129 # friends: [Character]
130 # appearsIn: [Episode]
131 # secretBackstory: String
132 # }
133
134 human_type = GraphQLObjectType(
135 "Human",
136 lambda: {
137 "id": GraphQLField(
138 GraphQLNonNull(GraphQLString), description="The id of the human."
139 ),
140 "name": GraphQLField(GraphQLString, description="The name of the human."),
141 "friends": GraphQLField(
142 GraphQLList(character_interface),
143 description="The friends of the human,"
144 " or an empty list if they have none.",
145 resolve=lambda human, _info: get_friends(human),
146 ),
147 "appearsIn": GraphQLField(
148 GraphQLList(episode_enum), description="Which movies they appear in."
149 ),
150 "homePlanet": GraphQLField(
151 GraphQLString,
152 description="The home planet of the human, or null if unknown.",
153 ),
154 "secretBackstory": GraphQLField(
155 GraphQLString,
156 resolve=lambda human, _info: get_secret_backstory(human),
157 description="Where are they from and how they came to be who they are.",
158 ),
159 },
160 interfaces=[character_interface],
161 description="A humanoid creature in the Star Wars universe.",
162 )
163
164 # The other type of character in Star Wars is a droid.
165 #
166 # This implements the following type system shorthand:
167 # type Droid : Character {
168 # id: String!
169 # name: String
170 # friends: [Character]
171 # appearsIn: [Episode]
172 # secretBackstory: String
173 # primaryFunction: String
174 # }
175
176 droid_type = GraphQLObjectType(
177 "Droid",
178 lambda: {
179 "id": GraphQLField(
180 GraphQLNonNull(GraphQLString), description="The id of the droid."
181 ),
182 "name": GraphQLField(GraphQLString, description="The name of the droid."),
183 "friends": GraphQLField(
184 GraphQLList(character_interface),
185 description="The friends of the droid,"
186 " or an empty list if they have none.",
187 resolve=lambda droid, _info: get_friends(droid),
188 ),
189 "appearsIn": GraphQLField(
190 GraphQLList(episode_enum), description="Which movies they appear in."
191 ),
192 "secretBackstory": GraphQLField(
193 GraphQLString,
194 resolve=lambda droid, _info: get_secret_backstory(droid),
195 description="Construction date and the name of the designer.",
196 ),
197 "primaryFunction": GraphQLField(
198 GraphQLString, description="The primary function of the droid."
199 ),
200 },
201 interfaces=[character_interface],
202 description="A mechanical creature in the Star Wars universe.",
203 )
204
205 # This is the type that will be the root of our query, and the
206 # entry point into our schema. It gives us the ability to fetch
207 # objects by their IDs, as well as to fetch the undisputed hero
208 # of the Star Wars trilogy, R2-D2, directly.
209 #
210 # This implements the following type system shorthand:
211 # type Query {
212 # hero(episode: Episode): Character
213 # human(id: String!): Human
214 # droid(id: String!): Droid
215 # }
216
217 # noinspection PyShadowingBuiltins
218 query_type = GraphQLObjectType(
219 "Query",
220 lambda: {
221 "hero": GraphQLField(
222 character_interface,
223 args={
224 "episode": GraphQLArgument(
225 episode_enum,
226 description=(
227 "If omitted, returns the hero of the whole saga."
228 " If provided, returns the hero of that particular episode."
229 ),
230 )
231 },
232 resolve=lambda _source, _info, episode=None: get_hero(episode),
233 ),
234 "human": GraphQLField(
235 human_type,
236 args={
237 "id": GraphQLArgument(
238 GraphQLNonNull(GraphQLString), description="id of the human"
239 )
240 },
241 resolve=lambda _source, _info, id: get_human(id),
242 ),
243 "droid": GraphQLField(
244 droid_type,
245 args={
246 "id": GraphQLArgument(
247 GraphQLNonNull(GraphQLString), description="id of the droid"
248 )
249 },
250 resolve=lambda _source, _info, id: get_droid(id),
251 ),
252 },
253 )
254
255 # Finally, we construct our schema (whose starting query type is the query
256 # type we defined above) and export it.
257
258 star_wars_schema = GraphQLSchema(query_type, types=[human_type, droid_type])