1
1
import * as _ from 'lodash' ;
2
+ import * as events from 'events' ;
2
3
import { GraphQLServer } from 'graphql-yoga'
3
4
import { GraphQLScalarType } from 'graphql' ;
4
5
@@ -24,6 +25,7 @@ const typeDefs = `
24
25
id: ID!,
25
26
proxyPort: Int!
26
27
): Boolean!
28
+ triggerUpdate: Void
27
29
}
28
30
29
31
type InterceptionConfig {
@@ -40,11 +42,13 @@ const typeDefs = `
40
42
41
43
scalar Json
42
44
scalar Error
45
+ scalar Void
43
46
`
44
47
45
48
const buildResolvers = (
46
49
config : HtkConfig ,
47
- interceptors : _ . Dictionary < Interceptor >
50
+ interceptors : _ . Dictionary < Interceptor > ,
51
+ eventEmitter : events . EventEmitter
48
52
) => {
49
53
return {
50
54
Query : {
@@ -73,6 +77,9 @@ const buildResolvers = (
73
77
74
78
await interceptor . deactivate ( proxyPort , options ) ;
75
79
return ! interceptor . isActive ( proxyPort ) ;
80
+ } ,
81
+ triggerUpdate : ( ) => {
82
+ eventEmitter . emit ( 'update-requested' ) ;
76
83
}
77
84
} ,
78
85
@@ -93,6 +100,14 @@ const buildResolvers = (
93
100
parseLiteral : ( ) : any => { throw new Error ( 'JSON literals are not supported' ) }
94
101
} ) ,
95
102
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
+
96
111
Error : new GraphQLScalarType ( {
97
112
name : 'Error' ,
98
113
description : 'An error' ,
@@ -114,16 +129,18 @@ const buildResolvers = (
114
129
}
115
130
} ;
116
131
117
- export class HttpToolkitServer {
132
+ export class HttpToolkitServer extends events . EventEmitter {
118
133
119
134
private graphql : GraphQLServer ;
120
135
121
136
constructor ( config : HtkConfig ) {
137
+ super ( ) ;
138
+
122
139
let interceptors = buildInterceptors ( config ) ;
123
140
124
141
this . graphql = new GraphQLServer ( {
125
142
typeDefs,
126
- resolvers : buildResolvers ( config , interceptors )
143
+ resolvers : buildResolvers ( config , interceptors , this )
127
144
} ) ;
128
145
}
129
146
0 commit comments