Skip to content
This repository has been archived by the owner on Apr 15, 2020. It is now read-only.

Commit

Permalink
fix(stitching): Directive disappears when enum has resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored and yaacovCR committed Sep 22, 2019
1 parent 0b914d3 commit 85b16cc
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 3 deletions.
1 change: 0 additions & 1 deletion src/stitching/schemaRecreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ export function recreateType(
name: type.name,
description: type.description,
astNode: type.astNode,

fields: () =>
inputFieldMapToFieldConfigMap(type.getFields(), resolveType),
});
Expand Down
36 changes: 36 additions & 0 deletions src/test/testDirectives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,13 @@ describe('@directives', () => {
it('are included in the schema AST', () => {
const schema = makeExecutableSchema({
typeDefs,
resolvers: {
Gender: {
NONBINARY: 'NB',
FEMALE: 'F',
MALE: 'M'
}
}
});

function checkDirectives(
Expand Down Expand Up @@ -167,6 +174,35 @@ describe('@directives', () => {
checkDirectives(schema.getType('WhateverUnion'), ['unionDirective']);
});

it('works with enum and its resolvers', () => {
const schema = makeExecutableSchema({
typeDefs: `
enum DateFormat {
LOCAL
ISO
}
directive @date(format: DateFormat) on FIELD_DEFINITION
scalar Date
type Query {
today: Date @date(format: LOCAL)
}
`,
resolvers: {
DateFormat: {
LOCAL: 'local',
ISO: 'iso'
}
}
});

assert.exists(schema.getType('DateFormat'));
assert.lengthOf(schema.getDirectives(), 4);
assert.exists(schema.getDirective('date'));
});

it('can be implemented with SchemaDirectiveVisitor', () => {
const visited: Set<GraphQLObjectType> = new Set;
const schema = makeExecutableSchema({ typeDefs });
Expand Down
7 changes: 5 additions & 2 deletions src/transforms/visitSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
isNamedType,
getNamedType,
} from 'graphql';
import { recreateType, createResolveType } from '../stitching/schemaRecreation';
import { recreateType, recreateDirective, createResolveType } from '../stitching/schemaRecreation';

export enum VisitSchemaKind {
TYPE = 'VisitSchemaKind.TYPE',
Expand Down Expand Up @@ -40,7 +40,7 @@ export function visitSchema(
visitor: SchemaVisitor,
stripResolvers?: boolean,
) {
const types = {};
const types: {[key: string]: GraphQLNamedType} = {};
const resolveType = createResolveType(name => {
if (typeof types[name] === 'undefined') {
throw new Error(`Can't find type ${name}.`);
Expand Down Expand Up @@ -73,6 +73,7 @@ export function visitSchema(
}
}
});

return new GraphQLSchema({
query: queryType ? (types[queryType.name] as GraphQLObjectType) : null,
mutation: mutationType
Expand All @@ -82,6 +83,8 @@ export function visitSchema(
? (types[subscriptionType.name] as GraphQLObjectType)
: null,
types: Object.keys(types).map(name => types[name]),
directives: [...schema.getDirectives().map(d => recreateDirective(d, resolveType))],
astNode: schema.astNode,
});
}

Expand Down

0 comments on commit 85b16cc

Please sign in to comment.