|
3 | 3 | import graphql.ExecutionInput; |
4 | 4 | import graphql.ExecutionResult; |
5 | 5 |
|
| 6 | +import javax.servlet.http.HttpServletResponse; |
6 | 7 | import java.io.IOException; |
7 | 8 | import java.io.Writer; |
8 | 9 | import java.util.Iterator; |
|
12 | 13 |
|
13 | 14 | public class TestBatchExecutionHandler implements BatchExecutionHandler { |
14 | 15 |
|
| 16 | + public static String BATCH_ERROR_MESSAGE = "Batch limit exceeded"; |
| 17 | + |
15 | 18 | @Override |
16 | | - public void handleBatch(GraphQLBatchedInvocationInput batchedInvocationInput, Writer writer, GraphQLObjectMapper graphQLObjectMapper, |
| 19 | + public void handleBatch(GraphQLBatchedInvocationInput batchedInvocationInput, HttpServletResponse response, GraphQLObjectMapper graphQLObjectMapper, |
17 | 20 | BiFunction<GraphQLInvocationInput, ExecutionInput, ExecutionResult> queryFunction) { |
18 | | - List<ExecutionResult> results = batchedInvocationInput.getExecutionInputs().parallelStream() |
19 | | - .limit(2) |
| 21 | + List<ExecutionInput> inputs = batchedInvocationInput.getExecutionInputs(); |
| 22 | + if (inputs.size() > 2) { |
| 23 | + handleBadInput(response); |
| 24 | + } |
| 25 | + List<ExecutionResult> results = inputs.parallelStream() |
20 | 26 | .map(input -> queryFunction.apply(batchedInvocationInput, input)) |
21 | 27 | .collect(Collectors.toList()); |
22 | | - writeResults(results, writer, graphQLObjectMapper); |
| 28 | + writeResults(results, response, graphQLObjectMapper); |
| 29 | + } |
| 30 | + |
| 31 | + private void handleBadInput(HttpServletResponse response) { |
| 32 | + try { |
| 33 | + response.sendError(HttpServletResponse.SC_BAD_REQUEST, BATCH_ERROR_MESSAGE); |
| 34 | + } catch (IOException e) { |
| 35 | + throw new RuntimeException(e); |
| 36 | + } |
23 | 37 | } |
24 | 38 |
|
25 | | - private void writeResults(List<ExecutionResult> results, Writer writer, GraphQLObjectMapper mapper) { |
| 39 | + private void writeResults(List<ExecutionResult> results, HttpServletResponse response, GraphQLObjectMapper mapper) { |
| 40 | + response.setContentType(AbstractGraphQLHttpServlet.APPLICATION_JSON_UTF8); |
| 41 | + response.setStatus(AbstractGraphQLHttpServlet.STATUS_OK); |
26 | 42 | try { |
| 43 | + Writer writer = response.getWriter(); |
27 | 44 | writer.write("["); |
28 | 45 | Iterator<ExecutionResult> iter = results.iterator(); |
29 | 46 | while (iter.hasNext()) { |
|
0 commit comments