UNPKG

@graphql-mesh/http

Version:
54 lines (53 loc) 2.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.graphqlHandler = void 0; const graphql_yoga_1 = require("graphql-yoga"); const utils_1 = require("@graphql-tools/utils"); const promise_helpers_1 = require("@whatwg-node/promise-helpers"); const graphqlHandler = ({ getBuiltMesh, playgroundTitle, playgroundEnabled, playgroundOffline, graphqlEndpoint, corsConfig, batchingLimit, healthCheckEndpoint = '/healthcheck', extraParamNames, renderGraphiQL: renderGraphiQLFn, }) => { const getYogaForMesh = (0, utils_1.memoize1)(function getYogaForMesh(mesh) { const yogaOptions = { plugins: [ ...mesh.plugins, (0, graphql_yoga_1.useLogger)({ skipIntrospection: true, logFn: (eventName, { args }) => { if (eventName.endsWith('-start')) { mesh.logger.debug(`\t headers: `, args.contextValue.headers); } }, }), ], extraParamNames, logging: mesh.logger, maskedErrors: false, graphiql: playgroundEnabled && { title: playgroundTitle, }, cors: corsConfig, graphqlEndpoint, landingPage: false, batching: batchingLimit ? { limit: batchingLimit } : false, healthCheckEndpoint, disposeOnProcessTerminate: true, }; if (playgroundEnabled && playgroundOffline) { if (!renderGraphiQLFn) { throw new Error('serve.playground.offline is enabled but renderGraphiQL was not provided. Import `renderGraphiQL` from `@graphql-yoga/render-graphiql` and pass it to createMeshHTTPHandler (generated Mesh artifacts do this automatically).'); } yogaOptions.renderGraphiQL = renderGraphiQLFn; } const yoga = (0, graphql_yoga_1.createYoga)(yogaOptions); // Dispose Yoga instance when the Mesh instance is destroyed const id = mesh.pubsub.subscribe('destroy', () => { const unsubscribe = () => { return mesh.pubsub.unsubscribe(id); }; // eslint-disable-next-line @typescript-eslint/no-floating-promises (0, promise_helpers_1.handleMaybePromise)(() => yoga.dispose(), unsubscribe, unsubscribe); }); return yoga; }); return (request, ctx) => getBuiltMesh().then(mesh => getYogaForMesh(mesh).handleRequest(request, ctx)); }; exports.graphqlHandler = graphqlHandler;