11import * as _ from 'lodash' ;
2+ import * as events from 'events' ;
23import { GraphQLServer } from 'graphql-yoga'
34import { GraphQLScalarType } from 'graphql' ;
45
@@ -24,6 +25,7 @@ const typeDefs = `
2425 id: ID!,
2526 proxyPort: Int!
2627 ): Boolean!
28+ triggerUpdate: Void
2729 }
2830
2931 type InterceptionConfig {
@@ -40,11 +42,13 @@ const typeDefs = `
4042
4143 scalar Json
4244 scalar Error
45+ scalar Void
4346`
4447
4548const buildResolvers = (
4649 config : HtkConfig ,
47- interceptors : _ . Dictionary < Interceptor >
50+ interceptors : _ . Dictionary < Interceptor > ,
51+ eventEmitter : events . EventEmitter
4852) => {
4953 return {
5054 Query : {
@@ -73,6 +77,9 @@ const buildResolvers = (
7377
7478 await interceptor . deactivate ( proxyPort , options ) ;
7579 return ! interceptor . isActive ( proxyPort ) ;
80+ } ,
81+ triggerUpdate : ( ) => {
82+ eventEmitter . emit ( 'update-requested' ) ;
7683 }
7784 } ,
7885
@@ -93,6 +100,14 @@ const buildResolvers = (
93100 parseLiteral : ( ) : any => { throw new Error ( 'JSON literals are not supported' ) }
94101 } ) ,
95102
103+ Void : new GraphQLScalarType ( {
104+ name : 'Void' ,
105+ description : 'Nothing at all' ,
106+ serialize : ( value : any ) => null ,
107+ parseValue : ( input : string ) : any => null ,
108+ parseLiteral : ( ) : any => { throw new Error ( 'Void literals are not supported' ) }
109+ } ) ,
110+
96111 Error : new GraphQLScalarType ( {
97112 name : 'Error' ,
98113 description : 'An error' ,
@@ -114,16 +129,18 @@ const buildResolvers = (
114129 }
115130} ;
116131
117- export class HttpToolkitServer {
132+ export class HttpToolkitServer extends events . EventEmitter {
118133
119134 private graphql : GraphQLServer ;
120135
121136 constructor ( config : HtkConfig ) {
137+ super ( ) ;
138+
122139 let interceptors = buildInterceptors ( config ) ;
123140
124141 this . graphql = new GraphQLServer ( {
125142 typeDefs,
126- resolvers : buildResolvers ( config , interceptors )
143+ resolvers : buildResolvers ( config , interceptors , this )
127144 } ) ;
128145 }
129146
0 commit comments