diff --git a/src/__tests__/starWarsData.js b/src/__tests__/starWarsData.js index e3eca33..136f4a7 100644 --- a/src/__tests__/starWarsData.js +++ b/src/__tests__/starWarsData.js @@ -55,24 +55,19 @@ var executor = { name: 'Executor', }; -var nextShip = 9; -export function getNewShipId() { - return '' + (nextShip++); -} - -export var rebels = { +var rebels = { id: '1', name: 'Alliance to Restore the Republic', ships: ['1', '2', '3', '4', '5'] }; -export var empire = { +var empire = { id: '2', name: 'Galactic Empire', ships: ['6', '7', '8'] }; -export var data = { +var data = { Faction: { 1: rebels, 2: empire @@ -88,3 +83,30 @@ export var data = { 8: executor } }; + +var nextShip = 9; +export function createShip(shipName, factionId) { + var newShip = { + id: '' + (nextShip++), + name: shipName + }; + data.Ship[newShip.id] = newShip; + data.Faction[factionId].ships.push(newShip.id); + return newShip; +} + +export function getShip(id) { + return data.Ship[id]; +} + +export function getFaction(id) { + return data.Faction[id]; +} + +export function getRebels() { + return rebels; +} + +export function getEmpire() { + return empire; +} diff --git a/src/__tests__/starWarsSchema.js b/src/__tests__/starWarsSchema.js index 227acd1..c73e661 100644 --- a/src/__tests__/starWarsSchema.js +++ b/src/__tests__/starWarsSchema.js @@ -35,10 +35,11 @@ import { } from '../mutation/mutation.js'; import { - rebels, - empire, - data, - getNewShipId, + getFaction, + getShip, + getRebels, + getEmpire, + createShip, } from './starWarsData.js'; /** @@ -125,7 +126,13 @@ import { var {nodeInterface, nodeField} = nodeDefinitions( (globalId) => { var {type, id} = fromGlobalId(globalId); - return data[type][id]; + if (type === 'Faction') { + return getFaction(id); + } else if (type === 'Ship') { + return getShip(id); + } else { + return null; + } }, (obj) => { return obj.ships ? factionType : shipType; @@ -197,7 +204,7 @@ var factionType = new GraphQLObjectType({ description: 'The ships used by the faction.', args: connectionArgs, resolve: (faction, args) => connectionFromArray( - faction.ships.map((id) => data.Ship[id]), + faction.ships.map((id) => getShip(id)), args ), } @@ -221,11 +228,11 @@ var queryType = new GraphQLObjectType({ fields: () => ({ rebels: { type: factionType, - resolve: () => rebels, + resolve: () => getRebels(), }, empire: { type: factionType, - resolve: () => empire, + resolve: () => getEmpire(), }, node: nodeField }) @@ -261,20 +268,15 @@ var shipMutation = mutationWithClientMutationId({ outputFields: { ship: { type: shipType, - resolve: (payload) => data['Ship'][payload.shipId] + resolve: (payload) => getShip(payload.shipId) }, faction: { type: factionType, - resolve: (payload) => data['Faction'][payload.factionId] + resolve: (payload) => getFaction(payload.factionId) } }, mutateAndGetPayload: ({shipName, factionId}) => { - var newShip = { - id: getNewShipId(), - name: shipName - }; - data.Ship[newShip.id] = newShip; - data.Faction[factionId].ships.push(newShip.id); + var newShip = createShip(shipName, factionId); return { shipId: newShip.id, factionId: factionId,