|
1 | 1 | package graphql.servlet.internal; |
2 | 2 |
|
| 3 | +import com.fasterxml.jackson.annotation.JsonCreator; |
| 4 | +import com.fasterxml.jackson.annotation.JsonInclude; |
| 5 | +import com.fasterxml.jackson.annotation.JsonValue; |
| 6 | +import graphql.ExecutionResult; |
| 7 | +import graphql.servlet.GraphQLObjectMapper; |
| 8 | +import org.reactivestreams.Publisher; |
| 9 | +import org.reactivestreams.Subscriber; |
| 10 | +import org.reactivestreams.Subscription; |
| 11 | +import org.slf4j.Logger; |
| 12 | +import org.slf4j.LoggerFactory; |
| 13 | + |
3 | 14 | import javax.websocket.Session; |
4 | 15 | import javax.websocket.server.HandshakeRequest; |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.concurrent.atomic.AtomicReference; |
5 | 20 |
|
6 | 21 | /** |
7 | 22 | * @author Andrew Potter |
8 | 23 | */ |
9 | 24 | public class ApolloSubscriptionProtocolHandler implements SubscriptionProtocolHandler { |
| 25 | + |
| 26 | + private static final Logger log = LoggerFactory.getLogger(ApolloSubscriptionProtocolHandler.class); |
| 27 | + |
| 28 | + private final SubscriptionHandlerInput input; |
| 29 | + |
| 30 | + public ApolloSubscriptionProtocolHandler(SubscriptionHandlerInput subscriptionHandlerInput) { |
| 31 | + this.input = subscriptionHandlerInput; |
| 32 | + } |
| 33 | + |
10 | 34 | @Override |
11 | 35 | public void onMessage(HandshakeRequest request, Session session, String text) { |
| 36 | + OperationMessage message; |
| 37 | + try { |
| 38 | + message = input.getGraphQLObjectMapper().getJacksonMapper().readValue(text, OperationMessage.class); |
| 39 | + } catch(Throwable t) { |
| 40 | + log.warn("Error parsing message", t); |
| 41 | + sendMessage(session, OperationMessage.Type.GQL_CONNECTION_ERROR, null); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + switch(message.getType()) { |
| 46 | + case GQL_CONNECTION_INIT: |
| 47 | + sendMessage(session, OperationMessage.Type.GQL_CONNECTION_ACK, message.getId()); |
| 48 | +// sendMessage(session, OperationMessage.Type.GQL_CONNECTION_KEEP_ALIVE, message.getId()); |
| 49 | + break; |
| 50 | + |
| 51 | + case GQL_START: |
| 52 | + handleSubscriptionStart( |
| 53 | + session, |
| 54 | + message.id, |
| 55 | + input.getQueryInvoker().query(input.getInvocationInputFactory().create( |
| 56 | + input.getGraphQLObjectMapper().getJacksonMapper().convertValue(message.payload, GraphQLRequest.class) |
| 57 | + )) |
| 58 | + ); |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @SuppressWarnings("unchecked") |
| 64 | + private void handleSubscriptionStart(Session session, String id, ExecutionResult executionResult) { |
| 65 | + executionResult = input.getGraphQLObjectMapper().sanitizeErrors(executionResult); |
| 66 | + OperationMessage.Type type = input.getGraphQLObjectMapper().areErrorsPresent(executionResult) ? OperationMessage.Type.GQL_ERROR : OperationMessage.Type.GQL_DATA; |
| 67 | + |
| 68 | + Object data = executionResult.getData(); |
| 69 | + if(data instanceof Publisher) { |
| 70 | + if(type == OperationMessage.Type.GQL_DATA) { |
| 71 | + AtomicReference<Subscription> subscriptionReference = new AtomicReference<>(); |
| 72 | + |
| 73 | + ((Publisher<ExecutionResult>) data).subscribe(new Subscriber<ExecutionResult>() { |
| 74 | + @Override |
| 75 | + public void onSubscribe(Subscription subscription) { |
| 76 | + subscriptionReference.set(subscription); |
| 77 | + subscriptionReference.get().request(1); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void onNext(ExecutionResult executionResult) { |
| 82 | + subscriptionReference.get().request(1); |
| 83 | + Map<String, Object> result = new HashMap<>(); |
| 84 | + result.put("data", executionResult.getData()); |
| 85 | + sendMessage(session, OperationMessage.Type.GQL_DATA, id, result); |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public void onError(Throwable throwable) { |
| 90 | + log.error("Subscription error", throwable); |
| 91 | + sendMessage(session, OperationMessage.Type.GQL_ERROR, id); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public void onComplete() { |
| 96 | + sendMessage(session, OperationMessage.Type.GQL_COMPLETE, id); |
| 97 | + } |
| 98 | + }); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + sendMessage(session, type, id, input.getGraphQLObjectMapper().convertSanitizedExecutionResult(executionResult)); |
| 103 | + } |
| 104 | + |
| 105 | + private void sendMessage(Session session, OperationMessage.Type type, String id) { |
| 106 | + sendMessage(session, type, id, null); |
| 107 | + } |
| 108 | + |
| 109 | + private void sendMessage(Session session, OperationMessage.Type type, String id, Object payload) { |
| 110 | + try { |
| 111 | + session.getBasicRemote().sendText(input.getGraphQLObjectMapper().getJacksonMapper().writeValueAsString( |
| 112 | + new OperationMessage(type, id, payload) |
| 113 | + )); |
| 114 | + } catch (IOException e) { |
| 115 | + throw new RuntimeException("Error sending subscription response", e); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @JsonInclude(JsonInclude.Include.NON_NULL) |
| 120 | + public static class OperationMessage { |
| 121 | + private Type type; |
| 122 | + private String id; |
| 123 | + private Object payload; |
| 124 | + |
| 125 | + public OperationMessage() { |
| 126 | + } |
| 127 | + |
| 128 | + public OperationMessage(Type type, String id, Object payload) { |
| 129 | + this.type = type; |
| 130 | + this.id = id; |
| 131 | + this.payload = payload; |
| 132 | + } |
| 133 | + |
| 134 | + public Type getType() { |
| 135 | + return type; |
| 136 | + } |
| 137 | + |
| 138 | + public String getId() { |
| 139 | + return id; |
| 140 | + } |
| 141 | + |
| 142 | + public Object getPayload() { |
| 143 | + return payload; |
| 144 | + } |
12 | 145 |
|
| 146 | + public enum Type { |
| 147 | + |
| 148 | + // Server Messages |
| 149 | + GQL_CONNECTION_ACK("connection_ack"), |
| 150 | + GQL_CONNECTION_ERROR("connection_error"), |
| 151 | + GQL_CONNECTION_KEEP_ALIVE("ka"), |
| 152 | + GQL_DATA("data"), |
| 153 | + GQL_ERROR("error"), |
| 154 | + GQL_COMPLETE("complete"), |
| 155 | + |
| 156 | + // Client Messages |
| 157 | + GQL_CONNECTION_INIT("connection_init"), |
| 158 | + GQL_CONNECTION_TERMINATE("connection_terminate"), |
| 159 | + GQL_START("start"), |
| 160 | + GQL_STOP("stop"); |
| 161 | + |
| 162 | + private static final Map<String, Type> reverseLookup = new HashMap<>(); |
| 163 | + |
| 164 | + static { |
| 165 | + for(Type type: Type.values()) { |
| 166 | + reverseLookup.put(type.getType(), type); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + private final String type; |
| 171 | + |
| 172 | + Type(String type) { |
| 173 | + this.type = type; |
| 174 | + } |
| 175 | + |
| 176 | + @JsonCreator |
| 177 | + public static Type findType(String type) { |
| 178 | + return reverseLookup.get(type); |
| 179 | + } |
| 180 | + |
| 181 | + @JsonValue |
| 182 | + public String getType() { |
| 183 | + return type; |
| 184 | + } |
| 185 | + } |
13 | 186 | } |
| 187 | + |
14 | 188 | } |
0 commit comments