UNPKG

@graphql-mesh/compose-cli

Version:
57 lines (56 loc) 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getComposedSchemaFromConfig = void 0; const fusion_composition_1 = require("@graphql-mesh/fusion-composition"); const utils_1 = require("@graphql-mesh/utils"); const graphql_file_loader_1 = require("@graphql-tools/graphql-file-loader"); const load_1 = require("@graphql-tools/load"); const fetch_1 = require("@whatwg-node/fetch"); async function getComposedSchemaFromConfig(meshComposeCLIConfig, spinnies) { const ctx = { fetch: meshComposeCLIConfig.fetch || fetch_1.fetch, cwd: meshComposeCLIConfig.cwd || globalThis.process?.cwd?.(), logger: new utils_1.DefaultLogger(), }; const subgraphConfigsForComposition = await Promise.all(meshComposeCLIConfig.subgraphs.map(async (subgraphCLIConfig) => { const { name: subgraphName, schema$ } = subgraphCLIConfig.sourceHandler(ctx); spinnies?.add(subgraphName, { text: `Loading subgraph ${subgraphName}` }); let subgraphSchema; try { subgraphSchema = await schema$; } catch (e) { throw new Error(`Failed to load subgraph ${subgraphName} - ${e.stack}`); } spinnies?.succeed(subgraphName, { text: `Loaded subgraph ${subgraphName}` }); return { name: subgraphName, schema: subgraphSchema, transforms: subgraphCLIConfig.transforms, }; })); spinnies?.add('composition', { text: `Composing fusiongraph` }); let additionalTypeDefs; if (meshComposeCLIConfig.additionalTypeDefs != null) { const result = await (0, load_1.loadTypedefs)(meshComposeCLIConfig.additionalTypeDefs, { noLocation: true, assumeValid: true, assumeValidSDL: true, loaders: [new graphql_file_loader_1.GraphQLFileLoader()], }); additionalTypeDefs = result.map(r => r.document || r.rawSDL); } let composedSchema = (0, fusion_composition_1.composeSubgraphs)(subgraphConfigsForComposition, { typeDefs: additionalTypeDefs, }); if (meshComposeCLIConfig.transforms?.length) { spinnies?.add('transforms', { text: `Applying transforms` }); for (const transform of meshComposeCLIConfig.transforms) { composedSchema = transform(composedSchema); } spinnies?.succeed('transforms', { text: `Applied transforms` }); } spinnies?.succeed('composition', { text: `Composed fusiongraph` }); return composedSchema; } exports.getComposedSchemaFromConfig = getComposedSchemaFromConfig;