Skip to content

Commit 1ea64dc

Browse files
committed
Add protected encode method to WebFlux AbstractGraphQlHttpHandler
This helps to encode directly when needed, e.g. for multipart stream with incremental delivery. See gh-959
1 parent f61dfc5 commit 1ea64dc

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

spring-graphql/src/main/java/org/springframework/graphql/server/webflux/AbstractGraphQlHttpHandler.java

+15-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919

2020
import java.util.List;
21+
import java.util.Map;
2122

2223
import org.apache.commons.logging.Log;
2324
import org.apache.commons.logging.LogFactory;
@@ -131,13 +132,24 @@ private static Mono<SerializableGraphQlRequest> applyApplicationGraphQlFallback(
131132
protected abstract Mono<ServerResponse> prepareResponse(ServerRequest request, WebGraphQlResponse response);
132133

133134
/**
134-
* Encode the GraphQL response if custom codecs were provided, or otherwise
135-
* return the result map.
135+
* Encode the GraphQL response if custom codecs were provided, or return the result map.
136136
* @param response the GraphQL response
137137
* @return the encoded response or the result map
138138
*/
139139
protected Object encodeResponseIfNecessary(WebGraphQlResponse response) {
140-
return (this.codecDelegate != null) ? this.codecDelegate.encode(response) : response.toMap();
140+
Map<String, Object> resultMap = response.toMap();
141+
return (this.codecDelegate != null) ? encode(resultMap) : resultMap;
142+
}
143+
144+
/**
145+
* Encode the result map.
146+
* <p>This method assumes that a {@link CodecConfigurer} has been provided.
147+
* @param resultMap the result to encode
148+
* @return the encoded result map
149+
*/
150+
protected DataBuffer encode(Map<String, Object> resultMap) {
151+
Assert.state(this.codecDelegate != null, "CodecConfigurer was not provided");
152+
return this.codecDelegate.encode(resultMap);
141153
}
142154

143155
}

spring-graphql/src/main/java/org/springframework/graphql/server/webflux/HttpCodecDelegate.java

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@
2626
import org.springframework.core.codec.Encoder;
2727
import org.springframework.core.io.buffer.DataBuffer;
2828
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
29-
import org.springframework.graphql.GraphQlResponse;
3029
import org.springframework.graphql.server.support.SerializableGraphQlRequest;
3130
import org.springframework.http.MediaType;
3231
import org.springframework.http.codec.CodecConfigurer;
@@ -77,9 +76,9 @@ private static Encoder<?> findJsonEncoder(CodecConfigurer configurer) {
7776

7877

7978
@SuppressWarnings("unchecked")
80-
DataBuffer encode(GraphQlResponse response) {
81-
return ((Encoder<Map<String, Object>>) this.encoder)
82-
.encodeValue(response.toMap(), DefaultDataBufferFactory.sharedInstance, RESPONSE_TYPE, MimeTypeUtils.APPLICATION_JSON, null);
79+
DataBuffer encode(Map<String, Object> resultMap) {
80+
return ((Encoder<Map<String, Object>>) this.encoder).encodeValue(
81+
resultMap, DefaultDataBufferFactory.sharedInstance, RESPONSE_TYPE, MimeTypeUtils.APPLICATION_JSON, null);
8382
}
8483

8584
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)