Skip to content

Commit

Permalink
Fix recreation of directives
Browse files Browse the repository at this point in the history
  • Loading branch information
freiksenet committed Jan 2, 2019
1 parent f03f7ab commit c0e4f1a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/stitching/mergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from '../makeExecutableSchema';
import {
recreateType,
recreateDirective,
fieldMapToFieldConfigMap,
createResolveType,
} from './schemaRecreation';
Expand Down Expand Up @@ -140,10 +141,12 @@ function mergeSchemasImplementation({
});
}

const directiveInstances = schema.getDirectives();
directiveInstances.forEach(directive => {
directives.push(directive);
});
if (mergeDirectives) {
const directiveInstances = schema.getDirectives();
directiveInstances.forEach(directive => {
directives.push(directive);
});
}

const typeMap = schema.getTypeMap();
Object.keys(typeMap).forEach(typeName => {
Expand All @@ -166,12 +169,12 @@ function mergeSchemasImplementation({
(schema && (schema as DocumentNode).kind === Kind.DOCUMENT)
) {
let parsedSchemaDocument =
typeof schema === 'string' ? parse(schema) : (schema as DocumentNode);
typeof schema === 'string' ? parse(schema) : (schema as DocumentNode);
parsedSchemaDocument.definitions.forEach(def => {
const type = typeFromAST(def);
if (type instanceof GraphQLDirective) {
if (type instanceof GraphQLDirective && mergeDirectives) {
directives.push(type);
} else if (type) {
} else if (type && !(type instanceof GraphQLDirective)) {
addTypeCandidate(typeCandidates, type.name, {
type: type,
});
Expand Down Expand Up @@ -248,7 +251,7 @@ function mergeSchemasImplementation({
mutation: types.Mutation as GraphQLObjectType,
subscription: types.Subscription as GraphQLObjectType,
types: Object.keys(types).map(key => types[key]),
directives: mergeDirectives ? directives : [],
directives: directives.map((directive) => recreateDirective(directive, resolveType))
});

extensions.forEach(extension => {
Expand Down
14 changes: 14 additions & 0 deletions src/stitching/schemaRecreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
GraphQLScalarType,
GraphQLType,
GraphQLUnionType,
GraphQLDirective,
Kind,
ValueNode,
getNamedType,
Expand Down Expand Up @@ -124,6 +125,19 @@ export function recreateType(
}
}

export function recreateDirective(
directive: GraphQLDirective,
resolveType: ResolveType<any>,
): GraphQLDirective {
return new GraphQLDirective({
name: directive.name,
description: directive.description,
locations: directive.locations,
args: argsToFieldConfigArgumentMap(directive.args, resolveType),
astNode: directive.astNode,
});
}

function parseLiteral(ast: ValueNode): any {
switch (ast.kind) {
case Kind.STRING:
Expand Down
9 changes: 9 additions & 0 deletions src/test/testMergeSchemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,8 +243,17 @@ const codeCoverageTypeDefs = `
let schemaDirectiveTypeDefs = `
directive @upper on FIELD_DEFINITION
directive @withEnumArg(enumArg: DirectiveEnum = FOO) on FIELD_DEFINITION
enum DirectiveEnum {
FOO
BAR
}
extend type Property {
someField: String! @upper
someOtherField: String! @withEnumArg
someThirdField: String! @withEnumArg(enumArg: BAR)
}
`;

Expand Down

0 comments on commit c0e4f1a

Please sign in to comment.