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
38 changes: 30 additions & 8 deletions src/__tests__/starWarsData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
34 changes: 18 additions & 16 deletions src/__tests__/starWarsSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ import {
} from '../mutation/mutation.js';

import {
rebels,
empire,
data,
getNewShipId,
getFaction,
getShip,
getRebels,
getEmpire,
createShip,
} from './starWarsData.js';

/**
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
),
}
Expand All @@ -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
})
Expand Down Expand Up @@ -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,
Expand Down