1
+ /* @flow */
1
2
/**
2
3
* Copyright (c) 2015, Facebook, Inc.
3
4
* All rights reserved.
14
15
*/
15
16
16
17
const luke = {
18
+ type : 'Human' ,
17
19
id : '1000' ,
18
20
name : 'Luke Skywalker' ,
19
21
friends : [ '1002' , '1003' , '2000' , '2001' ] ,
@@ -22,6 +24,7 @@ const luke = {
22
24
} ;
23
25
24
26
const vader = {
27
+ type : 'Human' ,
25
28
id : '1001' ,
26
29
name : 'Darth Vader' ,
27
30
friends : [ '1004' ] ,
@@ -30,13 +33,15 @@ const vader = {
30
33
} ;
31
34
32
35
const han = {
36
+ type : 'Human' ,
33
37
id : '1002' ,
34
38
name : 'Han Solo' ,
35
39
friends : [ '1000' , '1003' , '2001' ] ,
36
40
appearsIn : [ 4 , 5 , 6 ] ,
37
41
} ;
38
42
39
43
const leia = {
44
+ type : 'Human' ,
40
45
id : '1003' ,
41
46
name : 'Leia Organa' ,
42
47
friends : [ '1000' , '1002' , '2000' , '2001' ] ,
@@ -45,6 +50,7 @@ const leia = {
45
50
} ;
46
51
47
52
const tarkin = {
53
+ type : 'Human' ,
48
54
id : '1004' ,
49
55
name : 'Wilhuff Tarkin' ,
50
56
friends : [ '1001' ] ,
@@ -60,6 +66,7 @@ const humanData = {
60
66
} ;
61
67
62
68
const threepio = {
69
+ type : 'Droid' ,
63
70
id : '2000' ,
64
71
name : 'C-3PO' ,
65
72
friends : [ '1000' , '1002' , '1003' , '2001' ] ,
@@ -68,6 +75,7 @@ const threepio = {
68
75
} ;
69
76
70
77
const artoo = {
78
+ type : 'Droid' ,
71
79
id : '2001' ,
72
80
name : 'R2-D2' ,
73
81
friends : [ '1000' , '1002' , '1003' ] ,
@@ -80,6 +88,35 @@ const droidData = {
80
88
'2001' : artoo ,
81
89
} ;
82
90
91
+ /**
92
+ * These are Flow types which correspond to the schema.
93
+ * They represent the shape of the data visited during field resolution.
94
+ */
95
+ export type Character = {
96
+ id : string ,
97
+ name : string ,
98
+ friends : Array < string > ,
99
+ appearsIn : Array < number > ,
100
+ } ;
101
+
102
+ export type Human = {
103
+ type : 'Human' ,
104
+ id : string ,
105
+ name : string ,
106
+ friends : Array < string > ,
107
+ appearsIn : Array < number > ,
108
+ homePlanet : string ,
109
+ } ;
110
+
111
+ export type Droid = {
112
+ type : 'Droid' ,
113
+ id : string ,
114
+ name : string ,
115
+ friends : Array < string > ,
116
+ appearsIn : Array < number > ,
117
+ primaryFunction : string
118
+ } ;
119
+
83
120
/**
84
121
* Helper function to get a character by ID.
85
122
*/
@@ -91,14 +128,15 @@ function getCharacter(id) {
91
128
/**
92
129
* Allows us to query for a character's friends.
93
130
*/
94
- export function getFriends ( character ) {
131
+ export function getFriends ( character : Character ) : Array < Promise < Character >> {
132
+ // Notice that GraphQL accepts Arrays of Promises.
95
133
return character . friends . map ( id => getCharacter ( id ) ) ;
96
134
}
97
135
98
136
/**
99
137
* Allows us to fetch the undisputed hero of the Star Wars trilogy, R2-D2.
100
138
*/
101
- export function getHero ( episode ) {
139
+ export function getHero ( episode : number ) : Character {
102
140
if ( episode === 5 ) {
103
141
// Luke is the hero of Episode V.
104
142
return luke ;
@@ -110,13 +148,13 @@ export function getHero(episode) {
110
148
/**
111
149
* Allows us to query for the human with the given id.
112
150
*/
113
- export function getHuman ( id ) {
151
+ export function getHuman ( id : string ) : Human {
114
152
return humanData [ id ] ;
115
153
}
116
154
117
155
/**
118
156
* Allows us to query for the droid with the given id.
119
157
*/
120
- export function getDroid ( id ) {
158
+ export function getDroid ( id : string ) : Droid {
121
159
return droidData [ id ] ;
122
160
}
0 commit comments