@@ -3,7 +3,11 @@ import {
3
3
GraphQLSchema ,
4
4
subscribe as graphqlSubscribe ,
5
5
} 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' ;
7
11
import { useServer } from 'graphql-ws/lib/use/ws' ;
8
12
import {
9
13
GRAPHQL_WS ,
@@ -51,6 +55,8 @@ export interface GqlSubscriptionServiceOptions extends SubscriptionConfig {
51
55
export class GqlSubscriptionService {
52
56
private readonly wss : ws . Server ;
53
57
private readonly subTransWs : ws . Server ;
58
+ private wsGqlDisposable : Disposable ;
59
+ private subServer : SubscriptionServer ;
54
60
55
61
constructor (
56
62
private readonly options : GqlSubscriptionServiceOptions ,
@@ -83,7 +89,7 @@ export class GqlSubscriptionService {
83
89
const graphqlWsOptions =
84
90
this . options [ 'graphql-ws' ] === true ? { } : this . options [ 'graphql-ws' ] ;
85
91
supportedProtocols . push ( GRAPHQL_TRANSPORT_WS_PROTOCOL ) ;
86
- useServer (
92
+ this . wsGqlDisposable = useServer (
87
93
{
88
94
schema : this . options . schema ,
89
95
execute,
@@ -102,7 +108,7 @@ export class GqlSubscriptionService {
102
108
: this . options [ 'subscriptions-transport-ws' ] ;
103
109
104
110
supportedProtocols . push ( GRAPHQL_WS ) ;
105
- SubscriptionServer . create (
111
+ this . subServer = SubscriptionServer . create (
106
112
{
107
113
schema : this . options . schema ,
108
114
execute,
@@ -139,11 +145,7 @@ export class GqlSubscriptionService {
139
145
}
140
146
141
147
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 ( ) ;
148
150
}
149
151
}
0 commit comments