@theguild/federation-composition
Version:
Open Source Composition library for Apollo Federation
56 lines (55 loc) • 2.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ContextDirectiveRules = ContextDirectiveRules;
const graphql_1 = require("graphql");
const context_js_1 = require("../../../context.js");
const helpers_js_1 = require("../../../helpers.js");
function ContextDirectiveRules(context) {
return {
DirectiveDefinition(node) {
(0, helpers_js_1.validateDirectiveAgainstOriginal)(node, "context", context);
},
Directive(node) {
if (!context.isAvailableFederationDirective("context", node)) {
return;
}
context.stateBuilder.markSpecAsUsed("context");
const typeDef = context.typeNodeInfo.getTypeDef();
if (!typeDef) {
return;
}
if (typeDef.kind !== graphql_1.Kind.OBJECT_TYPE_DEFINITION &&
typeDef.kind !== graphql_1.Kind.OBJECT_TYPE_EXTENSION &&
typeDef.kind !== graphql_1.Kind.INTERFACE_TYPE_DEFINITION &&
typeDef.kind !== graphql_1.Kind.INTERFACE_TYPE_EXTENSION &&
typeDef.kind !== graphql_1.Kind.UNION_TYPE_DEFINITION &&
typeDef.kind !== graphql_1.Kind.UNION_TYPE_EXTENSION) {
return;
}
const name = node.arguments?.find((arg) => arg.name.value === "name")?.value;
if (!name || name.kind !== graphql_1.Kind.STRING) {
return;
}
if (!(0, context_js_1.isValidContextName)(name.value)) {
context.reportError(new graphql_1.GraphQLError(`Context name "${name.value}" is invalid. It should have only alphanumeric characters.`, {
nodes: node,
extensions: { code: "CONTEXT_NAME_INVALID" },
}));
}
switch (typeDef.kind) {
case graphql_1.Kind.OBJECT_TYPE_DEFINITION:
case graphql_1.Kind.OBJECT_TYPE_EXTENSION:
context.stateBuilder.objectType.addContext(typeDef.name.value, name.value);
break;
case graphql_1.Kind.INTERFACE_TYPE_DEFINITION:
case graphql_1.Kind.INTERFACE_TYPE_EXTENSION:
context.stateBuilder.interfaceType.addContext(typeDef.name.value, name.value);
break;
case graphql_1.Kind.UNION_TYPE_DEFINITION:
case graphql_1.Kind.UNION_TYPE_EXTENSION:
context.stateBuilder.unionType.addContext(typeDef.name.value, name.value);
break;
}
},
};
}