UNPKG

@graphql-mesh/fusion-execution

Version:
81 lines (80 loc) 3.42 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractSubgraphFromFusiongraph = void 0; const graphql_1 = require("graphql"); const utils_1 = require("@graphql-tools/utils"); function extractSubgraphFromFusiongraph(subgraph, fusiongraph) { const fusiongraphAst = (0, utils_1.getDocumentNodeFromSchema)(fusiongraph); const filterNodeBySubgraph = createFilterNodeBySubgraph(subgraph); const filteredAst = (0, graphql_1.visit)(fusiongraphAst, { Directive(node) { const subgraphArgument = node.arguments?.find(argument => argument.name.value === 'subgraph'); if (subgraphArgument != null && subgraphArgument.value.kind === graphql_1.Kind.STRING && subgraphArgument.value.value !== subgraph) { return null; } return node; }, ObjectTypeDefinition: { enter: filterNodeBySubgraph, }, FieldDefinition: { enter: filterNodeBySubgraph, }, EnumTypeDefinition: { enter: filterNodeBySubgraph, }, EnumValueDefinition: { enter: filterNodeBySubgraph, }, InputObjectTypeDefinition: { enter: filterNodeBySubgraph, }, ScalarTypeDefinition: { enter: filterNodeBySubgraph, }, }); return (0, graphql_1.buildASTSchema)(filteredAst, { assumeValid: true, assumeValidSDL: true, }); } exports.extractSubgraphFromFusiongraph = extractSubgraphFromFusiongraph; function createFilterNodeBySubgraph(subgraph) { return function filterNodeBySubgraph(node) { const sourceDirectives = node.directives?.filter(directive => directive.name.value === 'source'); const resolverDirectives = node.directives?.filter(directive => directive.name.value === 'resolver'); for (const sourceDirective of sourceDirectives ?? []) { const subgraphArgument = sourceDirective.arguments?.find(argument => argument.name.value === 'subgraph'); if (subgraphArgument != null && subgraphArgument.value.kind === graphql_1.Kind.STRING && subgraphArgument.value.value === subgraph) { let finalNode = node; const nameArgument = sourceDirective.arguments?.find(argument => argument.name.value === 'name'); if (nameArgument?.value.kind === graphql_1.Kind.STRING) { finalNode = { ...node, name: { kind: graphql_1.Kind.NAME, value: nameArgument.value.value, }, }; } const typeArgument = sourceDirective.arguments?.find(argument => argument.name.value === 'type'); if (typeArgument?.value.kind === graphql_1.Kind.STRING) { const typeStr = typeArgument.value.value; finalNode = { ...finalNode, type: (0, graphql_1.parseType)(typeStr, { noLocation: true }), }; } return finalNode; } } if (sourceDirectives.length > 0 || resolverDirectives.length > 0) { return null; } return node; }; }