@graphql-tools/stitch
Version:
A set of utils for faster development of GraphQL tools
42 lines (41 loc) • 1.28 kB
JavaScript
import { GraphQLDirective } from 'graphql';
import { mergeDeep } from '@graphql-tools/utils';
export function mergeDirectives(directives) {
if (directives.size === 0) {
return undefined;
}
if (directives.size === 1) {
const directive = directives.values().next().value;
return directive;
}
let name;
let description;
const locations = new Set();
const args = {};
const extensionsSet = new Set();
let isRepeatable = false;
for (const directive of directives) {
name = directive.name;
if (directive.description) {
description = directive.description;
}
for (const location of directive.locations) {
locations.add(location);
}
for (const arg of directive.args) {
args[arg.name] = arg;
}
isRepeatable = isRepeatable || directive.isRepeatable;
if (directive.extensions) {
extensionsSet.add(directive.extensions);
}
}
return new GraphQLDirective({
name: name,
description: description,
locations: Array.from(locations),
args,
isRepeatable,
extensions: extensionsSet.size > 0 ? mergeDeep([...extensionsSet]) : undefined,
});
}