add http headers for the response

This commit is contained in:
Andreas Marek
2020-09-15 03:29:15 +10:00
parent a7cf5b6d55
commit 7bc8cea8ed
2 changed files with 17 additions and 13 deletions

View File

@@ -65,19 +65,21 @@ public class GraphQLController {
serverRequest.headers().asHttpHeaders(),
serverRequest.queryParams());
return graphQLHandler.graphqlPOST(graphQLHttpRequest);
}).flatMap(graphQLResponseBody -> {
}).flatMap(graphQLHttpResponse -> {
//TODO: this should be handled better:
// we don't want to serialize `null` values for `errors` and `extensions`
// this is why we convert it to a Map here
Map<String, Object> responseBodyRaw = new LinkedHashMap<>();
responseBodyRaw.put("data", graphQLResponseBody.getData());
if (graphQLResponseBody.getErrors() != null) {
responseBodyRaw.put("errors", graphQLResponseBody.getErrors());
responseBodyRaw.put("data", graphQLHttpResponse.getData());
if (graphQLHttpResponse.getErrors() != null) {
responseBodyRaw.put("errors", graphQLHttpResponse.getErrors());
}
if (graphQLResponseBody.getExtensions() != null) {
responseBodyRaw.put("extensions", graphQLResponseBody.getExtensions());
if (graphQLHttpResponse.getExtensions() != null) {
responseBodyRaw.put("extensions", graphQLHttpResponse.getExtensions());
}
return ServerResponse.ok().bodyValue(responseBodyRaw);
return ServerResponse.ok().headers(httpHeaders -> {
httpHeaders.addAll(graphQLHttpResponse.getHttpHeaders());
}).bodyValue(responseBodyRaw);
});
}

View File

@@ -78,20 +78,22 @@ public class GraphQLController {
serverRequest.params());
Mono<Map<String, Object>> responseRawMono = graphQLHandler.graphqlPOST(graphQLHttpRequest)
.map(graphQLResponseBody -> {
.map(graphQLHttpResponse -> {
//TODO: this should be handled better:
// we don't want to serialize `null` values for `errors` and `extensions`
// this is why we convert it to a Map here
Map<String, Object> responseBodyRaw = new LinkedHashMap<>();
responseBodyRaw.put("data", graphQLResponseBody.getData());
if (graphQLResponseBody.getErrors() != null) {
responseBodyRaw.put("errors", graphQLResponseBody.getErrors());
responseBodyRaw.put("data", graphQLHttpResponse.getData());
if (graphQLHttpResponse.getErrors() != null) {
responseBodyRaw.put("errors", graphQLHttpResponse.getErrors());
}
if (graphQLResponseBody.getExtensions() != null) {
responseBodyRaw.put("extensions", graphQLResponseBody.getExtensions());
if (graphQLHttpResponse.getExtensions() != null) {
responseBodyRaw.put("extensions", graphQLHttpResponse.getExtensions());
}
return responseBodyRaw;
});
//TODO: how to add http headers for the response here? It is part of responseRawMono but
// ServerResponse don't accept a CompletableFuture or Mono as headers.
return ServerResponse.ok().body(responseRawMono);
}