import { ExecutionResult } from 'graphql' ; import { Observable , OperationOptions , SubscriptionClient } from 'subscriptions-transport-ws' ; import * as WebSocket from 'ws' ; export class SubscriptionCoordinator { private readonly wsClientTable : { [ url ] : SubscriptionClient ; } = { } ; forwardRequest ( url , request ) { return this . getClient ( url ) . request ( request ) ; } unsubscribeAll ( ) { Object . keys ( this . wsClientTable ) . forEach ( ( url ) => { this . wsClientTable [ url ] . unsubscribeAll ( ) ; delete this . wsClientTable [ url ] ; } ) ; } private getClient ( url ) { if ( ! this . wsClientTable [ url ] ) { const newClient = this . initializeClient ( url ) ; this . wsClientTable [ url ] = newClient ; } return this . wsClientTable [ url ] ; } private initializeClient ( url ) { return new SubscriptionClient ( url , { reconnect : true } , WebSocket ) . use ( [ { applyMiddleware : ( options , next ) => { if ( ! options . query ) { options . query = 'to be set' ; } next ( ) ; } } ] ) ; } }