1
1
// @flow strict
2
2
3
+ /**
4
+ * These are types which correspond to the schema.
5
+ * They represent the shape of the data visited during field resolution.
6
+ */
7
+ export type Character = {
8
+ id : string ,
9
+ name : string ,
10
+ friends : Array < string > ,
11
+ appearsIn : Array < number > ,
12
+ ...
13
+ } ;
14
+
15
+ export type Human = { |
16
+ type : 'Human' ,
17
+ id : string ,
18
+ name : string ,
19
+ friends : Array < string > ,
20
+ appearsIn : Array < number > ,
21
+ homePlanet ? : string ,
22
+ | } ;
23
+
24
+ export type Droid = { |
25
+ type : 'Droid' ,
26
+ id : string ,
27
+ name : string ,
28
+ friends : Array < string > ,
29
+ appearsIn : Array < number > ,
30
+ primaryFunction : string ,
31
+ | } ;
32
+
3
33
/**
4
34
* This defines a basic set of data for our Star Wars Schema.
5
35
*
8
38
* JSON objects in a more complex demo.
9
39
*/
10
40
11
- const luke = {
41
+ const luke : Human = {
12
42
type : 'Human' ,
13
43
id : '1000' ,
14
44
name : 'Luke Skywalker' ,
@@ -17,7 +47,7 @@ const luke = {
17
47
homePlanet : 'Tatooine' ,
18
48
} ;
19
49
20
- const vader = {
50
+ const vader : Human = {
21
51
type : 'Human' ,
22
52
id : '1001' ,
23
53
name : 'Darth Vader' ,
@@ -26,15 +56,15 @@ const vader = {
26
56
homePlanet : 'Tatooine' ,
27
57
} ;
28
58
29
- const han = {
59
+ const han : Human = {
30
60
type : 'Human' ,
31
61
id : '1002' ,
32
62
name : 'Han Solo' ,
33
63
friends : [ '1000' , '1003' , '2001' ] ,
34
64
appearsIn : [ 4 , 5 , 6 ] ,
35
65
} ;
36
66
37
- const leia = {
67
+ const leia : Human = {
38
68
type : 'Human' ,
39
69
id : '1003' ,
40
70
name : 'Leia Organa' ,
@@ -43,23 +73,23 @@ const leia = {
43
73
homePlanet : 'Alderaan' ,
44
74
} ;
45
75
46
- const tarkin = {
76
+ const tarkin : Human = {
47
77
type : 'Human' ,
48
78
id : '1004' ,
49
79
name : 'Wilhuff Tarkin' ,
50
80
friends : [ '1001' ] ,
51
81
appearsIn : [ 4 ] ,
52
82
} ;
53
83
54
- const humanData = {
84
+ const humanData : { | [ id : string ] : Human | } = {
55
85
'1000' : luke ,
56
86
'1001' : vader ,
57
87
'1002' : han ,
58
88
'1003' : leia ,
59
89
'1004' : tarkin ,
60
90
} ;
61
91
62
- const threepio = {
92
+ const threepio : Droid = {
63
93
type : 'Droid' ,
64
94
id : '2000' ,
65
95
name : 'C-3PO' ,
@@ -68,7 +98,7 @@ const threepio = {
68
98
primaryFunction : 'Protocol' ,
69
99
} ;
70
100
71
- const artoo = {
101
+ const artoo : Droid = {
72
102
type : 'Droid' ,
73
103
id : '2001' ,
74
104
name : 'R2-D2' ,
@@ -77,53 +107,25 @@ const artoo = {
77
107
primaryFunction : 'Astromech' ,
78
108
} ;
79
109
80
- const droidData = {
110
+ const droidData : { | [ id : string ] : Droid | } = {
81
111
'2000' : threepio ,
82
112
'2001' : artoo ,
83
113
} ;
84
114
85
- /**
86
- * These are Flow types which correspond to the schema.
87
- * They represent the shape of the data visited during field resolution.
88
- */
89
- export type Character = {
90
- id : string ,
91
- name : string ,
92
- friends : Array < string > ,
93
- appearsIn : Array < number > ,
94
- ...
95
- } ;
96
-
97
- export type Human = { |
98
- type : 'Human' ,
99
- id : string ,
100
- name : string ,
101
- friends : Array < string > ,
102
- appearsIn : Array < number > ,
103
- homePlanet : string ,
104
- | } ;
105
-
106
- export type Droid = { |
107
- type : 'Droid' ,
108
- id : string ,
109
- name : string ,
110
- friends : Array < string > ,
111
- appearsIn : Array < number > ,
112
- primaryFunction : string ,
113
- | } ;
114
-
115
115
/**
116
116
* Helper function to get a character by ID.
117
117
*/
118
- function getCharacter ( id ) {
118
+ function getCharacter ( id : string ) : Promise < Character | null > {
119
119
// Returning a promise just to illustrate that GraphQL.js supports it.
120
120
return Promise . resolve ( humanData [ id ] ?? droidData [ id ] ) ;
121
121
}
122
122
123
123
/**
124
124
* Allows us to query for a character's friends.
125
125
*/
126
- export function getFriends ( character : Character ) : Array < Promise < Character >> {
126
+ export function getFriends (
127
+ character : Character ,
128
+ ) : Array < Promise < Character | null >> {
127
129
// Notice that GraphQL accepts Arrays of Promises.
128
130
return character . friends . map ( ( id ) => getCharacter ( id ) ) ;
129
131
}
@@ -143,13 +145,13 @@ export function getHero(episode: number): Character {
143
145
/**
144
146
* Allows us to query for the human with the given id.
145
147
*/
146
- export function getHuman ( id : string ) : Human {
148
+ export function getHuman ( id : string ) : Human | null {
147
149
return humanData [ id ] ;
148
150
}
149
151
150
152
/**
151
153
* Allows us to query for the droid with the given id.
152
154
*/
153
- export function getDroid ( id : string ) : Droid {
155
+ export function getDroid ( id : string ) : Droid | null {
154
156
return droidData [ id ] ;
155
157
}
0 commit comments