Skip to content

Commit ed8c279

Browse files
committed
Fix cyclic dependencies and polishing
1 parent c2c6ae6 commit ed8c279

File tree

7 files changed

+81
-42
lines changed

7 files changed

+81
-42
lines changed

spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/RSocketGraphQlTesterBuilderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ private static class BuilderSetup {
126126
private Closeable server;
127127

128128
public BuilderSetup() {
129-
this.graphQlService.setDefaultDataAsJson("{}");
129+
this.graphQlService.setDefaultResponse("{}");
130130
}
131131

132132
public RSocketGraphQlTester.Builder<?> initBuilder() {

spring-graphql-test/src/test/java/org/springframework/graphql/test/tester/WebGraphQlTesterBuilderTests.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.http.codec.json.Jackson2JsonDecoder;
4646
import org.springframework.lang.Nullable;
4747
import org.springframework.test.web.reactive.server.WebTestClient;
48+
import org.springframework.util.Assert;
4849
import org.springframework.util.MimeType;
4950
import org.springframework.web.reactive.function.server.RouterFunction;
5051
import org.springframework.web.reactive.function.server.ServerResponse;
@@ -205,12 +206,13 @@ private interface TesterBuilderSetup {
205206

206207
private static class WebBuilderSetup implements TesterBuilderSetup {
207208

209+
@Nullable
208210
private WebGraphQlRequest request;
209211

210212
private final MockExecutionGraphQlService graphQlService = new MockExecutionGraphQlService();
211213

212214
public WebBuilderSetup() {
213-
this.graphQlService.setDefaultDataAsJson("{}");
215+
this.graphQlService.setDefaultResponse("{}");
214216
}
215217

216218
@Override
@@ -234,6 +236,7 @@ public void setMockResponse(String document, ExecutionResult result) {
234236

235237
@Override
236238
public WebGraphQlRequest getWebGraphQlRequest() {
239+
Assert.state(this.request != null, "No saved WebGraphQlRequest");
237240
return this.request;
238241
}
239242

spring-graphql/src/main/java/org/springframework/graphql/execution/ThreadLocalAccessor.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2022 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.
@@ -20,7 +20,6 @@
2020
import java.util.Map;
2121

2222
import org.springframework.beans.factory.ObjectProvider;
23-
import org.springframework.graphql.server.WebGraphQlHandler;
2423

2524
/**
2625
* Interface to be implemented to assist with the extraction of ThreadLocal
@@ -38,7 +37,7 @@
3837
*
3938
* @author Rossen Stoyanchev
4039
* @since 1.0.0
41-
* @see WebGraphQlHandler.Builder#threadLocalAccessor(ThreadLocalAccessor...)
40+
* @see org.springframework.graphql.server.WebGraphQlHandler.Builder#threadLocalAccessor(ThreadLocalAccessor...)
4241
*/
4342
public interface ThreadLocalAccessor {
4443

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

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@
1818

1919
import java.time.Duration;
2020

21+
import reactor.core.publisher.Flux;
22+
import reactor.core.publisher.Mono;
23+
24+
import org.springframework.graphql.ExecutionGraphQlService;
2125
import org.springframework.graphql.GraphQlRequest;
26+
import org.springframework.graphql.GraphQlResponse;
2227
import org.springframework.graphql.execution.MockExecutionGraphQlService;
28+
import org.springframework.graphql.support.DefaultExecutionGraphQlRequest;
2329

2430
/**
2531
* Base class for {@link GraphQlClient} tests.
@@ -32,7 +38,7 @@ public class GraphQlClientTestSupport {
3238

3339
private final MockExecutionGraphQlService graphQlService = new MockExecutionGraphQlService();
3440

35-
private final GraphQlClient.Builder<?> clientBuilder = GraphQlClient.builder(this.graphQlService.asGraphQlTransport());
41+
private final GraphQlClient.Builder<?> clientBuilder = GraphQlClient.builder(new MockTransport(this.graphQlService));
3642

3743
private final GraphQlClient client = this.clientBuilder.build();
3844

@@ -53,4 +59,33 @@ protected GraphQlRequest request() {
5359
return this.graphQlService.getGraphQlRequest();
5460
}
5561

62+
63+
private static class MockTransport implements GraphQlTransport {
64+
65+
private final ExecutionGraphQlService graphQlService;
66+
67+
private MockTransport(ExecutionGraphQlService graphQlService) {
68+
this.graphQlService = graphQlService;
69+
}
70+
71+
@Override
72+
public Mono<GraphQlResponse> execute(GraphQlRequest request) {
73+
return graphQlService.execute(
74+
new DefaultExecutionGraphQlRequest(
75+
request.getDocument(),
76+
request.getOperationName(),
77+
request.getVariables(),
78+
request.getExtensions(),
79+
"1",
80+
null))
81+
.cast(GraphQlResponse.class);
82+
}
83+
84+
@Override
85+
public Flux<GraphQlResponse> executeSubscription(GraphQlRequest request) {
86+
return Flux.error(new UnsupportedOperationException());
87+
}
88+
89+
}
90+
5691
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private static class BuilderSetup {
9494
private Closeable server;
9595

9696
public BuilderSetup() {
97-
this.graphQlService.setDefaultDataAsJson("{}");
97+
this.graphQlService.setDefaultResponse("{}");
9898
}
9999

100100
public MockExecutionGraphQlService getGraphQlService() {

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
import org.springframework.http.server.reactive.HttpHandler;
4646
import org.springframework.lang.Nullable;
4747
import org.springframework.test.web.reactive.server.HttpHandlerConnector;
48+
import org.springframework.util.Assert;
4849
import org.springframework.util.MimeType;
4950
import org.springframework.web.reactive.function.client.WebClient;
5051
import org.springframework.web.reactive.function.server.HandlerStrategies;
@@ -239,12 +240,13 @@ private interface ClientBuilderSetup {
239240

240241
private abstract static class AbstractBuilderSetup implements ClientBuilderSetup {
241242

243+
@Nullable
242244
private WebGraphQlRequest graphQlRequest;
243245

244246
private final MockExecutionGraphQlService graphQlService = new MockExecutionGraphQlService();
245247

246248
public AbstractBuilderSetup() {
247-
this.graphQlService.setDefaultDataAsJson("{}");
249+
this.graphQlService.setDefaultResponse("{}");
248250
}
249251

250252
@Override
@@ -254,6 +256,7 @@ public MockExecutionGraphQlService getGraphQlService() {
254256

255257
@Override
256258
public WebGraphQlRequest getActualRequest() {
259+
Assert.state(this.graphQlRequest != null, "No saved WebGraphQlRequest");
257260
return this.graphQlRequest;
258261
}
259262

spring-graphql/src/testFixtures/java/org/springframework/graphql/execution/MockExecutionGraphQlService.java

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,18 @@
2828
import graphql.ExecutionResultImpl;
2929
import graphql.GraphQLError;
3030
import graphql.GraphqlErrorBuilder;
31-
import reactor.core.publisher.Flux;
3231
import reactor.core.publisher.Mono;
3332

3433
import org.springframework.graphql.ExecutionGraphQlRequest;
3534
import org.springframework.graphql.ExecutionGraphQlResponse;
3635
import org.springframework.graphql.ExecutionGraphQlService;
37-
import org.springframework.graphql.GraphQlRequest;
38-
import org.springframework.graphql.GraphQlResponse;
39-
import org.springframework.graphql.client.GraphQlTransport;
40-
import org.springframework.graphql.support.DefaultExecutionGraphQlRequest;
4136
import org.springframework.graphql.support.DefaultExecutionGraphQlResponse;
4237
import org.springframework.lang.Nullable;
4338
import org.springframework.util.Assert;
4439
import org.springframework.util.ObjectUtils;
4540

46-
4741
/**
48-
*
42+
* {@link ExecutionGraphQlService} with mock responses.
4943
*
5044
* @author Rossen Stoyanchev
5145
*/
@@ -54,6 +48,7 @@ public class MockExecutionGraphQlService implements ExecutionGraphQlService {
5448
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
5549

5650

51+
@Nullable
5752
private ExecutionGraphQlRequest graphQlRequest;
5853

5954
private final Map<String, ExecutionGraphQlResponse> responses = new HashMap<>();
@@ -62,30 +57,57 @@ public class MockExecutionGraphQlService implements ExecutionGraphQlService {
6257
private ExecutionGraphQlResponse defaultResponse;
6358

6459

65-
public void setDefaultDataAsJson(String dataJson) {
60+
/**
61+
* Get the last, saved request.
62+
*/
63+
public ExecutionGraphQlRequest getGraphQlRequest() {
64+
Assert.state(this.graphQlRequest != null, "No saved GraphQlRequest");
65+
return this.graphQlRequest;
66+
}
67+
68+
69+
/**
70+
* Set the default response to fall back on as a "data"-only response.
71+
*/
72+
public void setDefaultResponse(String dataJson) {
6673
ExecutionInput input = ExecutionInput.newExecutionInput().query("").build();
6774
ExecutionResult result = ExecutionResultImpl.newExecutionResult().data(decode(dataJson)).build();
6875
this.defaultResponse = new DefaultExecutionGraphQlResponse(input, result);
6976
}
7077

78+
/**
79+
* Set a "data"-only response for the given document.
80+
*/
7181
public void setDataAsJson(String document, String dataJson) {
7282
setResponse(document, decode(dataJson));
7383
}
7484

85+
/**
86+
* Set an "errors" response for the given document.
87+
*/
7588
public void setErrors(String document, GraphQLError... errors) {
7689
setResponse(document, null, errors);
7790
}
7891

92+
/**
93+
* Set an "errors" response for the given document.
94+
*/
7995
public void setError(String document, Consumer<GraphqlErrorBuilder<?>> errorBuilderConsumer) {
8096
GraphqlErrorBuilder<?> errorBuilder = GraphqlErrorBuilder.newError();
8197
errorBuilderConsumer.accept(errorBuilder);
8298
setResponse(document, null, errorBuilder.build());
8399
}
84100

101+
/**
102+
* Set a "data" and "errors" response for the given document.
103+
*/
85104
public void setDataAsJsonAndErrors(String document, String dataJson, GraphQLError... errors) {
86105
setResponse(document, decode(dataJson), errors);
87106
}
88107

108+
/**
109+
* Set a "data" and "errors" response for the given document.
110+
*/
89111
private void setResponse(String document, @Nullable Map<String, Object> data, GraphQLError... errors) {
90112
ExecutionResultImpl.Builder builder = new ExecutionResultImpl.Builder();
91113
if (data != null) {
@@ -97,6 +119,9 @@ private void setResponse(String document, @Nullable Map<String, Object> data, Gr
97119
setResponse(document, builder.build());
98120
}
99121

122+
/**
123+
* Set a response for the given document.
124+
*/
100125
@SuppressWarnings("unused")
101126
public void setResponse(String document, ExecutionResult result) {
102127
ExecutionInput input = ExecutionInput.newExecutionInput().query(document).build();
@@ -113,11 +138,6 @@ private Map<String, Object> decode(String json) {
113138
}
114139
}
115140

116-
public ExecutionGraphQlRequest getGraphQlRequest() {
117-
return this.graphQlRequest;
118-
}
119-
120-
121141
@Override
122142
public Mono<ExecutionGraphQlResponse> execute(ExecutionGraphQlRequest request) {
123143
this.graphQlRequest = request;
@@ -127,25 +147,4 @@ public Mono<ExecutionGraphQlResponse> execute(ExecutionGraphQlRequest request) {
127147
return Mono.just(response);
128148
}
129149

130-
public GraphQlTransport asGraphQlTransport() {
131-
return new GraphQlTransport() {
132-
133-
@Override
134-
public Mono<GraphQlResponse> execute(GraphQlRequest request) {
135-
ExecutionGraphQlRequest executionRequest = toExecutionRequest(request);
136-
return MockExecutionGraphQlService.this.execute(executionRequest).cast(GraphQlResponse.class);
137-
}
138-
139-
@Override
140-
public Flux<GraphQlResponse> executeSubscription(GraphQlRequest request) {
141-
return Flux.error(new UnsupportedOperationException());
142-
}
143-
};
144-
}
145-
146-
private ExecutionGraphQlRequest toExecutionRequest(GraphQlRequest request) {
147-
return new DefaultExecutionGraphQlRequest(
148-
request.getDocument(), request.getOperationName(), request.getVariables(), request.getExtensions(), "1", null);
149-
}
150-
151150
}

0 commit comments

Comments
 (0)