UNPKG

@graphql-mesh/serve-runtime

Version:
50 lines (49 loc) 2.36 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useCompleteSubscriptionsOnDispose = useCompleteSubscriptionsOnDispose; const graphql_yoga_1 = require("graphql-yoga"); function useCompleteSubscriptionsOnDispose(disposableStack) { function createShutdownError() { return (0, graphql_yoga_1.createGraphQLError)('subscription has been closed because the server is shutting down', { extensions: { code: 'SHUTTING_DOWN', }, }); } return { onSubscribe() { return { onSubscribeResult({ result, setResult }) { if ((0, graphql_yoga_1.isAsyncIterable)(result)) { // If shutdown has already been initiated, return an error immediately if (disposableStack.disposed) { // Complete the subscription immediately // eslint-disable-next-line @typescript-eslint/no-floating-promises result.return?.(); setResult({ errors: [createShutdownError()], }); } setResult(graphql_yoga_1.Repeater.race([ result, new graphql_yoga_1.Repeater((_push, stop) => { // eslint-disable-next-line @typescript-eslint/no-floating-promises stop.then(() => result.return?.()); // If shutdown has already been initiated, complete the subscription immediately if (disposableStack.disposed) { stop(createShutdownError()); } else { // If shutdown is initiated after this point, attach it to the disposable stack disposableStack.defer(() => { stop(createShutdownError()); }); } }), ])); } }, }; }, }; }