@graphql-mesh/http
Version:
50 lines (49 loc) • 2.4 kB
JavaScript
import { createYoga, useLogger } from 'graphql-yoga';
import { memoize1 } from '@graphql-tools/utils';
import { handleMaybePromise } from '@whatwg-node/promise-helpers';
export const graphqlHandler = ({ getBuiltMesh, playgroundTitle, playgroundEnabled, playgroundOffline, graphqlEndpoint, corsConfig, batchingLimit, healthCheckEndpoint = '/healthcheck', extraParamNames, renderGraphiQL: renderGraphiQLFn, }) => {
const getYogaForMesh = memoize1(function getYogaForMesh(mesh) {
const yogaOptions = {
plugins: [
...mesh.plugins,
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 = 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
handleMaybePromise(() => yoga.dispose(), unsubscribe, unsubscribe);
});
return yoga;
});
return (request, ctx) => getBuiltMesh().then(mesh => getYogaForMesh(mesh).handleRequest(request, ctx));
};