forked from apollographql/apollo-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
RequestsManager.ts
175 lines (155 loc) · 5.02 KB
/
RequestsManager.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
import { IObservable, Observer, Observable, Subscription } from './Observable';
import { ReactiveQueryOptions, runQueryReactive } from './runQueryReactive';
import { ReactiveGraphQLOptions } from './reactiveOptions';
import { formatError, ExecutionResult } from 'graphql';
import {
RGQL_MSG_ERROR,
RGQL_MSG_COMPLETE,
RGQL_MSG_DATA,
RGQL_MSG_START,
RGQL_MSG_STOP,
RGQLMessageType,
RGQLPayloadType,
RGQLPayloadStart,
} from './messageTypes';
export interface RGQLPacket {
id: number; // Per socket increasing number
type: RGQLMessageType;
payload: RGQLPayloadType,
}
export interface ReactiveRequest {
packet: RGQLPacket;
graphqlOptions?: ReactiveGraphQLOptions;
}
export class RequestsManager {
protected requests = {};
public handleRequest(message: ReactiveRequest, onMessageObserver: Observer<RGQLPacket>) {
this._subscribeRequest(this._prepareRequest(message), message.packet.id, onMessageObserver);
}
public unsubscribeAll() {
Object.keys(this.requests).forEach((k) => {
this._unsubscribe(parseInt(k, 10));
});
}
protected _prepareRespond(obs: IObservable<ExecutionResult>, key: number): IObservable<RGQLPacket> {
return new Observable((observer) => {
return this._convertRespond(obs).subscribe({
next: (data) => {
if ( undefined !== key ) {
observer.next(Object.assign(data, { id: key }));
} else {
observer.next(data);
}
},
error: observer.error,
complete: observer.complete,
});
});
}
protected _convertRespond(obs: IObservable<ExecutionResult>): IObservable<RGQLPacket> {
return new Observable((observer) => {
return obs.subscribe({
next: (data) => {
observer.next({
type: RGQL_MSG_DATA,
payload: data,
});
},
error: (e) => {
observer.next({
type: RGQL_MSG_ERROR,
payload: e,
});
observer.complete();
},
complete: () => {
observer.next({
type: RGQL_MSG_COMPLETE,
});
observer.complete();
},
});
});
}
protected _subscribeRequest(obs: IObservable<ExecutionResult>, key: number, onMessageObserver: Observer<RGQLPacket>): void {
const respondObs = this._prepareRespond(obs, key);
if ( key ) {
this.requests[key] = respondObs.subscribe(onMessageObserver);
} else {
respondObs.subscribe(onMessageObserver);
}
}
protected _unsubscribe(key: number) {
if ( this.requests.hasOwnProperty(key) ) {
this.requests[key].unsubscribe();
delete this.requests[key];
}
}
protected _prepareRequest({packet, graphqlOptions}: ReactiveRequest): IObservable<ExecutionResult> {
const formatErrorFn = graphqlOptions.formatError || formatError;
try {
this._validateRequest(packet);
} catch (e) {
return Observable.of({ errors: [formatErrorFn(e)] });
}
if (Array.isArray(packet)) {
return Observable.of({ errors: [formatErrorFn(new Error('Websocket does not support batching'))] });
}
this._unsubscribe(packet.id);
if ( packet.type === RGQL_MSG_STOP ) {
return Observable.empty();
}
const graphqlRequest: RGQLPayloadStart = packet.payload;
const query = graphqlRequest.query;
const operationName = graphqlRequest.operationName;
let variables = graphqlRequest.variables;
if (typeof variables === 'string') {
try {
variables = JSON.parse(variables);
} catch (error) {
return Observable.of({ errors: [formatErrorFn(new Error('Variables are invalid JSON.'))] });
}
}
let params = {
schema: graphqlOptions.schema,
query: query,
variables: variables,
context: graphqlOptions.context,
rootValue: graphqlOptions.rootValue,
operationName: operationName,
logFunction: graphqlOptions.logFunction,
validationRules: graphqlOptions.validationRules,
formatError: formatErrorFn,
formatResponse: graphqlOptions.formatResponse,
debug: graphqlOptions.debug,
executeReactive: graphqlOptions.executor.executeReactive.bind(graphqlOptions.executor),
};
if (graphqlOptions.formatParams) {
params = graphqlOptions.formatParams(params);
}
return runQueryReactive(params);
}
protected _validateRequest(packet: RGQLPacket) {
if ( undefined === packet.id ) {
throw new Error('Message missing id field');
}
if ( undefined === packet.type ) {
throw new Error('Message missing type field');
}
switch ( packet.type ) {
case RGQL_MSG_START:
if ( undefined === packet.payload ) {
throw new Error('Message missing payload field');
}
if ( undefined === (<RGQLPayloadStart> packet.payload).query ) {
throw new Error('Message missing payload.query field');
}
return;
case RGQL_MSG_STOP:
// Nothing much to check, no payload.
return;
default:
throw new Error('Invalid type used');
}
}
}