|
| 1 | +package graphql.servlet; |
| 2 | + |
| 3 | +import graphql.ExecutionInput; |
| 4 | +import graphql.ExecutionResult; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.Writer; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.Iterator; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class TestBatchInputHandlerFactory implements GraphQLExecutionResultHandlerFactory { |
| 13 | + @Override |
| 14 | + public ExecutionResultHandler getBatchHandler(Writer respWriter, GraphQLObjectMapper graphQLObjectMapper) { |
| 15 | + return new LimitedBatchSizeHandler(respWriter, graphQLObjectMapper); |
| 16 | + } |
| 17 | + |
| 18 | + private class LimitedBatchSizeHandler implements ExecutionResultHandler { |
| 19 | + |
| 20 | + Writer writer; |
| 21 | + GraphQLObjectMapper mapper; |
| 22 | + List<ExecutionResult> results = new ArrayList<>(); |
| 23 | + |
| 24 | + private LimitedBatchSizeHandler(Writer respWriter, GraphQLObjectMapper graphQLObjectMapper) { |
| 25 | + this.mapper = graphQLObjectMapper; |
| 26 | + this.writer = respWriter; |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void finalizeResults() { |
| 31 | + try { |
| 32 | + writer.write("["); |
| 33 | + Iterator<ExecutionResult> iter = results.iterator(); |
| 34 | + while (iter.hasNext()) { |
| 35 | + writer.write(mapper.serializeResultAsJson(iter.next())); |
| 36 | + if (iter.hasNext()) { |
| 37 | + writer.write(","); |
| 38 | + } |
| 39 | + } |
| 40 | + writer.write("]"); |
| 41 | + } catch (IOException e) { |
| 42 | + throw new RuntimeException(e); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void accept(ExecutionResult executionResult) { |
| 48 | + results.add(executionResult); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public boolean shouldContinue(Iterator<ExecutionInput> executionInputIterator) { |
| 53 | + return results.size() < 2 && executionInputIterator.hasNext(); |
| 54 | + } |
| 55 | + } |
| 56 | +} |
0 commit comments