Skip to content

Commit cdbf86c

Browse files
author
Jason Kossis
committed
fix(@nestjs/graphql): need to properly close websocket servers
1 parent bbd478e commit cdbf86c

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

packages/apollo/tests/subscriptions/compat.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ describe('Use graphql-ws + subscriptions-transport-ws', () => {
169169
try {
170170
await wsClient?.dispose();
171171
} catch {}
172-
await subWsClient?.close();
172+
subWsClient?.close();
173173
await app.close();
174174
jest.clearAllMocks();
175175
});

packages/graphql/lib/services/gql-subscription.service.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import {
33
GraphQLSchema,
44
subscribe as graphqlSubscribe,
55
} from 'graphql';
6-
import { GRAPHQL_TRANSPORT_WS_PROTOCOL, ServerOptions } from 'graphql-ws';
6+
import {
7+
Disposable,
8+
GRAPHQL_TRANSPORT_WS_PROTOCOL,
9+
ServerOptions,
10+
} from 'graphql-ws';
711
import { useServer } from 'graphql-ws/lib/use/ws';
812
import {
913
GRAPHQL_WS,
@@ -51,6 +55,8 @@ export interface GqlSubscriptionServiceOptions extends SubscriptionConfig {
5155
export class GqlSubscriptionService {
5256
private readonly wss: ws.Server;
5357
private readonly subTransWs: ws.Server;
58+
private wsGqlDisposable: Disposable;
59+
private subServer: SubscriptionServer;
5460

5561
constructor(
5662
private readonly options: GqlSubscriptionServiceOptions,
@@ -83,7 +89,7 @@ export class GqlSubscriptionService {
8389
const graphqlWsOptions =
8490
this.options['graphql-ws'] === true ? {} : this.options['graphql-ws'];
8591
supportedProtocols.push(GRAPHQL_TRANSPORT_WS_PROTOCOL);
86-
useServer(
92+
this.wsGqlDisposable = useServer(
8793
{
8894
schema: this.options.schema,
8995
execute,
@@ -102,7 +108,7 @@ export class GqlSubscriptionService {
102108
: this.options['subscriptions-transport-ws'];
103109

104110
supportedProtocols.push(GRAPHQL_WS);
105-
SubscriptionServer.create(
111+
this.subServer = SubscriptionServer.create(
106112
{
107113
schema: this.options.schema,
108114
execute,
@@ -139,11 +145,7 @@ export class GqlSubscriptionService {
139145
}
140146

141147
async stop() {
142-
for (const client of this.wss.clients) {
143-
client.close(1001, 'Going away');
144-
}
145-
for (const client of this.subTransWs.clients) {
146-
client.close(1001, 'Going away');
147-
}
148+
await this.wsGqlDisposable?.dispose();
149+
this.subServer?.close();
148150
}
149151
}

0 commit comments

Comments
 (0)