UNPKG

@apollo/composition

Version:
70 lines 3.54 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.validateSatisfiability = exports.composeServices = exports.compose = void 0; const federation_internals_1 = require("@apollo/federation-internals"); const query_graphs_1 = require("@apollo/query-graphs"); const merging_1 = require("./merging"); const validate_1 = require("./validate"); function validateCompositionOptions(options) { var _a; (0, federation_internals_1.assert)(!((_a = options === null || options === void 0 ? void 0 : options.allowedFieldTypeMergingSubtypingRules) === null || _a === void 0 ? void 0 : _a.includes("list_upgrade")), "The `list_upgrade` field subtyping rule is currently not supported"); } function compose(subgraphs, options = {}) { var _a; const { runSatisfiability = true, sdlPrintOptions, maxValidationSubgraphPaths } = options; validateCompositionOptions(options); const mergeResult = validateSubgraphsAndMerge(subgraphs); if (mergeResult.errors) { return { errors: mergeResult.errors }; } let satisfiabilityResult; if (runSatisfiability) { satisfiabilityResult = validateSatisfiability({ supergraphSchema: mergeResult.supergraph, }, { maxValidationSubgraphPaths }); if (satisfiabilityResult.errors) { return { errors: satisfiabilityResult.errors }; } } let supergraphSdl; try { supergraphSdl = (0, federation_internals_1.printSchema)(mergeResult.supergraph, sdlPrintOptions !== null && sdlPrintOptions !== void 0 ? sdlPrintOptions : (0, federation_internals_1.shallowOrderPrintedDefinitions)(federation_internals_1.defaultPrintOptions)); } catch (err) { return { errors: [err] }; } return { schema: mergeResult.supergraph, supergraphSdl, hints: [...mergeResult.hints, ...((_a = satisfiabilityResult === null || satisfiabilityResult === void 0 ? void 0 : satisfiabilityResult.hints) !== null && _a !== void 0 ? _a : [])], }; } exports.compose = compose; function composeServices(services, options = {}) { const subgraphs = (0, federation_internals_1.subgraphsFromServiceList)(services); if (Array.isArray(subgraphs)) { return { errors: subgraphs }; } return compose(subgraphs, options); } exports.composeServices = composeServices; function validateSatisfiability({ supergraphSchema, supergraphSdl }, options = {}) { const supergraph = supergraphSchema ? new federation_internals_1.Supergraph(supergraphSchema, null) : federation_internals_1.Supergraph.build(supergraphSdl, { supportedFeatures: null }); const supergraphQueryGraph = (0, query_graphs_1.buildSupergraphAPIQueryGraph)(supergraph); const federatedQueryGraph = (0, query_graphs_1.buildFederatedQueryGraph)(supergraph, false); return (0, validate_1.validateGraphComposition)(supergraph.schema, supergraph.subgraphNameToGraphEnumValue(), supergraphQueryGraph, federatedQueryGraph, options); } exports.validateSatisfiability = validateSatisfiability; function validateSubgraphsAndMerge(subgraphs) { const upgradeResult = (0, federation_internals_1.upgradeSubgraphsIfNecessary)(subgraphs); if (upgradeResult.errors) { return { errors: upgradeResult.errors }; } const toMerge = upgradeResult.subgraphs; const validationErrors = toMerge.validate(); if (validationErrors) { return { errors: validationErrors }; } return (0, merging_1.mergeSubgraphs)(toMerge); } //# sourceMappingURL=compose.js.map