Skip to content

Commit 5c5f295

Browse files
authored
Merge pull request #371 from donbeave/master
Added an ability to add headers before writing GraphQL response.
2 parents 7bb806a + 0949e51 commit 5c5f295

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

graphql-java-servlet/src/main/java/graphql/kickstart/servlet/HttpRequestInvokerImpl.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,10 @@ private CompletableFuture<Void> handle(
9191
return futureResult
9292
.thenApplyQueryResult()
9393
.thenAccept(
94-
it -> writeResultResponse(futureResult.getInvocationInput(), it, request, response))
94+
it -> {
95+
listenerHandler.beforeFlush();
96+
writeResultResponse(futureResult.getInvocationInput(), it, request, response);
97+
})
9598
.thenAccept(it -> listenerHandler.onSuccess())
9699
.exceptionally(
97100
t ->

graphql-java-servlet/src/main/java/graphql/kickstart/servlet/ListenerHandler.java

+4
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,10 @@ void runCallbacks(Consumer<RequestCallback> action) {
6060
});
6161
}
6262

63+
void beforeFlush() {
64+
runCallbacks(it -> it.beforeFlush(request, response));
65+
}
66+
6367
void onSuccess() {
6468
runCallbacks(it -> it.onSuccess(request, response));
6569
}

graphql-java-servlet/src/main/java/graphql/kickstart/servlet/core/GraphQLServletListener.java

+33
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,50 @@
66
/** @author Andrew Potter */
77
public interface GraphQLServletListener {
88

9+
/**
10+
* Called this method when the request started processing.
11+
* @param request http request
12+
* @param response http response
13+
* @return request callback or {@literal null}
14+
*/
915
default RequestCallback onRequest(HttpServletRequest request, HttpServletResponse response) {
1016
return null;
1117
}
1218

19+
/**
20+
* The callback which used to add additional listeners for GraphQL request execution.
21+
*/
1322
interface RequestCallback {
1423

24+
/**
25+
* Called right before the response will be written and flushed. Can be used for applying some
26+
* changes to the response object, like adding response headers.
27+
* @param request http request
28+
* @param response http response
29+
*/
30+
default void beforeFlush(HttpServletRequest request, HttpServletResponse response) {}
31+
32+
/**
33+
* Called when GraphQL invoked successfully and the response was written already.
34+
* @param request http request
35+
* @param response http response
36+
*/
1537
default void onSuccess(HttpServletRequest request, HttpServletResponse response) {}
1638

39+
/**
40+
* Called when GraphQL was failed and the response was written already.
41+
* @param request http request
42+
* @param response http response
43+
*/
1744
default void onError(
1845
HttpServletRequest request, HttpServletResponse response, Throwable throwable) {}
1946

47+
/**
48+
* Called finally once on both success and failed GraphQL invocation. The response is also
49+
* already written.
50+
* @param request http request
51+
* @param response http response
52+
*/
2053
default void onFinally(HttpServletRequest request, HttpServletResponse response) {}
2154
}
2255
}

0 commit comments

Comments
 (0)