This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 341
/
server.ts
478 lines (411 loc) · 17 KB
/
server.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import * as WebSocket from 'ws';
import MessageTypes from './message-types';
import { GRAPHQL_WS, GRAPHQL_SUBSCRIPTIONS } from './protocol';
import isObject from './utils/is-object';
import {
parse,
ExecutionResult,
GraphQLSchema,
DocumentNode,
validate,
ValidationContext,
specifiedRules,
GraphQLFieldResolver,
ExecutionArgs,
SubscriptionArgs,
} from 'graphql';
import { createEmptyIterable } from './utils/empty-iterable';
import { createAsyncIterator, forAwaitEach, isAsyncIterable } from 'iterall';
import { isASubscriptionOperation } from './utils/is-subscriptions';
import { parseLegacyProtocolMessage } from './legacy/parse-legacy-protocol';
import { IncomingMessage } from 'http';
export type ExecutionIterator = AsyncIterator<ExecutionResult>;
export interface ExecutionParams<TContext = any> {
query: string | DocumentNode;
variables: { [key: string]: any };
operationName: string;
context: TContext;
formatResponse?: Function;
formatError?: Function;
callback?: Function;
schema?: GraphQLSchema;
}
export type ConnectionContext = {
initPromise?: Promise<any>,
isLegacy: boolean,
socket: WebSocket,
request: IncomingMessage,
operations: {
[opId: string]: ExecutionIterator,
},
};
export interface OperationMessagePayload {
[key: string]: any; // this will support for example any options sent in init like the auth token
query?: string;
variables?: { [key: string]: any };
operationName?: string;
}
export interface OperationMessage {
payload?: OperationMessagePayload;
id?: string;
type: string;
}
export type ExecuteFunction = (args: ExecutionArgs) =>
| ExecutionResult
| Promise<ExecutionResult>
| AsyncIterator<ExecutionResult>;
export type SubscribeFunction = (args: SubscriptionArgs) =>
| AsyncIterator<ExecutionResult>
| Promise<AsyncIterator<ExecutionResult> | ExecutionResult>;
export interface ServerOptions {
rootValue?: any;
schema?: GraphQLSchema;
execute?: ExecuteFunction;
subscribe?: SubscribeFunction;
validationRules?:
Array<(context: ValidationContext) => any> | ReadonlyArray<any>;
onOperation?: Function;
onOperationComplete?: Function;
onConnect?: Function;
onDisconnect?: Function;
keepAlive?: number;
}
const isWebSocketServer = (socket: any) => socket.on;
export class SubscriptionServer {
private onOperation: Function;
private onOperationComplete: Function;
private onConnect: Function;
private onDisconnect: Function;
private wsServer: WebSocket.Server;
private execute: ExecuteFunction;
private subscribe: SubscribeFunction;
private schema: GraphQLSchema;
private rootValue: any;
private keepAlive: number;
private closeHandler: () => void;
private specifiedRules:
Array<(context: ValidationContext) => any> |
ReadonlyArray<any>;
public static create(options: ServerOptions, socketOptionsOrServer: WebSocket.ServerOptions | WebSocket.Server) {
return new SubscriptionServer(options, socketOptionsOrServer);
}
constructor(options: ServerOptions, socketOptionsOrServer: WebSocket.ServerOptions | WebSocket.Server) {
const {
onOperation, onOperationComplete, onConnect, onDisconnect, keepAlive,
} = options;
this.specifiedRules = options.validationRules || specifiedRules;
this.loadExecutor(options);
this.onOperation = onOperation;
this.onOperationComplete = onOperationComplete;
this.onConnect = onConnect;
this.onDisconnect = onDisconnect;
this.keepAlive = keepAlive;
if (isWebSocketServer(socketOptionsOrServer)) {
this.wsServer = <WebSocket.Server>socketOptionsOrServer;
} else {
// Init and connect WebSocket server to http
this.wsServer = new WebSocket.Server(socketOptionsOrServer || {});
}
const connectionHandler = ((socket: WebSocket, request: IncomingMessage) => {
// Add `upgradeReq` to the socket object to support old API, without creating a memory leak
// See: https://github.com/websockets/ws/pull/1099
(socket as any).upgradeReq = request;
// NOTE: the old GRAPHQL_SUBSCRIPTIONS protocol support should be removed in the future
if (socket.protocol === undefined ||
(socket.protocol.indexOf(GRAPHQL_WS) === -1 && socket.protocol.indexOf(GRAPHQL_SUBSCRIPTIONS) === -1)) {
// Close the connection with an error code, ws v2 ensures that the
// connection is cleaned up even when the closing handshake fails.
// 1002: protocol error
socket.close(1002);
return;
}
const connectionContext: ConnectionContext = Object.create(null);
connectionContext.initPromise = Promise.resolve(true);
connectionContext.isLegacy = false;
connectionContext.socket = socket;
connectionContext.request = request;
connectionContext.operations = {};
const connectionClosedHandler = (error: any) => {
if (error) {
this.sendError(
connectionContext,
'',
{ message: error.message ? error.message : error },
MessageTypes.GQL_CONNECTION_ERROR,
);
setTimeout(() => {
// 1011 is an unexpected condition prevented the request from being fulfilled
connectionContext.socket.close(1011);
}, 10);
}
this.onClose(connectionContext);
if (this.onDisconnect) {
this.onDisconnect(socket, connectionContext);
}
};
socket.on('error', connectionClosedHandler);
socket.on('close', connectionClosedHandler);
socket.on('message', this.onMessage(connectionContext));
});
this.wsServer.on('connection', connectionHandler);
this.closeHandler = () => {
this.wsServer.removeListener('connection', connectionHandler);
this.wsServer.close();
};
}
public get server(): WebSocket.Server {
return this.wsServer;
}
public close(): void {
this.closeHandler();
}
private loadExecutor(options: ServerOptions) {
const { execute, subscribe, schema, rootValue } = options;
if (!execute) {
throw new Error('Must provide `execute` for websocket server constructor.');
}
this.schema = schema;
this.rootValue = rootValue;
this.execute = execute;
this.subscribe = subscribe;
}
private unsubscribe(connectionContext: ConnectionContext, opId: string) {
if (connectionContext.operations && connectionContext.operations[opId]) {
if (connectionContext.operations[opId].return) {
connectionContext.operations[opId].return();
}
delete connectionContext.operations[opId];
if (this.onOperationComplete) {
this.onOperationComplete(connectionContext.socket, opId);
}
}
}
private onClose(connectionContext: ConnectionContext) {
Object.keys(connectionContext.operations).forEach((opId) => {
this.unsubscribe(connectionContext, opId);
});
}
private onMessage(connectionContext: ConnectionContext) {
return (message: any) => {
let parsedMessage: OperationMessage;
try {
parsedMessage = parseLegacyProtocolMessage(connectionContext, JSON.parse(message));
} catch (e) {
this.sendError(connectionContext, null, { message: e.message }, MessageTypes.GQL_CONNECTION_ERROR);
return;
}
const opId = parsedMessage.id;
switch (parsedMessage.type) {
case MessageTypes.GQL_CONNECTION_INIT:
if (this.onConnect) {
connectionContext.initPromise = new Promise((resolve, reject) => {
try {
// TODO - this should become a function call with just 2 arguments in the future
// when we release the breaking change api: parsedMessage.payload and connectionContext
resolve(this.onConnect(parsedMessage.payload, connectionContext.socket, connectionContext));
} catch (e) {
reject(e);
}
});
}
connectionContext.initPromise.then((result) => {
if (result === false) {
throw new Error('Prohibited connection!');
}
this.sendMessage(
connectionContext,
undefined,
MessageTypes.GQL_CONNECTION_ACK,
undefined,
);
if (this.keepAlive) {
this.sendKeepAlive(connectionContext);
// Regular keep alive messages if keepAlive is set
const keepAliveTimer = setInterval(() => {
if (connectionContext.socket.readyState === WebSocket.OPEN) {
this.sendKeepAlive(connectionContext);
} else {
clearInterval(keepAliveTimer);
}
}, this.keepAlive);
}
}).catch((error: Error) => {
this.sendError(
connectionContext,
opId,
{ message: error.message },
MessageTypes.GQL_CONNECTION_ERROR,
);
// Close the connection with an error code, ws v2 ensures that the
// connection is cleaned up even when the closing handshake fails.
// 1011: an unexpected condition prevented the operation from being fulfilled
// We are using setTimeout because we want the message to be flushed before
// disconnecting the client
setTimeout(() => {
connectionContext.socket.close(1011);
}, 10);
});
break;
case MessageTypes.GQL_CONNECTION_TERMINATE:
connectionContext.socket.close();
break;
case MessageTypes.GQL_START:
connectionContext.initPromise.then((initResult) => {
// if we already have a subscription with this id, unsubscribe from it first
if (connectionContext.operations && connectionContext.operations[opId]) {
this.unsubscribe(connectionContext, opId);
}
const baseParams: ExecutionParams = {
query: parsedMessage.payload.query,
variables: parsedMessage.payload.variables,
operationName: parsedMessage.payload.operationName,
context: isObject(initResult) ? Object.assign(Object.create(Object.getPrototypeOf(initResult)), initResult) : {},
formatResponse: <any>undefined,
formatError: <any>undefined,
callback: <any>undefined,
schema: this.schema,
};
let promisedParams = Promise.resolve(baseParams);
// set an initial mock subscription to only registering opId
connectionContext.operations[opId] = createEmptyIterable();
if (this.onOperation) {
let messageForCallback: any = parsedMessage;
promisedParams = Promise.resolve(this.onOperation(messageForCallback, baseParams, connectionContext.socket));
}
return promisedParams.then((params) => {
if (typeof params !== 'object') {
const error = `Invalid params returned from onOperation! return values must be an object!`;
this.sendError(connectionContext, opId, { message: error });
throw new Error(error);
}
if (!params.schema) {
const error = 'Missing schema information. The GraphQL schema should be provided either statically in' +
' the `SubscriptionServer` constructor or as a property on the object returned from onOperation!';
this.sendError(connectionContext, opId, { message: error });
throw new Error(error);
}
const document = typeof baseParams.query !== 'string' ? baseParams.query : parse(baseParams.query);
let executionPromise: Promise<AsyncIterator<ExecutionResult> | ExecutionResult>;
const validationErrors = validate(params.schema, document, this.specifiedRules);
if ( validationErrors.length > 0 ) {
executionPromise = Promise.resolve({ errors: validationErrors });
} else {
let executor: SubscribeFunction | ExecuteFunction = this.execute;
if (this.subscribe && isASubscriptionOperation(document, params.operationName)) {
executor = this.subscribe;
}
executionPromise = Promise.resolve(
executor({
schema: params.schema,
document,
rootValue: this.rootValue,
contextValue: params.context,
variableValues: params.variables,
operationName: params.operationName,
}),
);
}
return executionPromise.then((executionResult) => ({
executionIterable: isAsyncIterable(executionResult) ?
executionResult : createAsyncIterator([ executionResult ]),
params,
}));
}).then(({ executionIterable, params }) => {
forAwaitEach(
executionIterable as any,
(value: ExecutionResult) => {
let result = value;
if (params.formatResponse) {
try {
result = params.formatResponse(value, params);
} catch (err) {
console.error('Error in formatResponse function:', err);
}
}
this.sendMessage(connectionContext, opId, MessageTypes.GQL_DATA, result);
})
.then(() => {
this.sendMessage(connectionContext, opId, MessageTypes.GQL_COMPLETE, null);
})
.catch((e: Error) => {
let error = e;
if (params.formatError) {
try {
error = params.formatError(e, params);
} catch (err) {
console.error('Error in formatError function: ', err);
}
}
// plain Error object cannot be JSON stringified.
if (Object.keys(error).length === 0) {
error = { name: error.name, message: error.message };
}
this.sendError(connectionContext, opId, error);
});
return executionIterable;
}).then((subscription: ExecutionIterator) => {
connectionContext.operations[opId] = subscription;
}).then(() => {
// NOTE: This is a temporary code to support the legacy protocol.
// As soon as the old protocol has been removed, this coode should also be removed.
this.sendMessage(connectionContext, opId, MessageTypes.SUBSCRIPTION_SUCCESS, undefined);
}).catch((e: any) => {
if (e.errors) {
this.sendMessage(connectionContext, opId, MessageTypes.GQL_DATA, { errors: e.errors });
} else {
this.sendError(connectionContext, opId, { message: e.message });
}
// Remove the operation on the server side as it will be removed also in the client
this.unsubscribe(connectionContext, opId);
return;
});
}).catch((error) => {
// Handle initPromise rejected
this.sendError(connectionContext, opId, { message: error.message });
this.unsubscribe(connectionContext, opId);
});
break;
case MessageTypes.GQL_STOP:
// Find subscription id. Call unsubscribe.
this.unsubscribe(connectionContext, opId);
break;
default:
this.sendError(connectionContext, opId, { message: 'Invalid message type!' });
}
};
}
private sendKeepAlive(connectionContext: ConnectionContext): void {
if (connectionContext.isLegacy) {
this.sendMessage(connectionContext, undefined, MessageTypes.KEEP_ALIVE, undefined);
} else {
this.sendMessage(connectionContext, undefined, MessageTypes.GQL_CONNECTION_KEEP_ALIVE, undefined);
}
}
private sendMessage(connectionContext: ConnectionContext, opId: string, type: string, payload: any): void {
const parsedMessage = parseLegacyProtocolMessage(connectionContext, {
type,
id: opId,
payload,
});
if (parsedMessage && connectionContext.socket.readyState === WebSocket.OPEN) {
connectionContext.socket.send(JSON.stringify(parsedMessage));
}
}
private sendError(connectionContext: ConnectionContext, opId: string, errorPayload: any,
overrideDefaultErrorType?: string): void {
const sanitizedOverrideDefaultErrorType = overrideDefaultErrorType || MessageTypes.GQL_ERROR;
if ([
MessageTypes.GQL_CONNECTION_ERROR,
MessageTypes.GQL_ERROR,
].indexOf(sanitizedOverrideDefaultErrorType) === -1) {
throw new Error('overrideDefaultErrorType should be one of the allowed error messages' +
' GQL_CONNECTION_ERROR or GQL_ERROR');
}
this.sendMessage(
connectionContext,
opId,
sanitizedOverrideDefaultErrorType,
errorPayload,
);
}
}