1- package graphql .servlet . core ;
1+ package graphql .servlet ;
22
33import com .google .common .io .ByteStreams ;
44import com .google .common .io .CharStreams ;
55import graphql .ExecutionResult ;
66import graphql .introspection .IntrospectionQuery ;
77import graphql .schema .GraphQLFieldDefinition ;
8- import graphql .servlet .GraphQLMBean ;
9- import graphql .servlet .GraphQLObjectMapper ;
10- import graphql .servlet .GraphQLQueryInvoker ;
11- import graphql .servlet .GraphQLServletListener ;
12- import graphql .servlet .input .ContextSetting ;
8+ import graphql .servlet .core .GraphQLMBean ;
9+ import graphql .servlet .core .GraphQLObjectMapper ;
10+ import graphql .servlet .core .GraphQLQueryInvoker ;
11+ import graphql .servlet .core .GraphQLServletListener ;
12+ import graphql .servlet .config .GraphQLConfiguration ;
13+ import graphql .servlet .context .ContextSetting ;
14+ import graphql .servlet .input .BatchInputPreProcessResult ;
15+ import graphql .servlet .input .BatchInputPreProcessor ;
1316import graphql .servlet .input .GraphQLBatchedInvocationInput ;
1417import graphql .servlet .input .GraphQLSingleInvocationInput ;
1518import graphql .servlet .input .GraphQLInvocationInputFactory ;
16- import graphql .servlet .internal .GraphQLRequest ;
17- import graphql .servlet .internal .VariableMapper ;
19+ import graphql .servlet .core . internal .GraphQLRequest ;
20+ import graphql .servlet .core . internal .VariableMapper ;
1821import org .reactivestreams .Publisher ;
1922import org .reactivestreams .Subscriber ;
2023import org .reactivestreams .Subscription ;
3437import java .util .ArrayList ;
3538import java .util .Arrays ;
3639import java .util .HashMap ;
40+ import java .util .Iterator ;
3741import java .util .List ;
3842import java .util .Map ;
3943import java .util .Objects ;
@@ -121,7 +125,6 @@ public void init() {
121125 GraphQLInvocationInputFactory invocationInputFactory = configuration .getInvocationInputFactory ();
122126 GraphQLObjectMapper graphQLObjectMapper = configuration .getObjectMapper ();
123127 GraphQLQueryInvoker queryInvoker = configuration .getQueryInvoker ();
124- ContextSetting contextSetting = configuration .getContextSetting ();
125128
126129 String path = request .getPathInfo ();
127130 if (path == null ) {
@@ -135,8 +138,10 @@ public void init() {
135138 if (query != null ) {
136139
137140 if (isBatchedQuery (query )) {
138- queryInvoker .query (invocationInputFactory .createReadOnly (contextSetting , graphQLObjectMapper .readBatchedGraphQLRequest (query ),
139- request , response ), contextSetting , request , response , graphQLObjectMapper );
141+ List <GraphQLRequest > requests = graphQLObjectMapper .readBatchedGraphQLRequest (query );
142+ GraphQLBatchedInvocationInput batchedInvocationInput =
143+ invocationInputFactory .createReadOnly (configuration .getContextSetting (), requests , request , response );
144+ queryBatched (queryInvoker , batchedInvocationInput , request , response , configuration );
140145 } else {
141146 final Map <String , Object > variables = new HashMap <>();
142147 if (request .getParameter ("variables" ) != null ) {
@@ -160,7 +165,6 @@ public void init() {
160165 GraphQLInvocationInputFactory invocationInputFactory = configuration .getInvocationInputFactory ();
161166 GraphQLObjectMapper graphQLObjectMapper = configuration .getObjectMapper ();
162167 GraphQLQueryInvoker queryInvoker = configuration .getQueryInvoker ();
163- ContextSetting contextSetting = configuration .getContextSetting ();
164168
165169 try {
166170 if (APPLICATION_GRAPHQL .equals (request .getContentType ())) {
@@ -194,11 +198,9 @@ public void init() {
194198 List <GraphQLRequest > graphQLRequests =
195199 graphQLObjectMapper .readBatchedGraphQLRequest (inputStream );
196200 variablesMap .ifPresent (map -> graphQLRequests .forEach (r -> mapMultipartVariables (r , map , fileItems )));
197- GraphQLBatchedInvocationInput invocationInput =
198- invocationInputFactory .create (contextSetting , graphQLRequests , request , response );
199- //TODO next line doesn't make sense anymore?
200- //invocationInput.getContext().setParts(fileItems);
201- queryInvoker .query (invocationInput , contextSetting , request , response , graphQLObjectMapper );
201+ GraphQLBatchedInvocationInput batchedInvocationInput = invocationInputFactory .create (configuration .getContextSetting (),
202+ graphQLRequests , request , response );
203+ queryBatched (queryInvoker , batchedInvocationInput , request , response , configuration );
202204 return ;
203205 } else {
204206 GraphQLRequest graphQLRequest ;
@@ -211,8 +213,6 @@ public void init() {
211213 variablesMap .ifPresent (m -> mapMultipartVariables (graphQLRequest , m , fileItems ));
212214 GraphQLSingleInvocationInput invocationInput =
213215 invocationInputFactory .create (graphQLRequest , request , response );
214- //TODO next part doesn't make sense
215- //invocationInput.getContext().setParts(fileItems);
216216 query (queryInvoker , graphQLObjectMapper , invocationInput , Optional .of (request ), response );
217217 return ;
218218 }
@@ -225,9 +225,10 @@ public void init() {
225225 InputStream inputStream = asMarkableInputStream (request .getInputStream ());
226226
227227 if (isBatchedQuery (inputStream )) {
228- queryInvoker .query (invocationInputFactory .create (contextSetting ,
229- graphQLObjectMapper .readBatchedGraphQLRequest (inputStream ), request , response ), contextSetting , request , response ,
230- graphQLObjectMapper );
228+ List <GraphQLRequest > requests = graphQLObjectMapper .readBatchedGraphQLRequest (inputStream );
229+ GraphQLBatchedInvocationInput batchedInvocationInput =
230+ invocationInputFactory .create (configuration .getContextSetting (), requests , request , response );
231+ queryBatched (queryInvoker , batchedInvocationInput , request , response , configuration );
231232 } else {
232233 query (queryInvoker , graphQLObjectMapper , invocationInputFactory .create (graphQLObjectMapper .readGraphQLRequest (inputStream ), request , response ), Optional .of (request ), response );
233234 }
@@ -385,6 +386,32 @@ private void query(GraphQLQueryInvoker queryInvoker, GraphQLObjectMapper graphQL
385386 }
386387 }
387388
389+ private void queryBatched (GraphQLQueryInvoker queryInvoker , GraphQLBatchedInvocationInput batchedInvocationInput , HttpServletRequest request ,
390+ HttpServletResponse response , GraphQLConfiguration configuration ) throws IOException {
391+ BatchInputPreProcessor batchInputPreProcessor = configuration .getBatchInputPreProcessor ();
392+ ContextSetting contextSetting = configuration .getContextSetting ();
393+ BatchInputPreProcessResult batchInputPreProcessResult = batchInputPreProcessor .preProcessBatch (batchedInvocationInput , request , response );
394+ if (batchInputPreProcessResult .isExecutable ()) {
395+ List <ExecutionResult > results = queryInvoker .query (batchInputPreProcessResult .getBatchedInvocationInput ().getExecutionInputs (),
396+ contextSetting );
397+ response .setContentType (AbstractGraphQLHttpServlet .APPLICATION_JSON_UTF8 );
398+ response .setStatus (AbstractGraphQLHttpServlet .STATUS_OK );
399+ Writer writer = response .getWriter ();
400+ Iterator <ExecutionResult > executionInputIterator = results .iterator ();
401+ writer .write ("[" );
402+ GraphQLObjectMapper graphQLObjectMapper = configuration .getObjectMapper ();
403+ while (executionInputIterator .hasNext ()) {
404+ writer .write (graphQLObjectMapper .serializeResultAsJson (executionInputIterator .next ()));
405+ if (executionInputIterator .hasNext ()) {
406+ writer .write ("," );
407+ }
408+ }
409+ writer .write ("]" );
410+ } else {
411+ response .sendError (batchInputPreProcessResult .getStatusCode (), batchInputPreProcessResult .getStatusMessage ());
412+ }
413+ }
414+
388415 private <R > List <R > runListeners (Function <? super GraphQLServletListener , R > action ) {
389416 return configuration .getListeners ().stream ()
390417 .map (listener -> {
0 commit comments