Skip to content

Commit

Permalink
Flow type tests & reorganize
Browse files Browse the repository at this point in the history
This cleans up the top level tests and flow-types them. It also moved one unrelated test into a more appropriate location.

Inspired by #425
  • Loading branch information
leebyron committed Nov 16, 2016
1 parent 1fc43c3 commit 59a6172
Show file tree
Hide file tree
Showing 6 changed files with 364 additions and 299 deletions.
46 changes: 42 additions & 4 deletions src/__tests__/starWarsData.js
@@ -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
@@ -1,3 +1,4 @@
/* @flow */
/**
* Copyright (c) 2015, Facebook, Inc.
* All rights reserved.
Expand Down

0 comments on commit 59a6172

Please sign in to comment.