UNPKG

@graphql-mesh/transform-filter-schema

Version:
34 lines (33 loc) 1.33 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.healSchemaRootAst = healSchemaRootAst; const graphql_1 = require("graphql"); /** * mapSchema can leave schema.astNode.operationTypes pointing at root types that * were removed (e.g. empty Mutation after filterSchema). Heal those AST nodes so * later SDL round-trips (mergeSchemas / printSchemaWithDirectives) stay valid. */ function healSchemaRootAst(schema) { const config = schema.toConfig(); const presentRootNames = new Set([config.query?.name, config.mutation?.name, config.subscription?.name].filter((name) => Boolean(name))); const filterOperationTypes = (node) => { if (!node?.operationTypes) { return node ?? undefined; } const operationTypes = node.operationTypes.filter(op => presentRootNames.has(op.type.name.value)); if (operationTypes.length === node.operationTypes.length) { return node; } return { ...node, operationTypes, }; }; const astNode = filterOperationTypes(config.astNode); const extensionASTNodes = (config.extensionASTNodes ?? []).map(node => filterOperationTypes(node)); return new graphql_1.GraphQLSchema({ ...config, astNode, extensionASTNodes, }); }