-
Notifications
You must be signed in to change notification settings - Fork 896
/
GraphqlWSSubProtocol.java
426 lines (373 loc) · 17 KB
/
GraphqlWSSubProtocol.java
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
/*
* Copyright 2021 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package com.linecorp.armeria.server.graphql;
import static com.linecorp.armeria.internal.common.websocket.WebSocketUtil.maybeTruncate;
import static java.util.Collections.emptyList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.dataloader.DataLoaderRegistry;
import org.reactivestreams.Publisher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.linecorp.armeria.common.annotation.Nullable;
import com.linecorp.armeria.common.stream.StreamMessage;
import com.linecorp.armeria.common.websocket.WebSocketCloseStatus;
import com.linecorp.armeria.common.websocket.WebSocketWriter;
import com.linecorp.armeria.server.ServiceRequestContext;
import graphql.ErrorClassification;
import graphql.ErrorType;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import graphql.GraphQLError;
import graphql.language.SourceLocation;
/**
* Handles the graphql-ws sub protocol within a web socket.
* Handles potentially multiple subscriptions through the same web socket.
*/
class GraphqlWSSubProtocol {
private static final Logger logger = LoggerFactory.getLogger(GraphqlWSSubProtocol.class);
private static final ObjectMapper mapper = new ObjectMapper();
private final HashMap<String, ExecutionResultSubscriber> graphqlSubscriptions = new HashMap<>();
private static final TypeReference<Map<String, Object>> JSON_MAP =
new TypeReference<Map<String, Object>>() {};
private boolean connectionInitiated;
private final ServiceRequestContext ctx;
private final GraphqlExecutor graphqlExecutor;
private final Function<? super ServiceRequestContext, ? extends DataLoaderRegistry>
dataLoaderRegistryFunction;
private final Map<String, Object> upgradeCtx;
private Map<String, Object> connectionCtx = ImmutableMap.of();
GraphqlWSSubProtocol(
ServiceRequestContext ctx,
GraphqlExecutor graphqlExecutor,
Function<? super ServiceRequestContext, ? extends DataLoaderRegistry> dataLoaderRegistryFunction) {
this.ctx = ctx;
this.graphqlExecutor = graphqlExecutor;
this.dataLoaderRegistryFunction = dataLoaderRegistryFunction;
upgradeCtx = GraphqlServiceContexts.graphqlContext(ctx);
}
/**
* Called when a binary frame is received. Binary frames are not supported by the graphql-ws protocol.
*/
@Nullable
public void handleBinary(WebSocketWriter out) {
out.close(WebSocketCloseStatus.INVALID_MESSAGE_TYPE, "Binary frames are not supported");
}
/**
* Receives an event and returns a response if one should be sent.
*/
@Nullable
public void handleText(String event, WebSocketWriter out) {
if (!out.isOpen()) {
return;
}
try {
final Map<String, Object> eventMap = parseJsonString(event, JSON_MAP);
final String type = toStringFromJson(eventMap.get("type"));
if (type == null) {
throw new GraphqlWebSocketCloseException(4400, "type is required");
}
final String id;
switch (type) {
case "connection_init":
if (connectionInitiated) {
// Already initiated, that's an error
throw new GraphqlWebSocketCloseException(4429, "Already initiated");
}
final Object rawPayload = eventMap.get("payload");
if (rawPayload != null) {
connectionCtx = toMapFromJson(rawPayload);
}
connectionInitiated = true;
writeConnectionAck(out);
break;
case "ping":
writePong(out);
break;
case "pong":
break;
case "subscribe":
ensureInitiated();
id = toStringFromJson(eventMap.get("id"));
if (id == null) {
throw new GraphqlWebSocketCloseException(4400, "id is required");
}
final Map<String, Object> payload = toMapFromJson(eventMap.get("payload"));
try {
if (graphqlSubscriptions.containsKey(id)) {
// Subscription already exists
throw new GraphqlWebSocketCloseException(4409, "Already subscribed");
}
final String operationName = toStringFromJson(payload.get("operationName"));
final String query = toStringFromJson(payload.get("query"));
final Map<String, Object> variables = toMapFromJson(payload.get("variables"));
final Map<String, Object> extensions = toMapFromJson(payload.get("extensions"));
final ExecutionInput executionInput =
ExecutionInput.newExecutionInput()
.graphQLContext(connectionCtx)
.graphQLContext(upgradeCtx)
.query(query)
.variables(variables)
.operationName(operationName)
.extensions(extensions)
.dataLoaderRegistry(dataLoaderRegistryFunction.apply(ctx))
.build();
final CompletableFuture<ExecutionResult> future =
graphqlExecutor.executeGraphql(ctx, executionInput);
future.handleAsync((executionResult, throwable) -> {
handleExecutionResult(out, id, executionResult, throwable);
return null;
}, ctx.eventLoop());
} catch (GraphqlWebSocketCloseException e) {
logger.debug("Error handling subscription", e);
// Also cancel subscription if present before closing websocket
final ExecutionResultSubscriber s = graphqlSubscriptions.remove(id);
if (s != null) {
s.setCompleted();
}
out.close(e.getWebSocketCloseStatus());
} catch (Exception e) {
logger.debug("Error handling subscription", e);
// Unknown but possibly recoverable error
writeError(out, id, e);
return;
}
break;
case "complete":
ensureInitiated();
// Read id and remove that subscription
id = toStringFromJson(eventMap.get("id"));
if (id == null) {
throw new GraphqlWebSocketCloseException(4400, "id is required");
}
final ExecutionResultSubscriber s = graphqlSubscriptions.remove(id);
if (s != null) {
s.setCompleted();
}
return;
default:
final String reasonPhrase = maybeTruncate("Unknown event type: " + type);
assert reasonPhrase != null;
throw new GraphqlWebSocketCloseException(4400, reasonPhrase);
}
} catch (GraphqlWebSocketCloseException e) {
logger.debug("Error while handling event", e);
out.close(e.getWebSocketCloseStatus());
} catch (Exception e) {
logger.debug("Error while handling event", e);
out.close(e);
}
}
private void handleExecutionResult(WebSocketWriter out, String id,
@Nullable ExecutionResult executionResult, @Nullable Throwable t) {
if (t != null) {
logger.debug("Error handling subscription", t);
writeError(out, id, t);
return;
}
if (executionResult == null) {
logger.debug("ExecutionResult was null but no error was thrown");
writeError(out, id, new IllegalArgumentException("ExecutionResult was null"));
return;
}
if (!executionResult.getErrors().isEmpty()) {
try {
writeError(out, id, executionResult.getErrors());
} catch (JsonProcessingException e) {
logger.warn("Error serializing error event", e);
out.close(e);
}
return;
}
if (!(executionResult.getData() instanceof Publisher)) {
writeError(out, id, new Exception("Result of operation was not a subscription"));
return;
}
final Publisher<ExecutionResult> publisher = executionResult.getData();
final StreamMessage<ExecutionResult> streamMessage = StreamMessage.of(publisher);
final ExecutionResultSubscriber executionResultSubscriber =
new ExecutionResultSubscriber(id, new GraphqlSubProtocol() {
boolean completed;
@Override
public void sendResult(String operationId, ExecutionResult executionResult)
throws JsonProcessingException {
writeNext(out, operationId, executionResult);
}
@Override
public void sendGraphqlErrors(List<GraphQLError> errors) throws JsonProcessingException {
writeError(out, id, errors);
}
@Override
public void completeWithError(Throwable cause) {
if (completed) {
return;
}
completed = true;
writeError(out, id, cause);
graphqlSubscriptions.remove(id);
}
@Override
public void complete() {
if (completed) {
return;
}
completed = true;
writeComplete(out, id);
graphqlSubscriptions.remove(id);
}
});
graphqlSubscriptions.put(id, executionResultSubscriber);
streamMessage.subscribe(executionResultSubscriber, ctx.eventLoop());
}
void cancel() {
for (ExecutionResultSubscriber subscriber : graphqlSubscriptions.values()) {
subscriber.setCompleted();
}
graphqlSubscriptions.clear();
}
private void ensureInitiated() throws Exception {
if (!connectionInitiated) {
// ConnectionAck not sent yet. Must be closed with 4401 Unauthorized.
throw new GraphqlWebSocketCloseException(4401, "Unauthorized");
}
}
private static String serializeToJson(Object object) throws JsonProcessingException {
return mapper.writer().writeValueAsString(object);
}
@Nullable
private static String toStringFromJson(@Nullable Object value) throws GraphqlWebSocketCloseException {
if (value == null) {
return null;
}
if (value instanceof String) {
return (String) value;
} else {
throw new GraphqlWebSocketCloseException(4400, "Expected string value");
}
}
/**
* This only works reliably if maybeMap is from Json, as maps(objects) in Json
* can only have string keys.
*/
@SuppressWarnings("unchecked")
private static Map<String, Object> toMapFromJson(@Nullable Object maybeMap)
throws GraphqlWebSocketCloseException {
if (maybeMap == null) {
return ImmutableMap.of();
}
if (maybeMap instanceof Map) {
final Map<?, ?> map = (Map<?, ?>) maybeMap;
if (map.isEmpty()) {
return ImmutableMap.of();
}
return Collections.unmodifiableMap((Map<String, Object>) maybeMap);
} else {
throw new GraphqlWebSocketCloseException(4400, "Expected map value");
}
}
private static <T> T parseJsonString(String content, TypeReference<T> typeReference)
throws GraphqlWebSocketCloseException {
try {
return mapper.readValue(content, typeReference);
} catch (JsonProcessingException e) {
throw new GraphqlWebSocketCloseException(4400, "Invalid JSON");
}
}
private static void writePong(WebSocketWriter out) {
out.tryWrite("{\"type\":\"pong\"}");
}
private static void writeConnectionAck(WebSocketWriter out) {
out.tryWrite("{\"type\":\"connection_ack\"}");
}
private static void writeNext(WebSocketWriter out, String operationId, ExecutionResult executionResult)
throws JsonProcessingException {
final Map<String, Object> response = ImmutableMap.of(
"id", operationId,
"type", "next",
"payload", executionResult.toSpecification());
final String event = serializeToJson(response);
logger.trace("NEXT: {}", event);
out.tryWrite(event);
}
private static void writeError(WebSocketWriter out, String operationId, List<GraphQLError> errors)
throws JsonProcessingException {
final List<Map<String, Object>> errorSpecifications =
errors.stream().map(GraphQLError::toSpecification).collect(Collectors.toList());
final Map<String, Object> errorResponse = ImmutableMap.of(
"type", "error",
"id", operationId,
"payload", errorSpecifications);
final String event = serializeToJson(errorResponse);
logger.trace("ERROR: {}", event);
out.tryWrite(event);
}
private static void writeError(WebSocketWriter out, String operationId, Throwable t) {
final Map<String, Object> errorResponse = ImmutableMap.of(
"type", "error",
"id", operationId,
"payload", ImmutableList.of(
new GraphQLError() {
@Override
public String getMessage() {
return t.getMessage();
}
@Override
public List<SourceLocation> getLocations() {
return emptyList();
}
@Override
public ErrorClassification getErrorType() {
return ErrorType.DataFetchingException;
}
}.toSpecification()
));
try {
final String event = serializeToJson(errorResponse);
logger.trace("ERROR: {}", event);
out.tryWrite(event);
} catch (JsonProcessingException e) {
logger.warn("Error serializing error event", e);
out.close(e);
}
}
private static void writeComplete(WebSocketWriter out, String operationId) {
out.tryWrite("{\"type\":\"complete\",\"id\":\"" + operationId + "\"}");
}
private static final class GraphqlWebSocketCloseException extends Exception {
private static final long serialVersionUID = 1196626539261081709L;
private final WebSocketCloseStatus webSocketCloseStatus;
GraphqlWebSocketCloseException(int code, String reason) {
webSocketCloseStatus = WebSocketCloseStatus.ofPrivateUse(code, reason);
}
WebSocketCloseStatus getWebSocketCloseStatus() {
return webSocketCloseStatus;
}
@Override
public Throwable fillInStackTrace() {
return this;
}
}
}