-
-
Notifications
You must be signed in to change notification settings - Fork 281
/
GraphQLWSModule.ts
55 lines (46 loc) · 1.38 KB
/
GraphQLWSModule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import {Constant, Inject, InjectorService, Module} from "@tsed/di";
import {useServer} from "graphql-ws/lib/use/ws";
import Http from "http";
import Https from "https";
import {WebSocketServer} from "ws";
import {GraphQLWSOptions} from "./GraphQLWSOptions";
@Module()
export class GraphQLWSModule {
@Constant("graphqlWs", {})
private settings: GraphQLWSOptions;
@Inject(Http.Server)
private httpServer: Http.Server | null;
@Inject(Https.Server)
private httpsServer: Https.Server | null;
@Inject()
private injector: InjectorService;
createWSServer(settings: GraphQLWSOptions) {
const wsServer = new WebSocketServer({
...(this.settings.wsServerOptions || {}),
...settings.wsServerOptions,
server: this.httpsServer || this.httpServer!,
path: settings.path
});
return useServer(
{
...(this.settings.wsUseServerOptions || {}),
...settings.wsUseServerOptions,
schema: settings.schema
},
wsServer
);
}
async $alterApolloServerPlugins(plugins: any[], settings: GraphQLWSOptions) {
const wsServer = await this.createWSServer(settings);
this.injector.logger.info(`Create GraphQL WS server on: ${settings.path}`);
return plugins.concat({
serverWillStart() {
return {
async drainServer() {
await wsServer.dispose();
}
};
}
} as any);
}
}