4444import com .fasterxml .jackson .databind .ObjectMapper ;
4545import com .introproventures .graphql .jpa .query .schema .GraphQLExecutor ;
4646import com .introproventures .graphql .jpa .query .schema .impl .GraphQLJpaExecutor ;
47-
4847import graphql .DeferredExecutionResult ;
4948import graphql .ExecutionResult ;
5049import graphql .GraphQL ;
5655 *
5756 */
5857@ RestController
59- @ Transactional ( readOnly = true )
58+ @ Transactional
6059public class GraphQLController {
6160
6261 private static final String PATH = "${spring.graphql.jpa.query.path:/graphql}" ;
@@ -76,7 +75,7 @@ public GraphQLController(GraphQLExecutor graphQLExecutor, ObjectMapper mapper) {
7675 this .graphQLExecutor = graphQLExecutor ;
7776 this .mapper = mapper ;
7877 }
79-
78+
8079 @ GetMapping (value = PATH ,
8180 consumes = MediaType .TEXT_EVENT_STREAM_VALUE ,
8281 produces = MediaType .TEXT_EVENT_STREAM_VALUE )
@@ -85,18 +84,18 @@ public SseEmitter getEventStream(@RequestParam(name = "query") final String quer
8584 Map <String , Object > variablesMap = variablesStringToMap (variables );
8685
8786 ExecutionResult executionResult = graphQLExecutor .execute (query , variablesMap );
88-
87+
8988 SseEmitter sseEmitter = new SseEmitter (180_000L ); // FIXME need to add parameter
9089 sseEmitter .onTimeout (sseEmitter ::complete );
91-
90+
9291 if (!executionResult .getErrors ().isEmpty ()) {
9392 sseEmitter .send (executionResult .toSpecification (), MediaType .APPLICATION_JSON );
9493 sseEmitter .completeWithError (new RuntimeException (executionResult .getErrors ().toString ()));
9594 return sseEmitter ;
9695 }
97-
98- Publisher <ExecutionResult > deferredResults = executionResult .getData ();
99-
96+
97+ Publisher <ExecutionResult > deferredResults = executionResult .getData ();
98+
10099 // now send each deferred part which is given to us as a reactive stream
101100 // of deferred values
102101 deferredResults .subscribe (new Subscriber <ExecutionResult >() {
@@ -115,7 +114,7 @@ public void onNext(ExecutionResult executionResult) {
115114
116115 try {
117116 SseEventBuilder event = wrap (executionResult );
118-
117+
119118 sseEmitter .send (event );
120119 } catch (IOException e ) {
121120 sseEmitter .completeWithError (e );
@@ -131,21 +130,21 @@ public void onError(Throwable t) {
131130 public void onComplete () {
132131 sseEmitter .complete ();
133132 }
134-
133+
135134 SseEventBuilder wrap (ExecutionResult executionResult ) {
136135 Map <String , Object > result = executionResult .getData ();
137136 String name = result .keySet ().iterator ().next ();
138-
137+
139138 return SseEmitter .event ()
140139 .id ((id ++).toString ())
141140 .name (name )
142141 .data (result , MediaType .APPLICATION_JSON );
143-
142+
144143 }
145- });
146-
144+ });
145+
147146 return sseEmitter ;
148- }
147+ }
149148
150149 /**
151150 * Handle standard GraphQL POST request that consumes
@@ -191,11 +190,11 @@ public void postJson(@RequestBody @Valid final GraphQLQueryRequest queryRequest,
191190 public void getQuery (@ RequestParam (name = "query" ) final String query ,
192191 @ RequestParam (name = "variables" , required = false ) final String variables ,
193192 HttpServletResponse httpServletResponse ) throws IOException {
194-
193+
195194 Map <String , Object > variablesMap = variablesStringToMap (variables );
196195
197196 ExecutionResult executionResult = graphQLExecutor .execute (query , variablesMap );
198-
197+
199198 sendResponse (httpServletResponse , executionResult );
200199 }
201200
@@ -220,7 +219,7 @@ public void postForm(@RequestParam(name = "query") final String query,
220219 Map <String , Object > variablesMap = variablesStringToMap (variables );
221220
222221 ExecutionResult executionResult = graphQLExecutor .execute (query , variablesMap );
223-
222+
224223 sendResponse (httpServletResponse , executionResult );
225224 }
226225
@@ -312,11 +311,11 @@ public void setVariables(Map<String, Object> variables) {
312311 }
313312
314313 }
315-
314+
316315 private void sendResponse (HttpServletResponse response , ExecutionResult executionResult ) throws IOException {
317316 if (hasDeferredResults (executionResult )) {
318317 sendDeferredResponse (response , executionResult , executionResult .getExtensions ());
319- }
318+ }
320319 else if (hasPublisherResults (executionResult )) {
321320 sendMultipartResponse (response , executionResult , executionResult .getData ());
322321 } else {
@@ -328,8 +327,8 @@ private void sendNormalResponse(HttpServletResponse response, ExecutionResult ex
328327 response .setContentType ("application/json" );
329328 response .setStatus (HttpServletResponse .SC_OK );
330329 mapper .writeValue (response .getOutputStream (), executionResult .toSpecification ());
331- }
332-
330+ }
331+
333332 private boolean hasDeferredResults (ExecutionResult executionResult ) {
334333 return Optional .ofNullable (executionResult .getExtensions ())
335334 .map (it -> it .containsKey (GraphQL .DEFERRED_RESULTS ))
@@ -339,12 +338,12 @@ private boolean hasDeferredResults(ExecutionResult executionResult) {
339338 private boolean hasPublisherResults (ExecutionResult executionResult ) {
340339 return Publisher .class .isInstance (executionResult .getData ());
341340 }
342-
341+
343342 private static final String CRLF = "\r \n " ;
344343
345344 @ SuppressWarnings ("unchecked" )
346- private void sendDeferredResponse (HttpServletResponse response ,
347- ExecutionResult executionResult ,
345+ private void sendDeferredResponse (HttpServletResponse response ,
346+ ExecutionResult executionResult ,
348347 Map <Object , Object > extensions ) {
349348 Publisher <DeferredExecutionResult > deferredResults = (Publisher <DeferredExecutionResult >) extensions .get (GraphQL .DEFERRED_RESULTS );
350349 try {
@@ -354,8 +353,8 @@ private void sendDeferredResponse(HttpServletResponse response,
354353 }
355354 }
356355
357- private void sendMultipartResponse (HttpServletResponse response ,
358- ExecutionResult executionResult ,
356+ private void sendMultipartResponse (HttpServletResponse response ,
357+ ExecutionResult executionResult ,
359358 Publisher <? extends ExecutionResult > deferredResults ) {
360359 // this implements this Apollo defer spec: https://github.com/apollographql/apollo-server/blob/defer-support/docs/source/defer-support.md
361360 // the spec says CRLF + "-----" + CRLF is needed at the end, but it works without it and with it we get client
@@ -445,6 +444,6 @@ private String bodyToString() {
445444 throw new RuntimeException (e );
446445 }
447446 }
448- }
447+ }
449448
450449}
0 commit comments