Skip to content

Added an ability to add headers before writing GraphQL response. #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ private CompletableFuture<Void> handle(
return futureResult
.thenApplyQueryResult()
.thenAccept(
it -> writeResultResponse(futureResult.getInvocationInput(), it, request, response))
it -> {
listenerHandler.beforeFlush();
writeResultResponse(futureResult.getInvocationInput(), it, request, response);
})
.thenAccept(it -> listenerHandler.onSuccess())
.exceptionally(
t ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ void runCallbacks(Consumer<RequestCallback> action) {
});
}

void beforeFlush() {
runCallbacks(it -> it.beforeFlush(request, response));
}

void onSuccess() {
runCallbacks(it -> it.onSuccess(request, response));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,50 @@
/** @author Andrew Potter */
public interface GraphQLServletListener {

/**
* Called this method when the request started processing.
* @param request http request
* @param response http response
* @return request callback or {@literal null}
*/
default RequestCallback onRequest(HttpServletRequest request, HttpServletResponse response) {
return null;
}

/**
* The callback which used to add additional listeners for GraphQL request execution.
*/
interface RequestCallback {

/**
* Called right before the response will be written and flushed. Can be used for applying some
* changes to the response object, like adding response headers.
* @param request http request
* @param response http response
*/
default void beforeFlush(HttpServletRequest request, HttpServletResponse response) {}

/**
* Called when GraphQL invoked successfully and the response was written already.
* @param request http request
* @param response http response
*/
default void onSuccess(HttpServletRequest request, HttpServletResponse response) {}

/**
* Called when GraphQL was failed and the response was written already.
* @param request http request
* @param response http response
*/
default void onError(
HttpServletRequest request, HttpServletResponse response, Throwable throwable) {}

/**
* Called finally once on both success and failed GraphQL invocation. The response is also
* already written.
* @param request http request
* @param response http response
*/
default void onFinally(HttpServletRequest request, HttpServletResponse response) {}
}
}