Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 42 additions & 4 deletions src/__tests__/starWarsData.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
Expand All @@ -14,6 +15,7 @@
*/

const luke = {
type: 'Human',
id: '1000',
name: 'Luke Skywalker',
friends: [ '1002', '1003', '2000', '2001' ],
Expand All @@ -22,6 +24,7 @@ const luke = {
};

const vader = {
type: 'Human',
id: '1001',
name: 'Darth Vader',
friends: [ '1004' ],
Expand All @@ -30,13 +33,15 @@ const vader = {
};

const han = {
type: 'Human',
id: '1002',
name: 'Han Solo',
friends: [ '1000', '1003', '2001' ],
appearsIn: [ 4, 5, 6 ],
};

const leia = {
type: 'Human',
id: '1003',
name: 'Leia Organa',
friends: [ '1000', '1002', '2000', '2001' ],
Expand All @@ -45,6 +50,7 @@ const leia = {
};

const tarkin = {
type: 'Human',
id: '1004',
name: 'Wilhuff Tarkin',
friends: [ '1001' ],
Expand All @@ -60,6 +66,7 @@ const humanData = {
};

const threepio = {
type: 'Droid',
id: '2000',
name: 'C-3PO',
friends: [ '1000', '1002', '1003', '2001' ],
Expand All @@ -68,6 +75,7 @@ const threepio = {
};

const artoo = {
type: 'Droid',
id: '2001',
name: 'R2-D2',
friends: [ '1000', '1002', '1003' ],
Expand All @@ -80,6 +88,35 @@ const droidData = {
'2001': artoo,
};

/**
* These are Flow types which correspond to the schema.
* They represent the shape of the data visited during field resolution.
*/
export type Character = {
id: string,
name: string,
friends: Array<string>,
appearsIn: Array<number>,
};

export type Human = {
type: 'Human',
id: string,
name: string,
friends: Array<string>,
appearsIn: Array<number>,
homePlanet: string,
};

export type Droid = {
type: 'Droid',
id: string,
name: string,
friends: Array<string>,
appearsIn: Array<number>,
primaryFunction: string
};

/**
* Helper function to get a character by ID.
*/
Expand All @@ -91,14 +128,15 @@ function getCharacter(id) {
/**
* Allows us to query for a character's friends.
*/
export function getFriends(character) {
export function getFriends(character: Character): Array<Promise<Character>> {
// Notice that GraphQL accepts Arrays of Promises.
return character.friends.map(id => getCharacter(id));
}

/**
* Allows us to fetch the undisputed hero of the Star Wars trilogy, R2-D2.
*/
export function getHero(episode) {
export function getHero(episode: number): Character {
if (episode === 5) {
// Luke is the hero of Episode V.
return luke;
Expand All @@ -110,13 +148,13 @@ export function getHero(episode) {
/**
* Allows us to query for the human with the given id.
*/
export function getHuman(id) {
export function getHuman(id: string): Human {
return humanData[id];
}

/**
* Allows us to query for the droid with the given id.
*/
export function getDroid(id) {
export function getDroid(id: string): Droid {
return droidData[id];
}
1 change: 1 addition & 0 deletions src/__tests__/starWarsIntrospection-test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
Expand Down
Loading