Skip to content

Commit e60bc5d

Browse files
committed
Merge branch 'master' into query_invoker_refactor
2 parents 894476f + 207aa35 commit e60bc5d

File tree

8 files changed

+60
-27
lines changed

8 files changed

+60
-27
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ The servlet supports the following request formats:
101101
* operationName (optional)
102102
* variables (optional)
103103
* POST multipart parts named "query", "operationName" (optional), and "variables" (optional)
104+
* POST with Content Type "application/graphql" will treat the HTTP POST body contents as the GraphQL query string
104105

105106
## Servlet Listeners
106107

build.gradle

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ plugins {
1414
}
1515

1616
apply plugin: 'java'
17-
sourceCompatibility = '1.8'
18-
targetCompatibility = '1.8'
17+
sourceCompatibility = 1.8
18+
targetCompatibility = 1.8
1919

2020
// Tests
2121
apply plugin: 'groovy'
@@ -202,7 +202,11 @@ artifactory {
202202

203203
idea {
204204
project {
205-
languageLevel = '1.8'
205+
languageLevel = '11'
206206
vcs = 'Git'
207207
}
208208
}
209+
210+
wrapper {
211+
gradleVersion = '4.10.3'
212+
}

gradle/wrapper/gradle-wrapper.jar

1.8 KB
Binary file not shown.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Apr 25 17:03:16 EDT 2018
1+
#Thu Jun 20 12:32:36 CEST 2019
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-3.5-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-4.10.3-all.zip

src/main/java/graphql/servlet/AbstractGraphQLHttpServlet.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
import org.slf4j.Logger;
2525
import org.slf4j.LoggerFactory;
2626

27-
import javax.servlet.*;
27+
import javax.servlet.AsyncContext;
28+
import javax.servlet.AsyncEvent;
29+
import javax.servlet.AsyncListener;
30+
import javax.servlet.Servlet;
31+
import javax.servlet.ServletException;
2832
import javax.servlet.http.HttpServlet;
2933
import javax.servlet.http.HttpServletRequest;
3034
import javax.servlet.http.HttpServletResponse;
@@ -66,7 +70,7 @@ public abstract class AbstractGraphQLHttpServlet extends HttpServlet implements
6670
private static final String[] MULTIPART_KEYS = new String[]{"operations", "graphql", "query"};
6771

6872
private GraphQLConfiguration configuration;
69-
73+
7074
/**
7175
* @deprecated override {@link #getConfiguration()} instead
7276
*/
@@ -285,11 +289,19 @@ private void mapMultipartVariables(GraphQLRequest request,
285289
}
286290

287291
public void addListener(GraphQLServletListener servletListener) {
288-
configuration.add(servletListener);
292+
if (configuration != null) {
293+
configuration.add(servletListener);
294+
} else {
295+
listeners.add(servletListener);
296+
}
289297
}
290298

291299
public void removeListener(GraphQLServletListener servletListener) {
292-
configuration.remove(servletListener);
300+
if (configuration != null) {
301+
configuration.remove(servletListener);
302+
} else {
303+
listeners.remove(servletListener);
304+
}
293305
}
294306

295307
@Override
@@ -343,11 +355,13 @@ private void doRequest(HttpServletRequest request, HttpServletResponse response,
343355

344356
@Override
345357
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
358+
init();
346359
doRequestAsync(req, resp, getHandler);
347360
}
348361

349362
@Override
350363
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
364+
init();
351365
doRequestAsync(req, resp, postHandler);
352366
}
353367

@@ -498,23 +512,28 @@ default void accept(HttpServletRequest request, HttpServletResponse response) {
498512

499513
private static class SubscriptionAsyncListener implements AsyncListener {
500514
private final AtomicReference<Subscription> subscriptionRef;
515+
501516
public SubscriptionAsyncListener(AtomicReference<Subscription> subscriptionRef) {
502517
this.subscriptionRef = subscriptionRef;
503518
}
504519

505-
@Override public void onComplete(AsyncEvent event) {
520+
@Override
521+
public void onComplete(AsyncEvent event) {
506522
subscriptionRef.get().cancel();
507523
}
508524

509-
@Override public void onTimeout(AsyncEvent event) {
525+
@Override
526+
public void onTimeout(AsyncEvent event) {
510527
subscriptionRef.get().cancel();
511528
}
512529

513-
@Override public void onError(AsyncEvent event) {
530+
@Override
531+
public void onError(AsyncEvent event) {
514532
subscriptionRef.get().cancel();
515533
}
516534

517-
@Override public void onStartAsync(AsyncEvent event) {
535+
@Override
536+
public void onStartAsync(AsyncEvent event) {
518537
}
519538
}
520539

src/main/java/graphql/servlet/core/DefaultGraphQLErrorHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected boolean isClientError(GraphQLError error) {
5353
if (error instanceof ExceptionWhileDataFetching) {
5454
return ((ExceptionWhileDataFetching) error).getException() instanceof GraphQLError;
5555
}
56-
return !(error instanceof Throwable);
56+
return true;
5757
}
5858

5959
private GraphQLError replaceNonNullableFieldWasNullError(GraphQLError error) {

src/main/java/graphql/servlet/core/GraphQLObjectMapper.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@
2323
import java.util.Map;
2424
import java.util.function.Supplier;
2525

26+
import static java.util.stream.Collectors.toList;
27+
2628
/**
2729
* @author Andrew Potter
2830
*/
2931
public class GraphQLObjectMapper {
3032
private static final TypeReference<Map<String, List<String>>>
31-
MULTIPART_MAP_TYPE_REFERENCE = new TypeReference<Map<String, List<String>>>() {
32-
};
33+
MULTIPART_MAP_TYPE_REFERENCE = new TypeReference<Map<String, List<String>>>() {
34+
};
3335
private final ObjectMapperProvider objectMapperProvider;
3436
private final Supplier<GraphQLErrorHandler> graphQLErrorHandlerSupplier;
3537

@@ -44,7 +46,7 @@ protected GraphQLObjectMapper(ObjectMapperProvider objectMapperProvider, Supplie
4446
public ObjectMapper getJacksonMapper() {
4547
ObjectMapper result = mapper;
4648
if (result == null) { // First check (no locking)
47-
synchronized(this) {
49+
synchronized (this) {
4850
result = mapper;
4951
if (result == null) { // Second check (with locking)
5052
mapper = result = objectMapperProvider.provide();
@@ -110,7 +112,7 @@ public ExecutionResult sanitizeErrors(ExecutionResult executionResult) {
110112
List<GraphQLError> errors = executionResult.getErrors();
111113

112114
GraphQLErrorHandler errorHandler = graphQLErrorHandlerSupplier.get();
113-
if(errorHandler.errorsPresent(errors)) {
115+
if (errorHandler.errorsPresent(errors)) {
114116
errors = errorHandler.processErrors(errors);
115117
} else {
116118
errors = null;
@@ -131,14 +133,14 @@ public Map<String, Object> convertSanitizedExecutionResult(ExecutionResult execu
131133
final Map<String, Object> result = new LinkedHashMap<>();
132134

133135
if (areErrorsPresent(executionResult)) {
134-
result.put("errors", executionResult.getErrors());
136+
result.put("errors", executionResult.getErrors().stream().map(GraphQLError::toSpecification).collect(toList()));
135137
}
136138

137-
if(executionResult.getExtensions() != null && !executionResult.getExtensions().isEmpty()){
139+
if (executionResult.getExtensions() != null && !executionResult.getExtensions().isEmpty()) {
138140
result.put("extensions", executionResult.getExtensions());
139141
}
140142

141-
if(includeData) {
143+
if (includeData) {
142144
result.put("data", executionResult.getData());
143145
}
144146

@@ -153,7 +155,7 @@ public Map<String, Object> deserializeVariables(String variables) {
153155
}
154156
}
155157

156-
public Map<String,List<String>> deserializeMultipartMap(Part part) {
158+
public Map<String, List<String>> deserializeMultipartMap(Part part) {
157159
try {
158160
return getJacksonMapper().readValue(part.getInputStream(), MULTIPART_MAP_TYPE_REFERENCE);
159161
} catch (IOException e) {

src/main/java/graphql/servlet/core/internal/FallbackSubscriptionProtocolHandler.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package graphql.servlet.core.internal;
22

3+
import graphql.servlet.GraphQLSingleInvocationInput;
4+
35
import javax.websocket.Session;
46
import javax.websocket.server.HandshakeRequest;
57
import java.util.UUID;
@@ -19,18 +21,23 @@ public FallbackSubscriptionProtocolHandler(SubscriptionHandlerInput subscription
1921

2022
@Override
2123
public void onMessage(HandshakeRequest request, Session session, WsSessionSubscriptions subscriptions, String text) throws Exception {
24+
GraphQLSingleInvocationInput graphQLSingleInvocationInput = createInvocationInput(session, text);
2225
subscribe(
2326
session,
24-
input.getQueryInvoker().query(
25-
input.getInvocationInputFactory().create(
26-
input.getGraphQLObjectMapper().readGraphQLRequest(text)
27-
)
28-
),
27+
input.getQueryInvoker().query(graphQLSingleInvocationInput),
2928
subscriptions,
3029
UUID.randomUUID().toString()
3130
);
3231
}
3332

33+
private GraphQLSingleInvocationInput createInvocationInput(Session session, String text) throws IOException {
34+
GraphQLRequest graphQLRequest = input.getGraphQLObjectMapper().readGraphQLRequest(text);
35+
HandshakeRequest handshakeRequest = (HandshakeRequest) session.getUserProperties()
36+
.get(HandshakeRequest.class.getName());
37+
38+
return input.getInvocationInputFactory().create(graphQLRequest, session, handshakeRequest);
39+
}
40+
3441
@Override
3542
protected void sendDataMessage(Session session, String id, Object payload) {
3643
sender.send(session, payload);

0 commit comments

Comments
 (0)