UNPKG

@theguild/federation-composition

Version:
113 lines (112 loc) 4.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.federationDirectives = exports.federationEnums = exports.federationScalars = void 0; exports.transformSupergraphToPublicSchema = transformSupergraphToPublicSchema; const graphql_1 = require("graphql"); exports.federationScalars = new Set([ '_FieldSet', 'link__Import', 'join__FieldSet', 'join__DirectiveArguments', 'policy__Policy', 'requiresScopes__Scope', ]); exports.federationEnums = new Set(['core__Purpose', 'join__Graph', 'link__Purpose']); exports.federationDirectives = new Set([ 'link', 'core', 'tag', 'join__graph', 'join__type', 'join__owner', 'join__implements', 'join__unionMember', 'join__directive', 'join__enumValue', 'join__field', 'inaccessible', 'authenticated', 'policy', 'requiresScopes', ]); const specifiedDirectives = new Set(graphql_1.specifiedDirectives.map(d => d.name)); function getAdditionalDirectivesToStrip(documentNode) { const schemaDefinitionNode = documentNode.definitions.find((node) => node.kind === graphql_1.Kind.SCHEMA_DEFINITION); if (!schemaDefinitionNode?.directives?.length) { return null; } const additionalDirectivesToStrip = new Set(); for (const directive of schemaDefinitionNode.directives) { if (directive.name.value !== 'link') { continue; } const asArg = directive.arguments?.find(arg => arg.name.value === 'as'); if (asArg?.value.kind === graphql_1.Kind.STRING) { additionalDirectivesToStrip.add(asArg.value.value); } } return additionalDirectivesToStrip; } const federationInaccessibleDirectiveUrlPrefix = 'https://specs.apollo.dev/inaccessible'; function getInaccessibleDirectiveName(documentNode) { const schemaDefinitionNode = documentNode.definitions.find((node) => node.kind === graphql_1.Kind.SCHEMA_DEFINITION); if (schemaDefinitionNode?.directives?.length) { for (const directive of schemaDefinitionNode.directives) { if (directive.name.value !== 'link') { continue; } const urlArg = directive.arguments?.find(arg => arg.name.value === 'url'); const asArg = directive.arguments?.find(arg => arg.name.value === 'as'); if (urlArg?.value.kind === graphql_1.Kind.STRING && urlArg.value.value.startsWith(federationInaccessibleDirectiveUrlPrefix)) { if (asArg?.value.kind === graphql_1.Kind.STRING) { return asArg.value.value; } break; } } } return 'inaccessible'; } function transformSupergraphToPublicSchema(documentNode) { const additionalFederationDirectives = getAdditionalDirectivesToStrip(documentNode); const inaccessibleDirectiveName = getInaccessibleDirectiveName(documentNode); function removeFederationOrSpecifiedDirectives(node) { if (exports.federationDirectives.has(node.name.value) || additionalFederationDirectives?.has(node.name.value) || (node.kind === graphql_1.Kind.DIRECTIVE_DEFINITION && specifiedDirectives.has(node.name.value))) { return null; } } function hasInaccessibleDirective(node) { return node.directives?.some(d => d.name.value === inaccessibleDirectiveName); } function removeInaccessibleNode(node) { if (hasInaccessibleDirective(node)) { return null; } } return (0, graphql_1.visit)(documentNode, { [graphql_1.Kind.DIRECTIVE_DEFINITION]: removeFederationOrSpecifiedDirectives, [graphql_1.Kind.DIRECTIVE]: removeFederationOrSpecifiedDirectives, [graphql_1.Kind.SCHEMA_EXTENSION]: () => null, [graphql_1.Kind.SCHEMA_DEFINITION]: () => null, [graphql_1.Kind.SCALAR_TYPE_DEFINITION](node) { if (exports.federationScalars.has(node.name.value) || hasInaccessibleDirective(node)) { return null; } }, [graphql_1.Kind.ENUM_TYPE_DEFINITION](node) { if (exports.federationEnums.has(node.name.value) || hasInaccessibleDirective(node)) { return null; } }, [graphql_1.Kind.ENUM_VALUE_DEFINITION]: removeInaccessibleNode, [graphql_1.Kind.OBJECT_TYPE_DEFINITION]: removeInaccessibleNode, [graphql_1.Kind.FIELD_DEFINITION]: removeInaccessibleNode, [graphql_1.Kind.INTERFACE_TYPE_DEFINITION]: removeInaccessibleNode, [graphql_1.Kind.UNION_TYPE_DEFINITION]: removeInaccessibleNode, [graphql_1.Kind.INPUT_OBJECT_TYPE_DEFINITION]: removeInaccessibleNode, [graphql_1.Kind.INPUT_VALUE_DEFINITION]: removeInaccessibleNode, }); }