@graphql-mesh/utils
Version:
55 lines (54 loc) • 2.33 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getDefDirectives = getDefDirectives;
const graphql_1 = require("graphql");
const utils_1 = require("@graphql-tools/utils");
function getDefDirectives(schema, { astNode, extensions }, subgraph) {
const directiveAnnotations = [];
if (astNode != null && 'directives' in astNode) {
astNode.directives?.forEach(directiveNode => {
const directiveName = directiveNode.name.value;
const schemaDirective = schema.getDirective(directiveName);
const directiveAnnotation = {
name: directiveNode.name.value,
args: schemaDirective
? (0, utils_1.getArgumentValues)(schemaDirective, directiveNode)
: (directiveNode.arguments?.reduce((acc, arg) => {
acc[arg.name.value] = (0, graphql_1.valueFromASTUntyped)(arg.value);
return acc;
}, {}) ?? {}),
};
if (subgraph &&
directiveAnnotation.args.subgraph &&
directiveAnnotation.args.subgraph !== subgraph)
return;
directiveAnnotations.push(directiveAnnotation);
});
}
if (extensions?.directives != null) {
for (const directiveName in extensions.directives) {
const directiveExt = extensions.directives[directiveName];
if (directiveExt != null) {
if (Array.isArray(directiveExt)) {
directiveExt.forEach(directive => {
if (subgraph && directive.subgraph && directive.subgraph !== subgraph)
return;
directiveAnnotations.push({
name: directiveName,
args: directive,
});
});
}
else {
if (subgraph && directiveExt.subgraph && directiveExt.subgraph !== subgraph)
continue;
directiveAnnotations.push({
name: directiveName,
args: directiveExt,
});
}
}
}
}
return directiveAnnotations;
}
;