Skip to content

Commit

Permalink
buildASTSchema/extendSchema: unify directive building (#2236)
Browse files Browse the repository at this point in the history
  • Loading branch information
IvanGoncharov authored Oct 21, 2019
1 parent 1fe12f4 commit aa4511d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 21 deletions.
30 changes: 17 additions & 13 deletions src/utilities/buildASTSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ export function buildASTSchema(
subscription: (typeMap['Subscription']: any),
};

const directives = directiveDefs.map(def => astBuilder.buildDirective(def));
const directives = astBuilder.buildDirectives(directiveDefs);

// If specified directives were not explicitly declared, add them.
if (!directives.some(directive => directive.name === 'skip')) {
Expand Down Expand Up @@ -238,18 +238,22 @@ export class ASTDefinitionBuilder {
return this.getNamedType(node);
}

buildDirective(directive: DirectiveDefinitionNode): GraphQLDirective {
const locations = directive.locations.map(
({ value }) => ((value: any): DirectiveLocationEnum),
);

return new GraphQLDirective({
name: directive.name.value,
description: getDescription(directive, this._options),
locations,
isRepeatable: directive.repeatable,
args: this.buildArgumentMap(directive.arguments),
astNode: directive,
buildDirectives(
nodes: Array<DirectiveDefinitionNode>,
): Array<GraphQLDirective> {
return nodes.map(directive => {
const locations = directive.locations.map(
({ value }) => ((value: any): DirectiveLocationEnum),
);

return new GraphQLDirective({
name: directive.name.value,
description: getDescription(directive, this._options),
locations,
isRepeatable: directive.repeatable,
args: this.buildArgumentMap(directive.arguments),
astNode: directive,
});
});
}

Expand Down
18 changes: 10 additions & 8 deletions src/utilities/extendSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,10 @@ export function extendSchema(
return new GraphQLSchema({
...operationTypes,
types: objectValues(typeMap),
directives: getMergedDirectives(),
directives: [
...replaceDirectives(schemaConfig.directives),
...astBuilder.buildDirectives(directiveDefs),
],
astNode: schemaDef || schemaConfig.astNode,
extensionASTNodes: concatMaybeArrays(
schemaConfig.extensionASTNodes,
Expand All @@ -205,19 +208,18 @@ export function extendSchema(
return ((typeMap[type.name]: any): T);
}

function getMergedDirectives(): Array<GraphQLDirective> {
const existingDirectives = schema.getDirectives().map(directive => {
function replaceDirectives(
directives: $ReadOnlyArray<GraphQLDirective>,
): Array<GraphQLDirective> {
devAssert(directives, 'schema must have default directives');

return directives.map(directive => {
const config = directive.toConfig();
return new GraphQLDirective({
...config,
args: mapValue(config.args, extendArg),
});
});

devAssert(existingDirectives, 'schema must have default directives');
return existingDirectives.concat(
directiveDefs.map(node => astBuilder.buildDirective(node)),
);
}

function extendNamedType(type: GraphQLNamedType): GraphQLNamedType {
Expand Down

0 comments on commit aa4511d

Please sign in to comment.