UNPKG

@theguild/federation-composition

Version:
96 lines (95 loc) • 4.77 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.composeSchemaContract = composeSchemaContract; exports.composeServicesWithContracts = composeServicesWithContracts; const tag_extraction_js_1 = require("./tag-extraction.js"); const printer_js_1 = require("../graphql/printer.js"); const compose_js_1 = require("../compose.js"); const validate_js_1 = require("../validate.js"); const validate_supergraph_js_1 = require("../supergraph/validation/validate-supergraph.js"); const supergraph_state_contract_js_1 = require("./supergraph-state-contract.js"); const index_js_1 = require("../utils/link/index.js"); const add_inaccessible_to_unreachable_types_js_1 = require("./add-inaccessible-to-unreachable-types.js"); function composeSchemaContract(services, tagFilter, removeUnreachableTypes = true, options) { const filteredSubgraphs = (0, tag_extraction_js_1.applyTagFilterOnSubgraphs)(services, tagFilter); const compositionResult = (0, compose_js_1.composeServices)(filteredSubgraphs, options?.skipSatisfiability ? { disableValidationRules: ["SatisfiabilityRule"] } : undefined); if (!compositionResult.errors && compositionResult.supergraphDocumentNode && removeUnreachableTypes) { const { resolveImportName } = (0, index_js_1.extractLinkImplementations)(compositionResult.supergraphDocumentNode); return (0, add_inaccessible_to_unreachable_types_js_1.addInaccessibleToUnreachableTypes)(resolveImportName, compositionResult); } return compositionResult; } function composeServicesWithContracts(services, contracts) { const result = (0, validate_js_1.validateAndKeepState)(services); if (!result.success) { return { supergraph: { errors: result.errors }, contracts: contracts.map((contract) => composeSchemaContract(services, contract.filter, contract.removeUnreachableTypes ?? true)), }; } const supergraph = (0, compose_js_1.createCompositionSuccessReadCacheContainer)((0, compose_js_1.createSupergraphDocumentNode)(result)); const supergraphState = result.supergraphStateBuilder.getSupergraphState(); const applyTagFilter = (0, supergraph_state_contract_js_1.createSupergraphStateContractFilter)(services, supergraphState); const shareDefinitions = createDefinitionSharing(supergraph.supergraphDocumentNode); return { supergraph, contracts: contracts.map((contract) => { const filtered = applyTagFilter(contract.filter); try { if (filtered.errors.length > 0) { return { errors: filtered.errors }; } const errors = (0, validate_supergraph_js_1.validateInaccessibleUsage)(result.subgraphStates, supergraphState); if (errors.length > 0) { return { errors }; } const compositionResult = (0, compose_js_1.createCompositionSuccessReadCacheContainer)(shareDefinitions((0, compose_js_1.createSupergraphDocumentNode)({ ...result, supergraph: result.supergraphStateBuilder.build(), specs: supergraphState.specs, }))); if (contract.removeUnreachableTypes === false) { return compositionResult; } const { resolveImportName } = (0, index_js_1.extractLinkImplementations)(compositionResult.supergraphDocumentNode); return (0, add_inaccessible_to_unreachable_types_js_1.addInaccessibleToUnreachableTypes)(resolveImportName, compositionResult); } finally { filtered.restore(); } }), }; } function createDefinitionSharing(supergraphDocument) { let printedDefinitions = null; return function shareDefinitions(document) { if (!printedDefinitions) { printedDefinitions = new Map(); for (const node of supergraphDocument.definitions) { printedDefinitions.set(definitionKey(node), { node, printed: (0, printer_js_1.print)(node), }); } } const definitions = printedDefinitions; return { ...document, definitions: document.definitions.map((node) => { const existing = definitions.get(definitionKey(node)); return existing && existing.printed === (0, printer_js_1.print)(node) ? existing.node : node; }), }; }; } function definitionKey(node) { return "name" in node && node.name ? `${node.kind}:${node.name.value}` : node.kind; }