Skip to content

Commit 9613518

Browse files
committed
Refactor MapGraphQlResponse
MapGraphQlResponse is a simple wrapper around the response map exposing it as GraphQlResponse. It is now package private and made accessible through a static factory method on GraphQlTransport. See gh-10
1 parent 3c30376 commit 9613518

File tree

10 files changed

+34
-54
lines changed

10 files changed

+34
-54
lines changed

spring-graphql-test/src/main/java/org/springframework/graphql/test/tester/WebTestClientTransport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import org.springframework.graphql.GraphQlRequest;
2727
import org.springframework.graphql.GraphQlResponse;
2828
import org.springframework.graphql.client.GraphQlTransport;
29-
import org.springframework.graphql.support.MapGraphQlResponse;
3029
import org.springframework.http.MediaType;
3130
import org.springframework.test.web.reactive.server.WebTestClient;
3231
import org.springframework.util.Assert;
@@ -67,7 +66,7 @@ public Mono<GraphQlResponse> execute(GraphQlRequest request) {
6766
.getResponseBody();
6867

6968
responseMap = (responseMap != null ? responseMap : Collections.emptyMap());
70-
GraphQlResponse response = MapGraphQlResponse.forResponse(responseMap);
69+
GraphQlResponse response = GraphQlTransport.wrapResponseMap(responseMap);
7170
return Mono.just(response);
7271
}
7372

spring-graphql/src/main/java/org/springframework/graphql/client/DefaultClientGraphQlResponse.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
3232
import org.springframework.graphql.GraphQlRequest;
3333
import org.springframework.graphql.GraphQlResponse;
34-
import org.springframework.graphql.support.MapGraphQlResponse;
3534
import org.springframework.lang.Nullable;
3635
import org.springframework.util.MimeType;
3736
import org.springframework.util.MimeTypeUtils;

spring-graphql/src/main/java/org/springframework/graphql/client/GraphQlTransport.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.graphql.client;
1818

19+
import java.util.Map;
20+
1921
import reactor.core.publisher.Flux;
2022
import reactor.core.publisher.Mono;
2123

@@ -57,4 +59,12 @@ public interface GraphQlTransport {
5759
*/
5860
Flux<GraphQlResponse> executeSubscription(GraphQlRequest request);
5961

62+
63+
/**
64+
* Wrap the given response map and expose it as a {@link GraphQlResponse}.
65+
*/
66+
static GraphQlResponse wrapResponseMap(Map<String, Object> map) {
67+
return new MapGraphQlResponse(map);
68+
}
69+
6070
}

spring-graphql/src/main/java/org/springframework/graphql/client/HttpGraphQlTransport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import org.springframework.core.ParameterizedTypeReference;
2525
import org.springframework.graphql.GraphQlRequest;
2626
import org.springframework.graphql.GraphQlResponse;
27-
import org.springframework.graphql.support.MapGraphQlResponse;
2827
import org.springframework.http.MediaType;
2928
import org.springframework.util.Assert;
3029
import org.springframework.web.reactive.function.client.WebClient;
@@ -61,7 +60,7 @@ public Mono<GraphQlResponse> execute(GraphQlRequest request) {
6160
.bodyValue(request.toMap())
6261
.retrieve()
6362
.bodyToMono(MAP_TYPE)
64-
.map(MapGraphQlResponse::forResponse);
63+
.map(GraphQlTransport::wrapResponseMap);
6564
}
6665

6766
@Override

spring-graphql/src/main/java/org/springframework/graphql/support/MapGraphQlError.java renamed to spring-graphql/src/main/java/org/springframework/graphql/client/MapGraphQlError.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.graphql.support;
17+
package org.springframework.graphql.client;
1818

1919
import java.util.Collections;
2020
import java.util.List;
@@ -31,7 +31,7 @@
3131
import org.springframework.util.Assert;
3232

3333
/**
34-
* Implementation of {@link GraphQLError} backed by a {@link Map}.
34+
* {@link GraphQLError} that wraps a deserialized the GraphQL response map.
3535
*
3636
* @author Rossen Stoyanchev
3737
* @since 1.0.0

spring-graphql/src/main/java/org/springframework/graphql/support/MapGraphQlResponse.java renamed to spring-graphql/src/main/java/org/springframework/graphql/client/MapGraphQlResponse.java

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.graphql.support;
17+
package org.springframework.graphql.client;
1818

1919
import java.util.ArrayList;
2020
import java.util.Collections;
2121
import java.util.List;
2222
import java.util.Map;
2323

24-
import graphql.ExecutionResult;
2524
import graphql.GraphQLError;
2625

2726
import org.springframework.graphql.GraphQlResponse;
@@ -31,12 +30,12 @@
3130
import org.springframework.util.StringUtils;
3231

3332
/**
34-
* {@link GraphQlResponse} for client use that wraps the GraphQL response map.
33+
* {@link GraphQlResponse} that wraps a deserialized the GraphQL response map.
3534
*
3635
* @author Rossen Stoyanchev
3736
* @since 1.0.0
3837
*/
39-
public class MapGraphQlResponse implements GraphQlResponse {
38+
class MapGraphQlResponse implements GraphQlResponse {
4039

4140
/**
4241
* Returned from {@link #getFieldValue(List)} to indicate a value does not exist.
@@ -49,7 +48,7 @@ public class MapGraphQlResponse implements GraphQlResponse {
4948
private final List<GraphQLError> errors;
5049

5150

52-
protected MapGraphQlResponse(Map<String, Object> responseMap) {
51+
MapGraphQlResponse(Map<String, Object> responseMap) {
5352
Assert.notNull(responseMap, "'responseMap' is required");
5453
this.responseMap = responseMap;
5554
this.errors = wrapErrors(responseMap);
@@ -218,29 +217,4 @@ public String toString() {
218217
return this.responseMap.toString();
219218
}
220219

221-
222-
/**
223-
* Create an instance from an {@code ExecutionResult} serialized to map via
224-
* {@link ExecutionResult#toSpecification()}.
225-
*/
226-
public static MapGraphQlResponse forResponse(Map<String, Object> map) {
227-
return new MapGraphQlResponse(map);
228-
}
229-
230-
/**
231-
* Create an {@code ExecutionResult} with a "data" key that returns the
232-
* given map.
233-
*/
234-
public static MapGraphQlResponse forDataOnly(@Nullable Map<String, Object> map) {
235-
return new MapGraphQlResponse(Collections.singletonMap("data", map));
236-
}
237-
238-
/**
239-
* Create an {@code ExecutionResult} with an "errors" key that returns the
240-
* given serialized errors.
241-
*/
242-
public static MapGraphQlResponse forErrorsOnly(List<Map<String, Object>> errors) {
243-
return new MapGraphQlResponse(Collections.singletonMap("errors", errors));
244-
}
245-
246220
}

spring-graphql/src/main/java/org/springframework/graphql/client/WebSocketGraphQlTransport.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
import org.springframework.graphql.GraphQlRequest;
3636
import org.springframework.graphql.GraphQlResponse;
37-
import org.springframework.graphql.support.MapGraphQlResponse;
3837
import org.springframework.graphql.web.support.GraphQlMessage;
3938
import org.springframework.graphql.web.support.GraphQlMessageType;
4039
import org.springframework.http.HttpHeaders;
@@ -477,7 +476,7 @@ public void handleNext(GraphQlMessage message) {
477476
}
478477

479478
Map<String, Object> responseMap = message.getPayload();
480-
GraphQlResponse graphQlResponse = MapGraphQlResponse.forResponse(responseMap);
479+
GraphQlResponse graphQlResponse = GraphQlTransport.wrapResponseMap(responseMap);
481480

482481
Sinks.EmitResult emitResult = (responseState != null ?
483482
responseState.sink().tryEmitValue(graphQlResponse) :
@@ -508,7 +507,7 @@ public void handleError(GraphQlMessage message) {
508507
}
509508

510509
List<Map<String, Object>> errorList = message.getPayload();
511-
GraphQlResponse response = MapGraphQlResponse.forErrorsOnly(errorList);
510+
GraphQlResponse response = GraphQlTransport.wrapResponseMap(Collections.singletonMap("errors", errorList));
512511

513512
Sinks.EmitResult emitResult;
514513
if (responseState != null) {

spring-graphql/src/test/java/org/springframework/graphql/client/GraphQlClientTestSupport.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import reactor.core.publisher.Mono;
3030

3131
import org.springframework.graphql.GraphQlRequest;
32-
import org.springframework.graphql.support.MapGraphQlResponse;
3332
import org.springframework.lang.Nullable;
3433
import org.springframework.util.ObjectUtils;
3534

@@ -96,7 +95,7 @@ protected void initResponse(GraphQlRequest request, @Nullable String responseDat
9695
Map<String, Object> responseMap = executionResult.toSpecification();
9796

9897
when(this.transport.execute(eq(request)))
99-
.thenReturn(Mono.just(MapGraphQlResponse.forResponse(responseMap)));
98+
.thenReturn(Mono.just(GraphQlTransport.wrapResponseMap(responseMap)));
10099
}
101100

102101
@SuppressWarnings("unchecked")

spring-graphql/src/test/java/org/springframework/graphql/support/MapGraphQlResponseTests.java renamed to spring-graphql/src/test/java/org/springframework/graphql/client/MapGraphQlResponseTests.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@
1414
* limitations under the License.
1515
*/
1616

17-
package org.springframework.graphql.support;
17+
package org.springframework.graphql.client;
1818

1919
import java.io.IOException;
20+
import java.util.Collections;
2021
import java.util.List;
2122
import java.util.Map;
2223
import java.util.stream.Collectors;
@@ -32,7 +33,6 @@
3233

3334
import static org.assertj.core.api.Assertions.assertThat;
3435
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
35-
import static org.springframework.graphql.support.MapGraphQlResponse.NO_VALUE;
3636

3737

3838
/**
@@ -81,12 +81,12 @@ void fieldValue() throws Exception {
8181

8282
// null "data"
8383
testFieldValue("", "null", null);
84-
testFieldValue("me", "null", NO_VALUE);
84+
testFieldValue("me", "null", MapGraphQlResponse.NO_VALUE);
8585

8686
// no such key or index
87-
testFieldValue("me", "{}", NO_VALUE); // "data" not null but no such key
88-
testFieldValue("me.friends", "{\"me\":{}}", NO_VALUE);
89-
testFieldValue("me.friends[0]", "{\"me\": {\"friends\": []}}", NO_VALUE);
87+
testFieldValue("me", "{}", MapGraphQlResponse.NO_VALUE); // "data" not null but no such key
88+
testFieldValue("me.friends", "{\"me\":{}}", MapGraphQlResponse.NO_VALUE);
89+
testFieldValue("me.friends[0]", "{\"me\": {\"friends\": []}}", MapGraphQlResponse.NO_VALUE);
9090

9191
// nest within map or list
9292
testFieldValue("me.name", "{\"me\":{\"name\":\"Luke\"}}", "Luke");
@@ -97,7 +97,7 @@ void fieldValue() throws Exception {
9797
private static void testFieldValue(String path, String json, @Nullable Object expected) throws IOException {
9898
List<Object> parsedPath = MapGraphQlResponse.parseFieldPath(path);
9999
Map<String, Object> map = mapper.readValue(json, Map.class);
100-
MapGraphQlResponse response = MapGraphQlResponse.forDataOnly(map);
100+
MapGraphQlResponse response = new MapGraphQlResponse(Collections.singletonMap("data", map));
101101
Object value = response.getFieldValue(parsedPath);
102102
if (expected != null) {
103103
assertThat(value).isEqualTo(expected);
@@ -119,7 +119,7 @@ void fieldValueInvalidPath() throws Exception {
119119
private static void testFieldValueInvalidPath(String path, String json) throws IOException {
120120
List<Object> parsedPath = MapGraphQlResponse.parseFieldPath(path);
121121
Map<String, Object> map = mapper.readValue(json, Map.class);
122-
MapGraphQlResponse response = MapGraphQlResponse.forDataOnly(map);
122+
MapGraphQlResponse response = new MapGraphQlResponse(Collections.singletonMap("data", map));
123123

124124
assertThatIllegalArgumentException().isThrownBy(() -> response.getFieldValue(parsedPath))
125125
.withMessage("Invalid path " + parsedPath + ", data: " + map);
@@ -139,7 +139,7 @@ void fieldErrors() {
139139
Stream.of(error0, error1, error2, error3)
140140
.map(GraphQLError::toSpecification).collect(Collectors.toList());
141141

142-
MapGraphQlResponse response = MapGraphQlResponse.forErrorsOnly(errorList);
142+
MapGraphQlResponse response = new MapGraphQlResponse(Collections.singletonMap("errors", errorList));
143143
List<GraphQLError> errors = response.getFieldErrors(path);
144144

145145
assertThat(errors).containsExactly(error1, error2, error3);

spring-graphql/src/test/java/org/springframework/graphql/client/MockWebSocketGraphQlTransportTests.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
import org.springframework.graphql.GraphQlRequest;
3636
import org.springframework.graphql.GraphQlResponse;
37-
import org.springframework.graphql.support.MapGraphQlResponse;
3837
import org.springframework.graphql.web.TestWebSocketClient;
3938
import org.springframework.graphql.web.TestWebSocketConnection;
4039
import org.springframework.graphql.web.support.GraphQlMessage;
@@ -70,9 +69,11 @@ public class MockWebSocketGraphQlTransportTests {
7069

7170
private final WebSocketGraphQlTransport transport = createTransport(this.webSocketClient);
7271

73-
private final GraphQlResponse response1 = MapGraphQlResponse.forDataOnly(Collections.singletonMap("key1", "value1"));
72+
private final GraphQlResponse response1 = GraphQlTransport.wrapResponseMap(
73+
Collections.singletonMap("data", Collections.singletonMap("key1", "value1")));
7474

75-
private final GraphQlResponse response2 = MapGraphQlResponse.forDataOnly(Collections.singletonMap("key2", "value2"));
75+
private final GraphQlResponse response2 = GraphQlTransport.wrapResponseMap(
76+
Collections.singletonMap("data", Collections.singletonMap("key2", "value2")));
7677

7778

7879
@Test

0 commit comments

Comments
 (0)