-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathswapi-schema.tsx
519 lines (450 loc) · 11.9 KB
/
swapi-schema.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
// @ts-nocheck
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the license found in the
* LICENSE file in the root directory of this source tree.
*/
import { Buffer } from "buffer"
import { makeExecutableSchema } from "@graphql-tools/schema"
const typeDefs = /* GraphQL */ `
schema {
query: Query
mutation: Mutation
}
"The query type, represents all of the entry points into our object graph"
type Query {
hero(episode: Episode): Character
reviews(episode: Episode!): [Review]
search(text: String): [SearchResult]
character(id: ID!): Character
droid(id: ID!): Droid
human(id: ID!): Human
starship(id: ID!): Starship
}
"The mutation type, represents all updates we can make to our data"
type Mutation {
createReview(episode: Episode, review: ReviewInput!): Review
rateFilm(episode: Episode!, rating: FilmRating!): Film
updateHumanName(id: ID!, name: String!): Human
deleteStarship(id: ID!): ID
}
"The episodes in the Star Wars trilogy"
enum Episode {
"Star Wars Episode IV: A New Hope, released in 1977."
NEWHOPE
"Star Wars Episode V: The Empire Strikes Back, released in 1980."
EMPIRE
"Star Wars Episode VI: Return of the Jedi, released in 1983."
JEDI
}
"A personal rating for a Star Wars episode"
enum FilmRating {
"Negative rating"
THUMBS_DOWN
"Positive rating"
THUMBS_UP
}
"A film from the Star Wars trilogy"
type Film {
"The Star Wars episode portrayed in the film"
episode: Episode!
"The authenticated user's rating of the film"
viewerRating: FilmRating
}
"A character from the Star Wars universe"
interface Character {
"The ID of the character"
id: ID!
"The name of the character"
name: String!
"The friends of the character, or an empty list if they have none"
friends: [Character]
"The friends of the character exposed as a connection with edges"
friendsConnection(first: Int, after: ID): FriendsConnection!
"The movies this character appears in"
appearsIn: [Episode]!
}
"Units of height"
enum LengthUnit {
"The standard unit around the world"
METER
"Primarily used in the United States"
FOOT
}
"A humanoid creature from the Star Wars universe"
type Human implements Character {
"The ID of the human"
id: ID!
"What this human calls themselves"
name: String!
"Height in the preferred unit, default is meters"
height(unit: LengthUnit = METER): Float
"Mass in kilograms, or null if unknown"
mass: Float
"This human's friends, or an empty list if they have none"
friends: [Character]
"The friends of the human exposed as a connection with edges"
friendsConnection(first: Int, after: ID): FriendsConnection!
"The movies this human appears in"
appearsIn: [Episode]!
"A list of starships this person has piloted, or an empty list if none"
starships: [Starship]
}
"An autonomous mechanical character in the Star Wars universe"
type Droid implements Character {
"The ID of the droid"
id: ID!
"What others call this droid"
name: String!
"This droid's friends, or an empty list if they have none"
friends: [Character]
"The friends of the droid exposed as a connection with edges"
friendsConnection(first: Int, after: ID): FriendsConnection!
"The movies this droid appears in"
appearsIn: [Episode]!
"This droid's primary function"
primaryFunction: String
}
"A connection object for a character's friends"
type FriendsConnection {
"The total number of friends"
totalCount: Int
"The edges for each of the character's friends."
edges: [FriendsEdge]
"A list of the friends, as a convenience when edges are not needed."
friends: [Character]
"Information for paginating this connection"
pageInfo: PageInfo!
}
"An edge object for a character's friends"
type FriendsEdge {
"A cursor used for pagination"
cursor: ID!
"The character represented by this friendship edge"
node: Character
}
"Information for paginating this connection"
type PageInfo {
startCursor: ID
endCursor: ID
hasNextPage: Boolean!
}
"Represents a review for a movie"
type Review {
"The number of stars this review gave, 1-5"
stars: Int!
"Comment about the movie"
commentary: String
}
"The input object sent when someone is creating a new review"
input ReviewInput {
"0-5 stars"
stars: Int!
"Comment about the movie, optional"
commentary: String
}
type Starship {
"The ID of the starship"
id: ID!
"The name of the starship"
name: String!
"Length of the starship, along the longest axis"
length(unit: LengthUnit = METER): Float
}
union SearchResult = Human | Droid | Starship
`
/**
* This defines a basic set of data for our Star Wars Schema.
*
* This data is hard coded for the sake of the demo, but you could imagine
* fetching this data from a backend service rather than from hardcoded
* JSON objects in a more complex demo.
*/
const humans = [
{
id: "1000",
name: "Luke Skywalker",
friends: ["1002", "1003", "2000", "2001"],
appearsIn: ["NEWHOPE", "EMPIRE", "JEDI"],
height: 1.72,
mass: 77,
starships: ["3001", "3003"],
},
{
id: "1001",
name: "Darth Vader",
friends: ["1004"],
appearsIn: ["NEWHOPE", "EMPIRE", "JEDI"],
height: 2.02,
mass: 136,
starships: ["3002"],
},
{
id: "1002",
name: "Han Solo",
friends: ["1000", "1003", "2001"],
appearsIn: ["NEWHOPE", "EMPIRE", "JEDI"],
height: 1.8,
mass: 80,
starships: ["3000", "3003"],
},
{
id: "1003",
name: "Leia Organa",
friends: ["1000", "1002", "2000", "2001"],
appearsIn: ["NEWHOPE", "EMPIRE", "JEDI"],
height: 1.5,
mass: 49,
starships: [],
},
{
id: "1004",
name: "Wilhuff Tarkin",
friends: ["1001"],
appearsIn: ["NEWHOPE"],
height: 1.8,
mass: null,
starships: [],
},
]
const humanData = {}
humans.forEach(ship => {
humanData[ship.id] = ship
})
const droids = [
{
id: "2000",
name: "C-3PO",
friends: ["1000", "1002", "1003", "2001"],
appearsIn: ["NEWHOPE", "EMPIRE", "JEDI"],
primaryFunction: "Protocol",
},
{
id: "2001",
name: "R2-D2",
friends: ["1000", "1002", "1003"],
appearsIn: ["NEWHOPE", "EMPIRE", "JEDI"],
primaryFunction: "Astromech",
},
]
const droidData = {}
droids.forEach(ship => {
droidData[ship.id] = ship
})
const starships = [
{
id: "3000",
name: "Millenium Falcon",
length: 34.37,
},
{
id: "3001",
name: "X-Wing",
length: 12.5,
},
{
id: "3002",
name: "TIE Advanced x1",
length: 9.2,
},
{
id: "3003",
name: "Imperial shuttle",
length: 20,
},
]
const starshipData = {}
starships.forEach(ship => {
starshipData[ship.id] = ship
})
/**
* Helper function to get a character by ID.
*/
function getCharacter(id) {
// Returning a promise just to illustrate GraphQL.js's support.
return Promise.resolve(humanData[id] || droidData[id])
}
/**
* Allows us to query for a character's friends.
*/
function getFriends(character) {
return character.friends.map(id => getCharacter(id))
}
/**
* Allows us to fetch the undisputed hero of the Star Wars trilogy, R2-D2.
*/
function getHero(episode) {
if (episode === "EMPIRE") {
// Luke is the hero of Episode V.
return humanData["1000"]
}
// Artoo is the hero otherwise.
return droidData["2001"]
}
/**
* Allows us to query for the human with the given id.
*/
function getHuman(id) {
return humanData[id]
}
/**
* Allows us to query for the droid with the given id.
*/
function getDroid(id) {
return droidData[id]
}
function getStarship(id) {
return starshipData[id]
}
function toCursor(str) {
return Buffer("cursor" + str).toString("base64")
}
function fromCursor(str) {
return Buffer.from(str, "base64").toString().slice(6)
}
const resolvers = {
Query: {
hero: (root, { episode }) => getHero(episode),
character: (root, { id }) => getCharacter(id),
human: (root, { id }) => getHuman(id),
droid: (root, { id }) => getDroid(id),
starship: (root, { id }) => getStarship(id),
reviews: () => null,
search: (root, { text }) => {
const re = new RegExp(text, "i")
const allData = [...humans, ...droids, ...starships]
return allData.filter(obj => re.test(obj.name))
},
},
Mutation: {
createReview: (root, { episode, review }) => review,
rateFilm: (root, { episode, rating }) => ({
episode,
viewerRating: rating,
}),
updateHumanName: (root, { id, name }) => {
const human = humanData[id]
if (!human) {
throw new Error("Human not found")
}
return { ...human, name }
},
deleteStarship: (root, { id }) => {
const starship = getStarship(id)
if (!starship) {
throw new Error("Starship not found")
}
return id
},
},
Character: {
__resolveType(data, context, info) {
if (humanData[data.id]) {
return "Human"
}
if (droidData[data.id]) {
return "Droid"
}
return null
},
},
Human: {
height: ({ height }, { unit }) => {
if (unit === "FOOT") {
return height * 3.28084
}
return height
},
friends: ({ friends }) => friends.map(getCharacter),
friendsConnection: ({ friends }, { first, after }) => {
first = first || friends.length
after = after ? parseInt(fromCursor(after), 10) : 0
const edges = friends
.map((friend, i) => ({
cursor: toCursor(i + 1),
node: getCharacter(friend),
}))
.slice(after, first + after)
const slicedFriends = edges.map(({ node }) => node)
return {
edges,
friends: slicedFriends,
pageInfo: {
startCursor: edges.length > 0 ? edges[0].cursor : null,
hasNextPage: first + after < friends.length,
endCursor: edges.length > 0 ? edges[edges.length - 1].cursor : null,
},
totalCount: friends.length,
}
},
starships: ({ starships }) => starships.map(getStarship),
appearsIn: ({ appearsIn }) => appearsIn,
},
Droid: {
friends: ({ friends }) => friends.map(getCharacter),
friendsConnection: ({ friends }, { first, after }) => {
first = first || friends.length
after = after ? parseInt(fromCursor(after), 10) : 0
const edges = friends
.map((friend, i) => ({
cursor: toCursor(i + 1),
node: getCharacter(friend),
}))
.slice(after, first + after)
const slicedFriends = edges.map(({ node }) => node)
return {
edges,
friends: slicedFriends,
pageInfo: {
startCursor: edges.length > 0 ? edges[0].cursor : null,
hasNextPage: first + after < friends.length,
endCursor: edges.length > 0 ? edges[edges.length - 1].cursor : null,
},
totalCount: friends.length,
}
},
appearsIn: ({ appearsIn }) => appearsIn,
},
FriendsConnection: {
edges: ({ edges }) => edges,
friends: ({ friends }) => friends,
pageInfo: ({ pageInfo }) => pageInfo,
totalCount: ({ totalCount }) => totalCount,
},
FriendsEdge: {
node: ({ node }) => node,
cursor: ({ cursor }) => cursor,
},
Starship: {
length: ({ length }, { unit }) => {
if (unit === "FOOT") {
return length * 3.28084
}
return length
},
},
SearchResult: {
__resolveType(data, context, info) {
if (humanData[data.id]) {
return "Human"
}
if (droidData[data.id]) {
return "Droid"
}
if (starshipData[data.id]) {
return "Starship"
}
return null
},
},
}
/**
* Finally, we construct our schema (whose starting query type is the query
* type we defined above) and export it.
*/
export const StarWarsSchema = makeExecutableSchema({
typeDefs,
resolvers,
})