1+ /* @flow */
12/**
23 * Copyright (c) 2015, Facebook, Inc.
34 * All rights reserved.
1415 */
1516
1617const luke = {
18+ type : 'Human' ,
1719 id : '1000' ,
1820 name : 'Luke Skywalker' ,
1921 friends : [ '1002' , '1003' , '2000' , '2001' ] ,
@@ -22,6 +24,7 @@ const luke = {
2224} ;
2325
2426const vader = {
27+ type : 'Human' ,
2528 id : '1001' ,
2629 name : 'Darth Vader' ,
2730 friends : [ '1004' ] ,
@@ -30,13 +33,15 @@ const vader = {
3033} ;
3134
3235const han = {
36+ type : 'Human' ,
3337 id : '1002' ,
3438 name : 'Han Solo' ,
3539 friends : [ '1000' , '1003' , '2001' ] ,
3640 appearsIn : [ 4 , 5 , 6 ] ,
3741} ;
3842
3943const leia = {
44+ type : 'Human' ,
4045 id : '1003' ,
4146 name : 'Leia Organa' ,
4247 friends : [ '1000' , '1002' , '2000' , '2001' ] ,
@@ -45,6 +50,7 @@ const leia = {
4550} ;
4651
4752const tarkin = {
53+ type : 'Human' ,
4854 id : '1004' ,
4955 name : 'Wilhuff Tarkin' ,
5056 friends : [ '1001' ] ,
@@ -60,6 +66,7 @@ const humanData = {
6066} ;
6167
6268const threepio = {
69+ type : 'Droid' ,
6370 id : '2000' ,
6471 name : 'C-3PO' ,
6572 friends : [ '1000' , '1002' , '1003' , '2001' ] ,
@@ -68,6 +75,7 @@ const threepio = {
6875} ;
6976
7077const artoo = {
78+ type : 'Droid' ,
7179 id : '2001' ,
7280 name : 'R2-D2' ,
7381 friends : [ '1000' , '1002' , '1003' ] ,
@@ -80,6 +88,35 @@ const droidData = {
8088 '2001' : artoo ,
8189} ;
8290
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+
83120/**
84121 * Helper function to get a character by ID.
85122 */
@@ -91,14 +128,15 @@ function getCharacter(id) {
91128/**
92129 * Allows us to query for a character's friends.
93130 */
94- export function getFriends ( character ) {
131+ export function getFriends ( character : Character ) : Array < Promise < Character >> {
132+ // Notice that GraphQL accepts Arrays of Promises.
95133 return character . friends . map ( id => getCharacter ( id ) ) ;
96134}
97135
98136/**
99137 * Allows us to fetch the undisputed hero of the Star Wars trilogy, R2-D2.
100138 */
101- export function getHero ( episode ) {
139+ export function getHero ( episode : number ) : Character {
102140 if ( episode === 5 ) {
103141 // Luke is the hero of Episode V.
104142 return luke ;
@@ -110,13 +148,13 @@ export function getHero(episode) {
110148/**
111149 * Allows us to query for the human with the given id.
112150 */
113- export function getHuman ( id ) {
151+ export function getHuman ( id : string ) : Human {
114152 return humanData [ id ] ;
115153}
116154
117155/**
118156 * Allows us to query for the droid with the given id.
119157 */
120- export function getDroid ( id ) {
158+ export function getDroid ( id : string ) : Droid {
121159 return droidData [ id ] ;
122160}
0 commit comments