UNPKG

@graphql-mesh/supergraph

Version:
83 lines (82 loc) 3.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const graphql_1 = require("graphql"); const cross_helpers_1 = require("@graphql-mesh/cross-helpers"); const store_1 = require("@graphql-mesh/store"); const string_interpolation_1 = require("@graphql-mesh/string-interpolation"); const utils_1 = require("@graphql-mesh/utils"); const executor_http_1 = require("@graphql-tools/executor-http"); const federation_1 = require("@graphql-tools/federation"); class SupergraphHandler { constructor({ config, baseDir, store, importFn, logger, }) { this.config = config; this.baseDir = baseDir; this.supergraphSdl = store.proxy('nonExecutableSchema', store_1.PredefinedProxyOptions.JsonWithoutValidation); this.importFn = importFn; this.logger = logger; } async getSupergraphSdl() { const schemaHeadersFactory = (0, string_interpolation_1.getInterpolatedHeadersFactory)(this.config.schemaHeaders); if ((0, utils_1.isUrl)(this.config.source)) { const interpolatedSource = string_interpolation_1.stringInterpolator.parse(this.config.source, { env: cross_helpers_1.process.env, }); const res = await (0, utils_1.readUrl)(interpolatedSource, { headers: schemaHeadersFactory({ env: cross_helpers_1.process.env, }), cwd: this.baseDir, allowUnknownExtensions: true, importFn: this.importFn, fetch: this.fetchFn, logger: this.logger, }); if (typeof res === 'string') { return (0, graphql_1.parse)(res, { noLocation: true }); } return res; } return this.supergraphSdl.getWithSet(async () => { const interpolatedSource = string_interpolation_1.stringInterpolator.parse(this.config.source, { env: cross_helpers_1.process.env, }); const sdlOrIntrospection = await (0, utils_1.readFile)(interpolatedSource, { cwd: this.baseDir, allowUnknownExtensions: true, importFn: this.importFn, fetch: this.fetchFn, logger: this.logger, }); if (typeof sdlOrIntrospection === 'string') { return (0, graphql_1.parse)(sdlOrIntrospection, { noLocation: true }); } return sdlOrIntrospection; }); } async getMeshSource({ fetchFn }) { this.fetchFn = fetchFn; const supergraphSdl = await this.getSupergraphSdl(); const operationHeadersFactory = this.config.operationHeaders != null ? (0, string_interpolation_1.getInterpolatedHeadersFactory)(this.config.operationHeaders) : undefined; const schema = (0, federation_1.getStitchedSchemaFromSupergraphSdl)({ supergraphSdl, onExecutor: ({ endpoint }) => { return (0, executor_http_1.buildHTTPExecutor)({ endpoint, fetch: fetchFn, headers: operationHeadersFactory && (execReq => operationHeadersFactory({ env: cross_helpers_1.process.env, context: execReq.context, })), }); }, batch: this.config.batch == null ? true : this.config.batch, }); return { schema, }; } } exports.default = SupergraphHandler;