diff --git a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebFluxGraphQlAutoConfiguration.java b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebFluxGraphQlAutoConfiguration.java index f25d427a0..19139ea2a 100644 --- a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebFluxGraphQlAutoConfiguration.java +++ b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebFluxGraphQlAutoConfiguration.java @@ -65,7 +65,9 @@ public class WebFluxGraphQlAutoConfiguration { @Bean @ConditionalOnMissingBean public WebGraphQlHandler webGraphQlHandler(ObjectProvider interceptors, GraphQlService service) { - return WebInterceptor.createHandler(interceptors.orderedStream().collect(Collectors.toList()), service); + return WebGraphQlHandler.builder(service) + .interceptors(interceptors.orderedStream().collect(Collectors.toList())) + .build(); } @Bean diff --git a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebMvcGraphQlAutoConfiguration.java b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebMvcGraphQlAutoConfiguration.java index 494ed10e2..e3844a66c 100644 --- a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebMvcGraphQlAutoConfiguration.java +++ b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/WebMvcGraphQlAutoConfiguration.java @@ -71,7 +71,9 @@ public class WebMvcGraphQlAutoConfiguration { @Bean @ConditionalOnMissingBean public WebGraphQlHandler webGraphQlHandler(ObjectProvider interceptors, GraphQlService service) { - return WebInterceptor.createHandler(interceptors.orderedStream().collect(Collectors.toList()), service); + return WebGraphQlHandler.builder(service) + .interceptors(interceptors.orderedStream().collect(Collectors.toList())) + .build(); } @Bean diff --git a/spring-graphql/src/main/java/org/springframework/graphql/web/DefaultWebGraphQlHandlerBuilder.java b/spring-graphql/src/main/java/org/springframework/graphql/web/DefaultWebGraphQlHandlerBuilder.java new file mode 100644 index 000000000..cae299b2e --- /dev/null +++ b/spring-graphql/src/main/java/org/springframework/graphql/web/DefaultWebGraphQlHandlerBuilder.java @@ -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 interceptors; + + + DefaultWebGraphQlHandlerBuilder(GraphQlService service) { + Assert.notNull(service, "GraphQlService must not be null"); + this.service = service; + } + + + @Override + public WebGraphQlHandler.Builder interceptors(List interceptors) { + this.interceptors = (this.interceptors != null ? this.interceptors : new ArrayList<>()); + this.interceptors.addAll(interceptors); + return this; + } + + @Override + public WebGraphQlHandler build() { + List 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)); + }; + } + +} diff --git a/spring-graphql/src/test/java/org/springframework/graphql/web/WebInterceptorTests.java b/spring-graphql/src/test/java/org/springframework/graphql/web/WebInterceptorTests.java index 4666eeb3d..3db1ec4d1 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/web/WebInterceptorTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/web/WebInterceptorTests.java @@ -50,7 +50,8 @@ void interceptorChain() { 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"); diff --git a/spring-graphql/src/test/java/org/springframework/graphql/web/webflux/GraphQlWebSocketHandlerTests.java b/spring-graphql/src/test/java/org/springframework/graphql/web/webflux/GraphQlWebSocketHandlerTests.java index 917102adf..796f65c4e 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/web/webflux/GraphQlWebSocketHandlerTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/web/webflux/GraphQlWebSocketHandlerTests.java @@ -272,11 +272,12 @@ private GraphQlWebSocketHandler initWebSocketHandler() throws Exception { } private GraphQlWebSocketHandler initWebSocketHandler( - @Nullable List interceptors, @Nullable Duration initTimeoutDuration) throws Exception { + @Nullable List 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(), diff --git a/spring-graphql/src/test/java/org/springframework/graphql/web/webmvc/GraphQlWebSocketHandlerTests.java b/spring-graphql/src/test/java/org/springframework/graphql/web/webmvc/GraphQlWebSocketHandlerTests.java index c29e558c4..5f99c148d 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/web/webmvc/GraphQlWebSocketHandlerTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/web/webmvc/GraphQlWebSocketHandlerTests.java @@ -258,9 +258,10 @@ private GraphQlWebSocketHandler initWebSocketHandler( @Nullable List 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)));