Skip to content

Commit 18a9ac7

Browse files
committed
Merge pull request #70 from graphql/hero
Allow an episode to be passed to hero
2 parents ca57496 + c3de35b commit 18a9ac7

File tree

4 files changed

+59
-9
lines changed

4 files changed

+59
-9
lines changed

src/__tests__/starWarsData.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313
* JSON objects in a more complex demo.
1414
*/
1515

16-
var luke = {
16+
/**
17+
* We export luke directly because the schema returns him
18+
* from a root field, and hence needs to reference him.
19+
*/
20+
export var luke = {
1721
id: '1000',
1822
name: 'Luke Skywalker',
1923
friends: ['1002', '1003', '2000', '2001'],

src/__tests__/starWarsIntrospectionTests.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ describe('Star Wars Introspection Tests', () => {
3333
{
3434
name: 'Query'
3535
},
36+
{
37+
name: 'Episode'
38+
},
3639
{
3740
name: 'Character'
3841
},
@@ -42,9 +45,6 @@ describe('Star Wars Introspection Tests', () => {
4245
{
4346
name: 'String'
4447
},
45-
{
46-
name: 'Episode'
47-
},
4848
{
4949
name: 'Droid'
5050
},
@@ -327,7 +327,20 @@ describe('Star Wars Introspection Tests', () => {
327327
fields: [
328328
{
329329
name: 'hero',
330-
args: []
330+
args: [
331+
{
332+
defaultValue: null,
333+
description: 'If omitted, returns the hero of the whole ' +
334+
'saga. If provided, returns the hero of ' +
335+
'that particular episode.',
336+
name: 'episode',
337+
type: {
338+
kind: 'ENUM',
339+
name: 'Episode',
340+
ofType: null
341+
}
342+
}
343+
]
331344
},
332345
{
333346
name: 'human',

src/__tests__/starWarsQueryTests.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,5 +344,24 @@ describe('Star Wars Query Tests', () => {
344344
var result = await graphql(StarWarsSchema, query);
345345
expect(result).to.deep.equal({ data: expected });
346346
});
347+
348+
it('Allows us to verify that Luke is a human', async () => {
349+
var query = `
350+
query CheckTypeOfLuke {
351+
hero(episode: EMPIRE) {
352+
__typename
353+
name
354+
}
355+
}
356+
`;
357+
var expected = {
358+
hero: {
359+
__typename: 'Human',
360+
name: 'Luke Skywalker'
361+
},
362+
};
363+
var result = await graphql(StarWarsSchema, query);
364+
expect(result).to.deep.equal({ data: expected });
365+
});
347366
});
348367
});

src/__tests__/starWarsSchema.js

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
GraphQLString,
1818
} from '../type';
1919

20-
import { starWarsData, getFriends, artoo } from './starWarsData.js';
20+
import { starWarsData, getFriends, artoo, luke } from './starWarsData.js';
2121

2222
/**
2323
* This is designed to be an end-to-end test, demonstrating
@@ -60,7 +60,7 @@ import { starWarsData, getFriends, artoo } from './starWarsData.js';
6060
* }
6161
*
6262
* type Query {
63-
* hero: Character
63+
* hero(episode: Episode): Character
6464
* human(id: String!): Human
6565
* droid(id: String!): Droid
6666
* }
@@ -222,7 +222,7 @@ var droidType = new GraphQLObjectType({
222222
*
223223
* This implements the following type system shorthand:
224224
* type Query {
225-
* hero: Character
225+
* hero(episode: Episode): Character
226226
* human(id: String!): Human
227227
* droid(id: String!): Droid
228228
* }
@@ -233,7 +233,21 @@ var queryType = new GraphQLObjectType({
233233
fields: () => ({
234234
hero: {
235235
type: characterInterface,
236-
resolve: () => artoo,
236+
args: {
237+
episode: {
238+
description: 'If omitted, returns the hero of the whole saga. If ' +
239+
'provided, returns the hero of that particular episode.',
240+
type: episodeEnum
241+
}
242+
},
243+
resolve: (root, {episode}) => {
244+
if (episode === 5) {
245+
// Luke is the hero of Episode V.
246+
return luke;
247+
}
248+
// Artoo is the hero otherwise.
249+
return artoo;
250+
}
237251
},
238252
human: {
239253
type: humanType,

0 commit comments

Comments
 (0)