diff --git a/packages/graphql/lib/graphql.module.ts b/packages/graphql/lib/graphql.module.ts index 9cf5c38db..a383cf5ef 100644 --- a/packages/graphql/lib/graphql.module.ts +++ b/packages/graphql/lib/graphql.module.ts @@ -1,6 +1,7 @@ import { Inject, Logger, Module, RequestMethod } from '@nestjs/common'; import { DynamicModule, + OnApplicationShutdown, OnModuleDestroy, OnModuleInit, Provider, @@ -55,7 +56,7 @@ import { extend, generateString } from './utils'; export class GraphQLModule< TAdapter extends AbstractGraphQLDriver = AbstractGraphQLDriver, > - implements OnModuleInit, OnModuleDestroy + implements OnModuleInit, OnModuleDestroy, OnApplicationShutdown { public completeOptions: GqlModuleOptions | undefined; private static readonly logger = new Logger(GraphQLModule.name, { @@ -76,7 +77,15 @@ export class GraphQLModule< ) {} async onModuleDestroy() { - await this._graphQlAdapter.stop(); + if (!this.options.stopOnApplicationShutdown) { + await this._graphQlAdapter.stop(); + } + } + + async onApplicationShutdown() { + if (this.options.stopOnApplicationShutdown) { + await this._graphQlAdapter.stop(); + } } static forRoot = GqlModuleOptions>( diff --git a/packages/graphql/lib/interfaces/gql-module-options.interface.ts b/packages/graphql/lib/interfaces/gql-module-options.interface.ts index 7c56daca2..46e941f64 100644 --- a/packages/graphql/lib/interfaces/gql-module-options.interface.ts +++ b/packages/graphql/lib/interfaces/gql-module-options.interface.ts @@ -148,6 +148,14 @@ export interface GqlModuleOptions { * @default true */ introspection?: boolean; + + /** + * If `true`, the GraphQL server will stop during `onApplicationShutdown` + * instead of `onModuleDestroy`. This allows the server to keep accepting + * requests during `beforeApplicationShutdown`, enabling graceful shutdown. + * @default false + */ + stopOnApplicationShutdown?: boolean; } export interface GqlOptionsFactory<