diff --git a/src/execution/__tests__/abstract-test.js b/src/execution/__tests__/abstract-test.js index 2311fba97b..a4884fc7be 100644 --- a/src/execution/__tests__/abstract-test.js +++ b/src/execution/__tests__/abstract-test.js @@ -14,19 +14,27 @@ import { GraphQLUnionType, } from '../../type/definition'; +import { buildSchema } from '../../utilities/buildASTSchema'; + import { executeSync, execute } from '../execute'; -async function executeQuery(args: {| schema: GraphQLSchema, query: string |}) { - const { schema, query } = args; +async function executeQuery(args: {| + schema: GraphQLSchema, + query: string, + rootValue?: mixed, +|}) { + const { schema, query, rootValue } = args; const document = parse(query); const result = executeSync({ schema, document, + rootValue, contextValue: { async: false }, }); const asyncResult = await execute({ schema, document, + rootValue, contextValue: { async: true }, }); @@ -752,4 +760,134 @@ describe('Execute: Handles execution of abstract types', () => { ], }); }); + + it('resolve Union type using __typename on source object', async () => { + const schema = buildSchema(` + type Query { + pets: [Pet] + } + + union Pet = Cat | Dog + + type Cat { + name: String + meows: Boolean + } + + type Dog { + name: String + woofs: Boolean + } + `); + + const query = ` + { + pets { + name + ... on Dog { + woofs + } + ... on Cat { + meows + } + } + } + `; + + const rootValue = { + pets: [ + { + __typename: 'Dog', + name: 'Odie', + woofs: true, + }, + { + __typename: 'Cat', + name: 'Garfield', + meows: false, + }, + ], + }; + + expect(await executeQuery({ schema, query, rootValue })).to.deep.equal({ + data: { + pets: [ + { + name: 'Odie', + woofs: true, + }, + { + name: 'Garfield', + meows: false, + }, + ], + }, + }); + }); + + it('resolve Interface type using __typename on source object', async () => { + const schema = buildSchema(` + type Query { + pets: [Pet] + } + + interface Pet { + name: String + } + + type Cat implements Pet { + name: String + meows: Boolean + } + + type Dog implements Pet { + name: String + woofs: Boolean + } + `); + + const query = ` + { + pets { + name + ... on Dog { + woofs + } + ... on Cat { + meows + } + } + } + `; + + const rootValue = { + pets: [ + { + __typename: 'Dog', + name: 'Odie', + woofs: true, + }, + { + __typename: 'Cat', + name: 'Garfield', + meows: false, + }, + ], + }; + + expect(await executeQuery({ schema, query, rootValue })).to.deep.equal({ + data: { + pets: [ + { + name: 'Odie', + woofs: true, + }, + { + name: 'Garfield', + meows: false, + }, + ], + }, + }); + }); }); diff --git a/src/utilities/__tests__/buildASTSchema-test.js b/src/utilities/__tests__/buildASTSchema-test.js index f2a92c96a5..f364e311dd 100644 --- a/src/utilities/__tests__/buildASTSchema-test.js +++ b/src/utilities/__tests__/buildASTSchema-test.js @@ -505,129 +505,6 @@ describe('Schema Builder', () => { expect(errors).to.have.lengthOf.above(0); }); - it('Specifying Union type using __typename', () => { - const schema = buildSchema(` - type Query { - fruits: [Fruit] - } - - union Fruit = Apple | Banana - - type Apple { - color: String - } - - type Banana { - length: Int - } - `); - - const source = ` - { - fruits { - ... on Apple { - color - } - ... on Banana { - length - } - } - } - `; - - const rootValue = { - fruits: [ - { - color: 'green', - __typename: 'Apple', - }, - { - length: 5, - __typename: 'Banana', - }, - ], - }; - - expect(graphqlSync({ schema, source, rootValue })).to.deep.equal({ - data: { - fruits: [ - { - color: 'green', - }, - { - length: 5, - }, - ], - }, - }); - }); - - it('Specifying Interface type using __typename', () => { - const schema = buildSchema(` - type Query { - characters: [Character] - } - - interface Character { - name: String! - } - - type Human implements Character { - name: String! - totalCredits: Int - } - - type Droid implements Character { - name: String! - primaryFunction: String - } - `); - - const source = ` - { - characters { - name - ... on Human { - totalCredits - } - ... on Droid { - primaryFunction - } - } - } - `; - - const rootValue = { - characters: [ - { - name: 'Han Solo', - totalCredits: 10, - __typename: 'Human', - }, - { - name: 'R2-D2', - primaryFunction: 'Astromech', - __typename: 'Droid', - }, - ], - }; - - expect(graphqlSync({ schema, source, rootValue })).to.deep.equal({ - data: { - characters: [ - { - name: 'Han Solo', - totalCredits: 10, - }, - { - name: 'R2-D2', - primaryFunction: 'Astromech', - }, - ], - }, - }); - }); - it('Custom Scalar', () => { const sdl = dedent` scalar CustomScalar