Skip to content

Serialize query response directly to Servlet's output stream to avoid intermediate String object creation #215

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
Nov 14, 2019
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
8 changes: 5 additions & 3 deletions src/main/java/graphql/servlet/AbstractGraphQLHttpServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ private void query(GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper graphQL
if (!(result.getData() instanceof Publisher || isDeferred)) {
resp.setContentType(APPLICATION_JSON_UTF8);
resp.setStatus(STATUS_OK);
resp.getWriter().write(graphQLObjectMapper.serializeResultAsJson(result));
graphQLObjectMapper.serializeResultAsJson(resp.getWriter(), result);
} else {
if (req == null) {
throw new IllegalStateException("Http servlet request can not be null");
Expand Down Expand Up @@ -414,7 +414,7 @@ private void queryBatched(GraphQLQueryInvoker queryInvoker, GraphQLBatchedInvoca
writer.write("[");
GraphQLObjectMapper graphQLObjectMapper = configuration.getObjectMapper();
while (executionInputIterator.hasNext()) {
writer.write(graphQLObjectMapper.serializeResultAsJson(executionInputIterator.next()));
graphQLObjectMapper.serializeResultAsJson(writer, executionInputIterator.next());
if (executionInputIterator.hasNext()) {
writer.write(",");
}
Expand Down Expand Up @@ -558,7 +558,9 @@ public void onSubscribe(Subscription subscription) {
public void onNext(ExecutionResult executionResult) {
try {
Writer writer = asyncContext.getResponse().getWriter();
writer.write("data: " + graphQLObjectMapper.serializeResultAsJson(executionResult) + "\n\n");
writer.write("data: ");
graphQLObjectMapper.serializeResultAsJson(writer, executionResult);
writer.write("\n\n");
writer.flush();
subscriptionRef.get().request(1);
} catch (IOException ignored) {
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/graphql/servlet/core/GraphQLObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
Expand Down Expand Up @@ -101,6 +102,10 @@ public String serializeResultAsJson(ExecutionResult executionResult) {
}
}

public void serializeResultAsJson(Writer writer, ExecutionResult executionResult) throws IOException {
getJacksonMapper().writeValue(writer, createResultFromExecutionResult(executionResult));
}

public boolean areErrorsPresent(ExecutionResult executionResult) {
return graphQLErrorHandlerSupplier.get().errorsPresent(executionResult.getErrors());
}
Expand Down