All files / examples/starwars-ts data.ts

90.91% Statements 20/22
50% Branches 2/4
83.33% Functions 5/6
90.48% Lines 19/21
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118  1x 1x             1x             1x           1x             1x           1x             1x             1x             1x         3x     3x   1x   1x     1x   1x   4x   1x       1x                                                                        
import { Character, Human, Droid } from './schema';
 
const luke: Human = {
  id: '1000',
  name: 'Luke Skywalker',
  friendIds: ['1002', '1003', '2000', '2001'],
  appearsIn: [4, 5, 6],
  homePlanet: 'Tatooine'
};
 
const vader: Human = {
  id: '1001',
  name: 'Darth Vader',
  friendIds: ['1004'],
  appearsIn: [4, 5, 6],
  homePlanet: 'Tatooine'
};
 
const han: Human = {
  id: '1002',
  name: 'Han Solo',
  friendIds: ['1000', '1003', '2001'],
  appearsIn: [4, 5, 6]
};
 
const leia: Human = {
  id: '1003',
  name: 'Leia Organa',
  friendIds: ['1000', '1002', '2000', '2001'],
  appearsIn: [4, 5, 6],
  homePlanet: 'Alderaan'
};
 
const tarkin: Human = {
  id: '1004',
  name: 'Wilhuff Tarkin',
  friendIds: ['1001'],
  appearsIn: [4]
};
 
const humanData: {
  [key: string]: Human;
} = {
  1000: luke,
  1001: vader,
  1002: han,
  1003: leia,
  1004: tarkin
};
 
const threepio: Droid = {
  id: '2000',
  name: 'C-3PO',
  friendIds: ['1000', '1002', '1003', '2001'],
  appearsIn: [4, 5, 6],
  primaryFunction: 'Protocol'
};
 
const artoo: Droid = {
  id: '2001',
  name: 'R2-D2',
  friendIds: ['1000', '1002', '1003'],
  appearsIn: [4, 5, 6],
  primaryFunction: 'Astromech'
};
 
const droidData: {
  [key: string]: Droid;
} = I{
  2000: threepio,
  2001: artoo
};
 
/**
 * Helper function to get a character by ID.
 */
function getCharacter(id: string): Human | Droid {
  // Returning a promise just to illustrate GraphQL.js's support.
  return humanData[id] || droidData[id];
}
// function getCharacter(id: string): Promise<Human | Droid> {
//   // Returning a promise just to illustrate GraphQL.js's support.
//   return Promise.resolve(humanData[id] || droidData[id]);
// }
 
/**
 * Allows us to query for a character's friendIds.
 */
export function getFriends(character: Character): (Human | Droid)[] {
  return character.friendIds.map(id => getCharacter(id));
}
 
/**
 * Allows us to fetch the undisputed hero of the Star Wars trilogy, R2-D2.
 */
export function getHero(episode: number): Human | Droid {
  if (episode === 5) {
    // Luke is the hero of Episode V.
    return luke;
  }
  // Artoo is the hero otherwise.
  return artoo;
}
 
/**
 * Allows us to query for the human with the given 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: string): Droid {
  return droidData[id];
}