UNPKG

@graphql-mesh/serve-runtime

Version:
114 lines (113 loc) 4.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createServeRuntime = void 0; const graphql_yoga_1 = require("graphql-yoga"); const fusion_federation_1 = require("@graphql-mesh/fusion-federation"); const fusion_runtime_1 = require("@graphql-mesh/fusion-runtime"); // eslint-disable-next-line import/no-extraneous-dependencies const utils_1 = require("@graphql-mesh/utils"); const executor_http_1 = require("@graphql-tools/executor-http"); const executor_yoga_1 = require("@graphql-tools/executor-yoga"); const utils_2 = require("@graphql-tools/utils"); const handleUnifiedGraphConfig_js_1 = require("./handleUnifiedGraphConfig.js"); function createServeRuntime(config) { let fetchAPI = config.fetchAPI; // eslint-disable-next-line prefer-const let logger; let wrappedFetchFn; const serveContext = { get fetch() { return wrappedFetchFn; }, get logger() { return logger; }, cwd: globalThis.process?.cwd(), cache: 'cache' in config ? config.cache : undefined, pubsub: 'pubsub' in config ? config.pubsub : undefined, }; let supergraphYogaPlugin; if ('http' in config) { const executor = (0, executor_http_1.buildHTTPExecutor)({ fetch: fetchAPI?.fetch, ...config.http, }); supergraphYogaPlugin = (0, executor_yoga_1.useExecutor)(executor); } else if ('fusiongraph' in config) { supergraphYogaPlugin = (0, fusion_runtime_1.useFusiongraph)({ getFusiongraph() { return (0, handleUnifiedGraphConfig_js_1.handleUnifiedGraphConfig)(config.fusiongraph || './fusiongraph.graphql', serveContext); }, transports: config.transports, polling: config.polling, additionalResolvers: config.additionalResolvers, transportBaseContext: serveContext, }); } else if ('supergraph' in config) { supergraphYogaPlugin = (0, fusion_runtime_1.useFusiongraph)({ getFusiongraph() { const supergraph$ = (0, handleUnifiedGraphConfig_js_1.handleUnifiedGraphConfig)(config.supergraph || './supergraph.graphql', serveContext); serveContext.logger?.info?.(`Converting Federation Supergraph to Fusiongraph`); if ((0, utils_2.isPromise)(supergraph$)) { return supergraph$.then(supergraph => (0, fusion_federation_1.convertSupergraphToFusiongraph)(supergraph)); } return (0, fusion_federation_1.convertSupergraphToFusiongraph)(supergraph$); }, transports: config.transports, polling: config.polling, additionalResolvers: config.additionalResolvers, transportBaseContext: serveContext, }); } const defaultFetchPlugin = { onFetch({ setFetchFn }) { setFetchFn(fetchAPI.fetch); }, onYogaInit({ yoga }) { const onFetchHooks = []; for (const plugin of yoga.getEnveloped._plugins) { if (plugin.onFetch) { onFetchHooks.push(plugin.onFetch); } } wrappedFetchFn = (0, utils_1.wrapFetchWithHooks)(onFetchHooks); }, }; const yoga = (0, graphql_yoga_1.createYoga)({ fetchAPI: config.fetchAPI, logging: config.logging == null ? new utils_1.DefaultLogger() : config.logging, plugins: [defaultFetchPlugin, supergraphYogaPlugin, ...(config.plugins?.(serveContext) || [])], context({ request, req, connectionParams }) { // Maybe Node-like environment if (req?.headers) { return { headers: (0, utils_1.getHeadersObj)(req.headers), connectionParams, }; } // Fetch environment if (request?.headers) { return { headers: (0, utils_1.getHeadersObj)(request.headers), connectionParams, }; } return undefined; }, cors: config.cors, graphiql: config.graphiql, batching: config.batching, graphqlEndpoint: config.graphqlEndpoint, maskedErrors: config.maskedErrors, }); fetchAPI ||= yoga.fetchAPI; logger = yoga.logger; Object.defineProperty(yoga, 'invalidateUnifiedGraph', { value: supergraphYogaPlugin.invalidateUnifiedGraph, configurable: true, }); return yoga; } exports.createServeRuntime = createServeRuntime;