|
| 1 | +package graphql.spring.reactive; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 4 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 5 | +import graphql.DeferredExecutionResult; |
| 6 | +import graphql.ExecutionResult; |
| 7 | +import graphql.GraphQL; |
| 8 | +import org.reactivestreams.Publisher; |
| 9 | +import org.springframework.beans.factory.annotation.Autowired; |
| 10 | +import org.springframework.core.io.buffer.DataBuffer; |
| 11 | +import org.springframework.core.io.buffer.DefaultDataBufferFactory; |
| 12 | +import org.springframework.http.HttpHeaders; |
| 13 | +import org.springframework.http.HttpStatus; |
| 14 | +import org.springframework.http.server.reactive.ServerHttpResponse; |
| 15 | +import org.springframework.stereotype.Component; |
| 16 | +import reactor.core.publisher.Flux; |
| 17 | +import reactor.core.publisher.Mono; |
| 18 | + |
| 19 | +import java.nio.charset.StandardCharsets; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +@Component |
| 23 | +public class DefaultExecutionResultHandler implements ExecutionResultHandler { |
| 24 | + |
| 25 | + @Autowired |
| 26 | + ObjectMapper objectMapper; |
| 27 | + |
| 28 | + private static final String CRLF = "\r\n"; |
| 29 | + |
| 30 | + @Override |
| 31 | + public Object handleExecutionResult(Mono<ExecutionResult> executionResultMono, ServerHttpResponse serverHttpResponse) { |
| 32 | + return executionResultMono.map(executionResult -> handleImpl(executionResult, serverHttpResponse)); |
| 33 | + } |
| 34 | + |
| 35 | + private Object handleImpl(ExecutionResult executionResult, ServerHttpResponse serverHttpResponse) { |
| 36 | + Map<Object, Object> extensions = executionResult.getExtensions(); |
| 37 | + if (extensions != null && extensions.containsKey(GraphQL.DEFERRED_RESULTS)) { |
| 38 | + return handleDeferResponse(serverHttpResponse, executionResult, extensions); |
| 39 | + } else { |
| 40 | + return executionResult.toSpecification(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private Mono<Void> handleDeferResponse(ServerHttpResponse serverHttpResponse, |
| 45 | + ExecutionResult executionResult, |
| 46 | + Map<Object, Object> extensions) { |
| 47 | + Publisher<DeferredExecutionResult> deferredResults = (Publisher<DeferredExecutionResult>) extensions.get(GraphQL.DEFERRED_RESULTS); |
| 48 | + // this implements this apollo defer spec: https://github.com/apollographql/apollo-server/blob/defer-support/docs/source/defer-support.md |
| 49 | + // the spec says CRLF + "-----" + CRLF is needed at the end, but it works without it and with it we get client |
| 50 | + // side errors with it, so we skp it |
| 51 | + serverHttpResponse.setStatusCode(HttpStatus.OK); |
| 52 | + HttpHeaders headers = serverHttpResponse.getHeaders(); |
| 53 | + headers.set("Content-Type", "multipart/mixed; boundary=\"-\""); |
| 54 | + headers.set("Connection", "keep-alive"); |
| 55 | + |
| 56 | + Flux<Mono<DataBuffer>> deferredDataBuffers = Flux.from(deferredResults).map(deferredExecutionResult -> { |
| 57 | + DeferPart deferPart = new DeferPart(deferredExecutionResult.toSpecification()); |
| 58 | + StringBuilder builder = new StringBuilder(); |
| 59 | + String body = deferPart.write(); |
| 60 | + builder.append(CRLF).append("---").append(CRLF); |
| 61 | + builder.append(body); |
| 62 | + return strToDataBuffer(builder.toString()); |
| 63 | + }); |
| 64 | + Flux<Mono<DataBuffer>> firstResult = Flux.just(firstResult(executionResult)); |
| 65 | + |
| 66 | + |
| 67 | + return serverHttpResponse.writeAndFlushWith(Flux.mergeSequential(firstResult, deferredDataBuffers)); |
| 68 | + } |
| 69 | + |
| 70 | + private Mono<DataBuffer> firstResult(ExecutionResult executionResult) { |
| 71 | + StringBuilder builder = new StringBuilder(); |
| 72 | + builder.append(CRLF).append("---").append(CRLF); |
| 73 | + DeferPart deferPart = new DeferPart(executionResult.toSpecification()); |
| 74 | + String body = deferPart.write(); |
| 75 | + builder.append(body); |
| 76 | + Mono<DataBuffer> dataBufferMono = strToDataBuffer(body); |
| 77 | + return dataBufferMono; |
| 78 | + } |
| 79 | + |
| 80 | + private Mono<DataBuffer> strToDataBuffer(String string) { |
| 81 | + byte[] bytes = string.getBytes(StandardCharsets.UTF_8); |
| 82 | + DefaultDataBufferFactory defaultDataBufferFactory = new DefaultDataBufferFactory(); |
| 83 | + return Mono.just(defaultDataBufferFactory.wrap(bytes)); |
| 84 | + } |
| 85 | + |
| 86 | + private class DeferPart { |
| 87 | + |
| 88 | + private Object body; |
| 89 | + |
| 90 | + public DeferPart(Object data) { |
| 91 | + this.body = data; |
| 92 | + } |
| 93 | + |
| 94 | + public String write() { |
| 95 | + StringBuilder result = new StringBuilder(); |
| 96 | + String bodyString = bodyToString(); |
| 97 | + result.append("Content-Type: application/json").append(CRLF); |
| 98 | + result.append("Content-Length: ").append(bodyString.length()).append(CRLF).append(CRLF); |
| 99 | + result.append(bodyString); |
| 100 | + return result.toString(); |
| 101 | + } |
| 102 | + |
| 103 | + private String bodyToString() { |
| 104 | + try { |
| 105 | + return objectMapper.writeValueAsString(body); |
| 106 | + } catch (JsonProcessingException e) { |
| 107 | + throw new RuntimeException(e); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + } |
| 112 | +} |
0 commit comments