UNPKG

@nestjs/graphql

Version:

Nest - modern, fast, powerful node.js web framework (@graphql)

50 lines 2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GqlSubscriptionService = void 0; const graphql_1 = require("graphql"); const graphql_ws_1 = require("graphql-ws"); const ws_1 = require("graphql-ws/use/ws"); const ws_2 = require("ws"); class GqlSubscriptionService { constructor(options, httpServer) { this.options = options; this.httpServer = httpServer; this.wss = new ws_2.WebSocketServer({ path: this.options['graphql-ws']?.path ?? this.options.path, noServer: true, }); this.initialize(); } initialize() { const { execute = graphql_1.execute, subscribe = graphql_1.subscribe } = this.options; if ('graphql-ws' in this.options) { const graphqlWsOptions = this.options['graphql-ws'] === true ? {} : this.options['graphql-ws']; this.wsGqlDisposable = (0, ws_1.useServer)({ schema: this.options.schema, execute, subscribe, context: this.options.context, ...graphqlWsOptions, }, this.wss); } this.httpServer.on('upgrade', (req, socket, head) => { const protocol = req.headers['sec-websocket-protocol']; let protocols = Array.isArray(protocol) ? protocol : protocol?.split(',').map((p) => p.trim()); protocols = protocols?.filter((supportedProtocol) => supportedProtocol === graphql_ws_1.GRAPHQL_TRANSPORT_WS_PROTOCOL); const wss = this.wss; if (req.url?.startsWith(wss.options.path)) { wss.handleUpgrade(req, socket, head, (ws) => { wss.emit('connection', ws, req); }); } }); } async stop() { await this.wsGqlDisposable?.dispose(); } } exports.GqlSubscriptionService = GqlSubscriptionService; //# sourceMappingURL=gql-subscription.service.js.map