Skip to content

Commit

Permalink
Merge pull request graphql#70 from graphql/hero
Browse files Browse the repository at this point in the history
Allow an episode to be passed to hero
  • Loading branch information
dschafer committed Jul 16, 2015
2 parents ca57496 + c3de35b commit 18a9ac7
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 9 deletions.
6 changes: 5 additions & 1 deletion src/__tests__/starWarsData.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@
* JSON objects in a more complex demo.
*/

var luke = {
/**
* We export luke directly because the schema returns him
* from a root field, and hence needs to reference him.
*/
export var luke = {
id: '1000',
name: 'Luke Skywalker',
friends: ['1002', '1003', '2000', '2001'],
Expand Down
21 changes: 17 additions & 4 deletions src/__tests__/starWarsIntrospectionTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ describe('Star Wars Introspection Tests', () => {
{
name: 'Query'
},
{
name: 'Episode'
},
{
name: 'Character'
},
Expand All @@ -42,9 +45,6 @@ describe('Star Wars Introspection Tests', () => {
{
name: 'String'
},
{
name: 'Episode'
},
{
name: 'Droid'
},
Expand Down Expand Up @@ -327,7 +327,20 @@ describe('Star Wars Introspection Tests', () => {
fields: [
{
name: 'hero',
args: []
args: [
{
defaultValue: null,
description: 'If omitted, returns the hero of the whole ' +
'saga. If provided, returns the hero of ' +
'that particular episode.',
name: 'episode',
type: {
kind: 'ENUM',
name: 'Episode',
ofType: null
}
}
]
},
{
name: 'human',
Expand Down
19 changes: 19 additions & 0 deletions src/__tests__/starWarsQueryTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,5 +344,24 @@ describe('Star Wars Query Tests', () => {
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});

it('Allows us to verify that Luke is a human', async () => {
var query = `
query CheckTypeOfLuke {
hero(episode: EMPIRE) {
__typename
name
}
}
`;
var expected = {
hero: {
__typename: 'Human',
name: 'Luke Skywalker'
},
};
var result = await graphql(StarWarsSchema, query);
expect(result).to.deep.equal({ data: expected });
});
});
});
22 changes: 18 additions & 4 deletions src/__tests__/starWarsSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
GraphQLString,
} from '../type';

import { starWarsData, getFriends, artoo } from './starWarsData.js';
import { starWarsData, getFriends, artoo, luke } from './starWarsData.js';

/**
* This is designed to be an end-to-end test, demonstrating
Expand Down Expand Up @@ -60,7 +60,7 @@ import { starWarsData, getFriends, artoo } from './starWarsData.js';
* }
*
* type Query {
* hero: Character
* hero(episode: Episode): Character
* human(id: String!): Human
* droid(id: String!): Droid
* }
Expand Down Expand Up @@ -222,7 +222,7 @@ var droidType = new GraphQLObjectType({
*
* This implements the following type system shorthand:
* type Query {
* hero: Character
* hero(episode: Episode): Character
* human(id: String!): Human
* droid(id: String!): Droid
* }
Expand All @@ -233,7 +233,21 @@ var queryType = new GraphQLObjectType({
fields: () => ({
hero: {
type: characterInterface,
resolve: () => artoo,
args: {
episode: {
description: 'If omitted, returns the hero of the whole saga. If ' +
'provided, returns the hero of that particular episode.',
type: episodeEnum
}
},
resolve: (root, {episode}) => {
if (episode === 5) {
// Luke is the hero of Episode V.
return luke;
}
// Artoo is the hero otherwise.
return artoo;
}
},
human: {
type: humanType,
Expand Down

0 comments on commit 18a9ac7

Please sign in to comment.