-
Notifications
You must be signed in to change notification settings - Fork 493
/
starwars.go
567 lines (507 loc) · 12.8 KB
/
starwars.go
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
// Package starwars provides a example schema and resolver based on Star Wars characters.
//
// Source: https://github.com/graphql/graphql.github.io/blob/source/site/_core/swapiSchema.js
package starwars
import (
"encoding/base64"
"fmt"
"strconv"
"strings"
graphql "github.com/neelance/graphql-go"
)
var schemaIDL = `
schema {
query: Query
mutation: Mutation
}
# The query type, represents all of the entry points into our object graph
type Query {
hero(episode: Episode = NEWHOPE): 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
}
# 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 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
`
type human struct {
ID graphql.ID
Name string
Friends []graphql.ID
AppearsIn []string
Height float64
Mass int
Starships []graphql.ID
}
var humans = []*human{
{
ID: "1000",
Name: "Luke Skywalker",
Friends: []graphql.ID{"1002", "1003", "2000", "2001"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
Height: 1.72,
Mass: 77,
Starships: []graphql.ID{"3001", "3003"},
},
{
ID: "1001",
Name: "Darth Vader",
Friends: []graphql.ID{"1004"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
Height: 2.02,
Mass: 136,
Starships: []graphql.ID{"3002"},
},
{
ID: "1002",
Name: "Han Solo",
Friends: []graphql.ID{"1000", "1003", "2001"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
Height: 1.8,
Mass: 80,
Starships: []graphql.ID{"3000", "3003"},
},
{
ID: "1003",
Name: "Leia Organa",
Friends: []graphql.ID{"1000", "1002", "2000", "2001"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
Height: 1.5,
Mass: 49,
},
{
ID: "1004",
Name: "Wilhuff Tarkin",
Friends: []graphql.ID{"1001"},
AppearsIn: []string{"NEWHOPE"},
Height: 1.8,
Mass: 0,
},
}
var humanData = make(map[graphql.ID]*human)
func init() {
for _, h := range humans {
humanData[h.ID] = h
}
}
type droid struct {
ID graphql.ID
Name string
Friends []graphql.ID
AppearsIn []string
PrimaryFunction string
}
var droids = []*droid{
{
ID: "2000",
Name: "C-3PO",
Friends: []graphql.ID{"1000", "1002", "1003", "2001"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
PrimaryFunction: "Protocol",
},
{
ID: "2001",
Name: "R2-D2",
Friends: []graphql.ID{"1000", "1002", "1003"},
AppearsIn: []string{"NEWHOPE", "EMPIRE", "JEDI"},
PrimaryFunction: "Astromech",
},
}
var droidData = make(map[graphql.ID]*droid)
func init() {
for _, d := range droids {
droidData[d.ID] = d
}
}
type starship struct {
ID graphql.ID
Name string
Length float64
}
var starships = []*starship{
{
ID: "3000",
Name: "Millennium 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,
},
}
var starshipData = make(map[graphql.ID]*starship)
func init() {
for _, s := range starships {
starshipData[s.ID] = s
}
}
type review struct {
Stars int32
Commentary *string
}
var reviews = make(map[string][]*review)
type root struct{}
func Schema() *graphql.Schema {
b := graphql.ParseSchema(schemaIDL)
b.Resolvers("Query", (*root)(nil), map[string]interface{}{
"hero": func(r *root, args struct{ Episode string }) interface{} {
if args.Episode == "EMPIRE" {
return humanData["1000"]
}
return droidData["2001"]
},
"reviews": func(r *root, args struct{ Episode string }) []*review {
return reviews[args.Episode]
},
"search": func(r *root, args struct{ Text string }) []interface{} {
var l []interface{}
for _, h := range humans {
if strings.Contains(h.Name, args.Text) {
l = append(l, h)
}
}
for _, d := range droids {
if strings.Contains(d.Name, args.Text) {
l = append(l, d)
}
}
for _, s := range starships {
if strings.Contains(s.Name, args.Text) {
l = append(l, s)
}
}
return l
},
"character": func(r *root, args struct{ ID graphql.ID }) interface{} {
if h := humanData[args.ID]; h != nil {
return h
}
if d := droidData[args.ID]; d != nil {
return d
}
return nil
},
"human": func(r *root, args struct{ ID graphql.ID }) *human {
return humanData[args.ID]
},
"droid": func(r *root, args struct{ ID graphql.ID }) *droid {
return droidData[args.ID]
},
"starship": func(r *root, args struct{ ID graphql.ID }) *starship {
return starshipData[args.ID]
},
})
b.Resolvers("Mutation", (*root)(nil), map[string]interface{}{
"createReview": func(r *root, args *struct {
Episode string
Review *reviewInput
}) *review {
review := &review{
Stars: args.Review.Stars,
Commentary: args.Review.Commentary,
}
reviews[args.Episode] = append(reviews[args.Episode], review)
return review
},
})
b.Resolvers("Human", (*human)(nil), map[string]interface{}{
"id": "ID",
"name": "Name",
"appearsIn": "AppearsIn",
"height": func(h *human, args struct{ Unit string }) float64 {
return convertLength(h.Height, args.Unit)
},
"mass": func(h *human) *float64 {
if h.Mass == 0 {
return nil
}
f := float64(h.Mass)
return &f
},
"friends": func(h *human) *[]interface{} {
return resolveCharacters(h.Friends)
},
"friendsConnection": func(h *human, args *friendsConnectionArgs) (*friendsConnection, error) {
return newFriendsConnection(h.Friends, args)
},
"starships": func(h *human) *[]*starship {
l := make([]*starship, len(h.Starships))
for i, id := range h.Starships {
l[i] = starshipData[id]
}
return &l
},
})
b.Resolvers("Droid", (*droid)(nil), map[string]interface{}{
"id": "ID",
"name": "Name",
"appearsIn": "AppearsIn",
"friends": func(d *droid) *[]interface{} {
return resolveCharacters(d.Friends)
},
"friendsConnection": func(d *droid, args *friendsConnectionArgs) (*friendsConnection, error) {
return newFriendsConnection(d.Friends, args)
},
"primaryFunction": func(d *droid) *string {
if d.PrimaryFunction == "" {
return nil
}
return &d.PrimaryFunction
},
})
b.Resolvers("Starship", (*starship)(nil), map[string]interface{}{
"id": "ID",
"name": "Name",
"length": func(s *starship, args struct{ Unit string }) float64 {
return convertLength(s.Length, args.Unit)
},
})
b.Resolvers("Review", (*review)(nil), map[string]interface{}{
"stars": "Stars",
"commentary": "Commentary",
})
b.Resolvers("FriendsConnection", (*friendsConnection)(nil), map[string]interface{}{
"totalCount": func(c *friendsConnection) int32 {
return int32(len(c.IDs))
},
"edges": func(c *friendsConnection) *[]*friendsEdge {
l := make([]*friendsEdge, c.To-c.From)
for i := range l {
l[i] = &friendsEdge{
Cursor: encodeCursor(c.From + i),
ID: c.IDs[c.From+i],
}
}
return &l
},
"friends": func(c *friendsConnection) *[]interface{} {
return resolveCharacters(c.IDs[c.From:c.To])
},
"pageInfo": func(c *friendsConnection) *pageInfo {
start := encodeCursor(c.From)
end := encodeCursor(c.To - 1)
return &pageInfo{
StartCursor: &start,
EndCursor: &end,
HasNextPage: c.To < len(c.IDs),
}
},
})
b.Resolvers("FriendsEdge", (*friendsEdge)(nil), map[string]interface{}{
"cursor": "Cursor",
"node": func(e *friendsEdge) interface{} {
return resolveCharacter(e.ID)
},
})
b.Resolvers("PageInfo", (*pageInfo)(nil), map[string]interface{}{
"startCursor": "StartCursor",
"endCursor": "EndCursor",
"hasNextPage": "HasNextPage",
})
return b.Build(&root{})
}
func convertLength(meters float64, unit string) float64 {
switch unit {
case "METER":
return meters
case "FOOT":
return meters * 3.28084
default:
panic("invalid unit")
}
}
func resolveCharacters(ids []graphql.ID) *[]interface{} {
var characters []interface{}
for _, id := range ids {
if c := resolveCharacter(id); c != nil {
characters = append(characters, c)
}
}
return &characters
}
func resolveCharacter(id graphql.ID) interface{} {
if h, ok := humanData[id]; ok {
return h
}
if d, ok := droidData[id]; ok {
return d
}
return nil
}
type friendsConnectionArgs struct {
First *int32
After *graphql.ID
}
type friendsConnection struct {
IDs []graphql.ID
From int
To int
}
type friendsEdge struct {
Cursor graphql.ID
ID graphql.ID
}
func newFriendsConnection(ids []graphql.ID, args *friendsConnectionArgs) (*friendsConnection, error) {
from := 0
if args.After != nil {
b, err := base64.StdEncoding.DecodeString(string(*args.After))
if err != nil {
return nil, err
}
i, err := strconv.Atoi(strings.TrimPrefix(string(b), "cursor"))
if err != nil {
return nil, err
}
from = i
}
to := len(ids)
if args.First != nil {
to = from + int(*args.First)
if to > len(ids) {
to = len(ids)
}
}
return &friendsConnection{
IDs: ids,
From: from,
To: to,
}, nil
}
func encodeCursor(i int) graphql.ID {
return graphql.ID(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("cursor%d", i+1))))
}
type pageInfo struct {
StartCursor *graphql.ID
EndCursor *graphql.ID
HasNextPage bool
}
type reviewInput struct {
Stars int32
Commentary *string
}