Add builder for WebGraphQlHandler
To allow adding more options when creating a WebGraphQlHandler. See gh-53
This commit is contained in:
@@ -65,7 +65,9 @@ public class WebFluxGraphQlAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public WebGraphQlHandler webGraphQlHandler(ObjectProvider<WebInterceptor> interceptors, GraphQlService service) {
|
||||
return WebInterceptor.createHandler(interceptors.orderedStream().collect(Collectors.toList()), service);
|
||||
return WebGraphQlHandler.builder(service)
|
||||
.interceptors(interceptors.orderedStream().collect(Collectors.toList()))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -71,7 +71,9 @@ public class WebMvcGraphQlAutoConfiguration {
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public WebGraphQlHandler webGraphQlHandler(ObjectProvider<WebInterceptor> interceptors, GraphQlService service) {
|
||||
return WebInterceptor.createHandler(interceptors.orderedStream().collect(Collectors.toList()), service);
|
||||
return WebGraphQlHandler.builder(service)
|
||||
.interceptors(interceptors.orderedStream().collect(Collectors.toList()))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
* Copyright 2002-2021 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.graphql.web;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import graphql.ExecutionInput;
|
||||
|
||||
import org.springframework.graphql.GraphQlService;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* Default implementation of {@link WebGraphQlHandler.Builder}.
|
||||
*/
|
||||
class DefaultWebGraphQlHandlerBuilder implements WebGraphQlHandler.Builder {
|
||||
|
||||
private final GraphQlService service;
|
||||
|
||||
@Nullable
|
||||
private List<WebInterceptor> interceptors;
|
||||
|
||||
|
||||
DefaultWebGraphQlHandlerBuilder(GraphQlService service) {
|
||||
Assert.notNull(service, "GraphQlService must not be null");
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public WebGraphQlHandler.Builder interceptors(List<WebInterceptor> interceptors) {
|
||||
this.interceptors = (this.interceptors != null ? this.interceptors : new ArrayList<>());
|
||||
this.interceptors.addAll(interceptors);
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public WebGraphQlHandler build() {
|
||||
List<WebInterceptor> interceptorsToUse =
|
||||
(this.interceptors != null ? this.interceptors : Collections.emptyList());
|
||||
|
||||
return interceptorsToUse.stream()
|
||||
.reduce(WebInterceptor::andThen)
|
||||
.map(interceptor -> (WebGraphQlHandler) input -> interceptor.intercept(input, createHandler()))
|
||||
.orElse(createHandler());
|
||||
}
|
||||
|
||||
private WebGraphQlHandler createHandler() {
|
||||
return webInput -> {
|
||||
ExecutionInput input = webInput.toExecutionInput();
|
||||
return this.service.execute(input).map(result -> new WebOutput(webInput, result));
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
@@ -50,7 +50,8 @@ public class WebInterceptorTests {
|
||||
WebInput input = new WebInput(
|
||||
URI.create("/"), new HttpHeaders(), Collections.singletonMap("query", "any"), "1");
|
||||
|
||||
WebOutput output = WebInterceptor.createHandler(interceptors, service).handle(input).block();
|
||||
WebOutput output = WebGraphQlHandler.builder(service).interceptors(interceptors).build()
|
||||
.handle(input).block();
|
||||
|
||||
assertThat(sb.toString()).isEqualTo(":pre1:pre2:pre3:post3:post2:post1");
|
||||
assertThat(output.getResponseHeaders().get("name")).containsExactly("value3", "value2", "value1");
|
||||
|
||||
@@ -272,11 +272,12 @@ public class GraphQlWebSocketHandlerTests {
|
||||
}
|
||||
|
||||
private GraphQlWebSocketHandler initWebSocketHandler(
|
||||
@Nullable List<WebInterceptor> interceptors, @Nullable Duration initTimeoutDuration) throws Exception {
|
||||
@Nullable List<WebInterceptor> interceptors, @Nullable Duration initTimeoutDuration) {
|
||||
|
||||
WebGraphQlHandler graphQlHandler = WebInterceptor.createHandler(
|
||||
(interceptors != null ? interceptors : Collections.emptyList()),
|
||||
new ExecutionGraphQlService(graphQlSource()));
|
||||
WebGraphQlHandler graphQlHandler =
|
||||
WebGraphQlHandler.builder(new ExecutionGraphQlService(graphQlSource()))
|
||||
.interceptors(interceptors != null ? interceptors : Collections.emptyList())
|
||||
.build();
|
||||
|
||||
return new GraphQlWebSocketHandler(graphQlHandler,
|
||||
ServerCodecConfigurer.create(),
|
||||
|
||||
@@ -258,9 +258,10 @@ public class GraphQlWebSocketHandlerTests {
|
||||
@Nullable List<WebInterceptor> interceptors, @Nullable Duration initTimeoutDuration) {
|
||||
|
||||
try {
|
||||
WebGraphQlHandler graphQlHandler = WebInterceptor.createHandler(
|
||||
(interceptors != null ? interceptors : Collections.emptyList()),
|
||||
new ExecutionGraphQlService(graphQlSource()));
|
||||
WebGraphQlHandler graphQlHandler =
|
||||
WebGraphQlHandler.builder(new ExecutionGraphQlService(graphQlSource()))
|
||||
.interceptors(interceptors != null ? interceptors : Collections.emptyList())
|
||||
.build();
|
||||
|
||||
return new GraphQlWebSocketHandler(graphQlHandler, converter,
|
||||
(initTimeoutDuration != null ? initTimeoutDuration : Duration.ofSeconds(60)));
|
||||
|
||||
Reference in New Issue
Block a user