From c3de35b5a93f5209982da302928e62b001671649 Mon Sep 17 00:00:00 2001 From: dschafer Date: Thu, 16 Jul 2015 16:31:53 -0700 Subject: [PATCH] Allow an episode to be passed to hero --- src/__tests__/starWarsData.js | 6 +++++- src/__tests__/starWarsIntrospectionTests.js | 21 ++++++++++++++++---- src/__tests__/starWarsQueryTests.js | 19 ++++++++++++++++++ src/__tests__/starWarsSchema.js | 22 +++++++++++++++++---- 4 files changed, 59 insertions(+), 9 deletions(-) diff --git a/src/__tests__/starWarsData.js b/src/__tests__/starWarsData.js index 18a5691ed9..d68d7b0775 100644 --- a/src/__tests__/starWarsData.js +++ b/src/__tests__/starWarsData.js @@ -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'], diff --git a/src/__tests__/starWarsIntrospectionTests.js b/src/__tests__/starWarsIntrospectionTests.js index 6e5aaa56dc..e342012f69 100644 --- a/src/__tests__/starWarsIntrospectionTests.js +++ b/src/__tests__/starWarsIntrospectionTests.js @@ -33,6 +33,9 @@ describe('Star Wars Introspection Tests', () => { { name: 'Query' }, + { + name: 'Episode' + }, { name: 'Character' }, @@ -42,9 +45,6 @@ describe('Star Wars Introspection Tests', () => { { name: 'String' }, - { - name: 'Episode' - }, { name: 'Droid' }, @@ -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', diff --git a/src/__tests__/starWarsQueryTests.js b/src/__tests__/starWarsQueryTests.js index 8f0859e612..26ab2b9cc4 100644 --- a/src/__tests__/starWarsQueryTests.js +++ b/src/__tests__/starWarsQueryTests.js @@ -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 }); + }); }); }); diff --git a/src/__tests__/starWarsSchema.js b/src/__tests__/starWarsSchema.js index 5d254328f0..5144b3ad04 100644 --- a/src/__tests__/starWarsSchema.js +++ b/src/__tests__/starWarsSchema.js @@ -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 @@ -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 * } @@ -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 * } @@ -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,