Skip to content
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

buildASTSchema/extendSchema: unify directive building #2236

Merged
merged 1 commit into from
Oct 21, 2019
Merged
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
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