Skip to content

Support for union types when using buildSchema #947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
131 changes: 131 additions & 0 deletions src/utilities/__tests__/buildASTSchema-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,137 @@ describe('Schema Builder', () => {
expect(output).to.equal(body);
});

it('Specifying Union type using __typename', async () => {
const schema = buildSchema(`
schema {
query: Root
}

type Root {
fruits: [Fruit]
}

union Fruit = Apple | Banana

type Apple {
color: String
}

type Banana {
length: Int
}
`);

const query = `
{
fruits {
... on Apple {
color
}
... on Banana {
length
}
}
}
`;

const root = {
fruits: [
{
color: 'green',
__typename: 'Apple',
},
{
length: 5,
__typename: 'Banana',
}
]
};

expect(await graphql(schema, query, root)).to.deep.equal({
data: {
fruits: [
{
color: 'green',
},
{
length: 5,
}
]
}
});
});

it('Specifying Interface type using __typename', async () => {
const schema = buildSchema(`
schema {
query: Root
}

type Root {
characters: [Character]
}

interface Character {
name: String!
}

type Human implements Character {
name: String!
totalCredits: Int
}

type Droid implements Character {
name: String!
primaryFunction: String
}
`);

const query = `
{
characters {
name
... on Human {
totalCredits
}
... on Droid {
primaryFunction
}
}
}
`;

const root = {
characters: [
{
name: 'Han Solo',
totalCredits: 10,
__typename: 'Human',
},
{
name: 'R2-D2',
primaryFunction: 'Astromech',
__typename: 'Droid',
}
]
};

expect(await graphql(schema, query, root)).to.deep.equal({
data: {
characters: [
{
name: 'Han Solo',
totalCredits: 10,
},
{
name: 'R2-D2',
primaryFunction: 'Astromech',
}
]
}
});
});

it('Custom Scalar', () => {
const body = dedent`
schema {
Expand Down
11 changes: 3 additions & 8 deletions src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import invariant from '../jsutils/invariant';
import keyValMap from '../jsutils/keyValMap';
import { valueFromAST } from './valueFromAST';
import { resolveTypeForGeneratedSchema } from './resolveTypeForGeneratedSchema';
import { TokenKind } from '../language/lexer';
import { parse } from '../language/parser';
import type { Source } from '../language/source';
Expand Down Expand Up @@ -398,7 +399,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
description: getDescription(def),
fields: () => makeFieldDefMap(def),
astNode: def,
resolveType: cannotExecuteSchema,
resolveType: resolveTypeForGeneratedSchema,
});
}

Expand All @@ -424,7 +425,7 @@ export function buildASTSchema(ast: DocumentNode): GraphQLSchema {
name: def.name.value,
description: getDescription(def),
types: def.types.map(t => produceObjectType(t)),
resolveType: cannotExecuteSchema,
resolveType: resolveTypeForGeneratedSchema,
astNode: def,
});
}
Expand Down Expand Up @@ -516,9 +517,3 @@ function leadingSpaces(str) {
}
return i;
}

function cannotExecuteSchema() {
throw new Error(
'Generated Schema cannot use Interface or Union types for execution.'
);
}
11 changes: 3 additions & 8 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
getDeprecationReason,
} from './buildASTSchema';
import { valueFromAST } from './valueFromAST';
import { resolveTypeForGeneratedSchema } from './resolveTypeForGeneratedSchema';
import { GraphQLError } from '../error/GraphQLError';
import { GraphQLSchema } from '../type/schema';

Expand Down Expand Up @@ -479,7 +480,7 @@ export function extendSchema(
description: getDescription(typeNode),
fields: () => buildFieldMap(typeNode),
astNode: typeNode,
resolveType: cannotExecuteExtendedSchema,
resolveType: resolveTypeForGeneratedSchema,
});
}

Expand All @@ -489,7 +490,7 @@ export function extendSchema(
description: getDescription(typeNode),
types: typeNode.types.map(getObjectTypeFromAST),
astNode: typeNode,
resolveType: cannotExecuteExtendedSchema,
resolveType: resolveTypeForGeneratedSchema,
});
}

Expand Down Expand Up @@ -608,9 +609,3 @@ export function extendSchema(
return getOutputTypeFromAST(typeNode);
}
}

function cannotExecuteExtendedSchema() {
throw new Error(
'Extended Schema cannot use Interface or Union types for execution.'
);
}
18 changes: 18 additions & 0 deletions src/utilities/resolveTypeForGeneratedSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// @flow

import type { GraphQLTypeResolver } from '../type/definition';

export const resolveTypeForGeneratedSchema: GraphQLTypeResolver<any, *> =
function (value: any): string {
if (
value &&
typeof value === 'object' &&
typeof value.__typename === 'string'
) {
return value.__typename;
}
throw new Error(
'To resolve Interface or Union types for a generated Schema the result ' +
'must have a __typename property containing the name of the actual type.'
);
};