graphql.servlet.DefaultBatchExecutionHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphql-java-servlet Show documentation
Show all versions of graphql-java-servlet Show documentation
relay.js-compatible GraphQL servlet
package graphql.servlet;
import graphql.ExecutionInput;
import graphql.ExecutionResult;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.Iterator;
import java.util.function.BiFunction;
public class DefaultBatchExecutionHandler implements BatchExecutionHandler {
@Override
public void handleBatch(GraphQLBatchedInvocationInput batchedInvocationInput, HttpServletResponse response, GraphQLObjectMapper graphQLObjectMapper,
BiFunction queryFunction) {
response.setContentType(AbstractGraphQLHttpServlet.APPLICATION_JSON_UTF8);
response.setStatus(AbstractGraphQLHttpServlet.STATUS_OK);
try {
Writer writer = response.getWriter();
Iterator executionInputIterator = batchedInvocationInput.getExecutionInputs().iterator();
writer.write("[");
while (executionInputIterator.hasNext()) {
ExecutionResult result = queryFunction.apply(batchedInvocationInput, executionInputIterator.next());
writer.write(graphQLObjectMapper.serializeResultAsJson(result));
if (executionInputIterator.hasNext()) {
writer.write(",");
}
}
writer.write("]");
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}