WebInterceptor contract revision

WebInterceptor now uses delegation, forming a chain of interceptors
followed by a GraphQLService at the end to invoke graphql.GraphQL.

Closes gh-49
This commit is contained in:
Rossen Stoyanchev
2021-04-30 21:26:26 +01:00
parent e5a92a310d
commit f93c46eee3
29 changed files with 402 additions and 407 deletions

View File

@@ -43,7 +43,7 @@ import reactor.core.publisher.Flux;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.graphql.RequestInput;
import org.springframework.graphql.web.WebGraphQLService;
import org.springframework.graphql.web.WebGraphQLHandler;
import org.springframework.graphql.web.WebInput;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@@ -83,9 +83,9 @@ class DefaultGraphQLTester implements GraphQLTester {
this.requestStrategy = new WebTestClientRequestStrategy(client, this.jsonPathConfig);
}
DefaultGraphQLTester(WebGraphQLService service) {
DefaultGraphQLTester(WebGraphQLHandler handler) {
this.jsonPathConfig = initJsonPathConfig();
this.requestStrategy = new DirectRequestStrategy(service, this.jsonPathConfig);
this.requestStrategy = new DirectRequestStrategy(handler, this.jsonPathConfig);
}
private Configuration initJsonPathConfig() {
@@ -182,12 +182,12 @@ class DefaultGraphQLTester implements GraphQLTester {
private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(5);
private final WebGraphQLService graphQLService;
private final WebGraphQLHandler graphQLHandler;
private final Configuration jsonPathConfig;
public DirectRequestStrategy(WebGraphQLService service, Configuration jsonPathConfig) {
this.graphQLService = service;
public DirectRequestStrategy(WebGraphQLHandler handler, Configuration jsonPathConfig) {
this.graphQLHandler = handler;
this.jsonPathConfig = jsonPathConfig;
}
@@ -213,7 +213,7 @@ class DefaultGraphQLTester implements GraphQLTester {
private ExecutionResult executeInternal(RequestInput input) {
WebInput webInput = new WebInput(DEFAULT_URL, DEFAULT_HEADERS, input.toMap(), null);
ExecutionResult result = this.graphQLService.execute(webInput).block(DEFAULT_TIMEOUT);
ExecutionResult result = this.graphQLHandler.handle(webInput).block(DEFAULT_TIMEOUT);
Assert.notNull(result, "Expected ExecutionResult");
return result;
}

View File

@@ -24,13 +24,14 @@ import graphql.GraphQLError;
import reactor.core.publisher.Flux;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.graphql.web.WebGraphQLService;
import org.springframework.graphql.web.WebGraphQLHandler;
import org.springframework.lang.Nullable;
import org.springframework.test.web.reactive.server.WebTestClient;
/**
* Main entry point for testing GraphQL with requests performed via
* {@link WebTestClient} as an HTTP client or via any {@link WebGraphQLService}.
* Main entry point for testing GraphQL with requests performed either as an
* HTTP client via {@link WebTestClient} or directly via a
* {@link WebGraphQLHandler}.
*
*
* <p>GraphQL requests to Spring MVC without an HTTP server:
@@ -75,7 +76,7 @@ import org.springframework.test.web.reactive.server.WebTestClient;
* }
* </pre>
*
* <p>GraphQL requests to any {@link WebGraphQLService}:
* <p>GraphQL requests to any {@link WebGraphQLHandler}:
* <pre class="code">
* &#064;SpringBootTest
* public class MyTests {
@@ -83,8 +84,8 @@ import org.springframework.test.web.reactive.server.WebTestClient;
* private GraphQLTester graphQLTester;
*
* &#064;BeforeEach
* public void setUp(&#064;Autowired WebGraphQLService service) {
* this.graphQLTester = GraphQLTester.create(service);
* public void setUp(&#064;Autowired WebGraphQLHandler handler) {
* this.graphQLTester = GraphQLTester.create(handler);
* }
* </pre>
*/
@@ -112,13 +113,13 @@ public interface GraphQLTester {
}
/**
* Create a {@code GraphQLTester} that performs GraphQL requests through the
* given {@link WebGraphQLService}.
* @param service the handler to execute requests with
* Create a {@code GraphQLTester} that performs GraphQL requests through
* the given {@link WebGraphQLHandler}.
* @param handler the handler to execute requests with
* @return the created {@code GraphQLTester} instance
*/
static GraphQLTester create(WebGraphQLService service) {
return new DefaultGraphQLTester(service);
static GraphQLTester create(WebGraphQLHandler handler) {
return new DefaultGraphQLTester(handler);
}

View File

@@ -40,7 +40,7 @@ import org.mockito.ArgumentCaptor;
import reactor.core.publisher.Mono;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.graphql.web.WebGraphQLService;
import org.springframework.graphql.web.WebGraphQLHandler;
import org.springframework.graphql.web.WebInput;
import org.springframework.graphql.web.WebOutput;
import org.springframework.http.HttpHeaders;
@@ -58,7 +58,7 @@ import static org.mockito.Mockito.when;
* Tests for {@link GraphQLTester} parameterized to:
* <ul>
* <li>Connect to {@link MockWebServer} and return a preset HTTP response.
* <li>Use mock {@link WebGraphQLService} to return a preset {@link ExecutionResult}.
* <li>Use mock {@link WebGraphQLHandler} to return a preset {@link ExecutionResult}.
* </ul>
*
* <p>There is no actual handling via {@link graphql.GraphQL} in either scenario.
@@ -71,7 +71,7 @@ public class GraphQLTesterTests {
public static Stream<GraphQLTesterSetup> argumentSource() {
return Stream.of(new MockWebServerSetup(), new MockWebGraphQLServiceSetup());
return Stream.of(new MockWebServerSetup(), new MockWebGraphQLHandlerSetup());
}
@@ -393,16 +393,16 @@ public class GraphQLTesterTests {
}
private static class MockWebGraphQLServiceSetup implements GraphQLTesterSetup {
private static class MockWebGraphQLHandlerSetup implements GraphQLTesterSetup {
private final WebGraphQLService service = mock(WebGraphQLService.class);
private final WebGraphQLHandler handler = mock(WebGraphQLHandler.class);
private final ArgumentCaptor<WebInput> bodyCaptor = ArgumentCaptor.forClass(WebInput.class);
private final GraphQLTester graphQLTester;
public MockWebGraphQLServiceSetup() {
this.graphQLTester = GraphQLTester.create(this.service);
public MockWebGraphQLHandlerSetup() {
this.graphQLTester = GraphQLTester.create(this.handler);
}
@Override
@@ -421,7 +421,7 @@ public class GraphQLTesterTests {
}
ExecutionResult result = builder.build();
WebOutput output = new WebOutput(mock(WebInput.class), result);
when(this.service.execute(this.bodyCaptor.capture())).thenReturn(Mono.just(output));
when(this.handler.handle(this.bodyCaptor.capture())).thenReturn(Mono.just(output));
}
@Override