From c0f97d3f9b3085c4ed80801e57d3fa3a1ea95464 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Mon, 21 Mar 2022 08:52:01 +0000 Subject: [PATCH] Move WebInterceptorChain into WebInterceptor Shorter name on method declaration within the interceptor, also for consistency with GraphQlClientInterceptor. --- .../client/GraphQlClientInterceptor.java | 2 +- .../web/DefaultWebGraphQlHandlerBuilder.java | 6 +-- .../graphql/web/WebInterceptor.java | 38 +++++++++++------ .../graphql/web/WebInterceptorChain.java | 41 ------------------- .../graphql/web/WebSocketInterceptor.java | 2 +- ...ConsumeOneAndNeverCompleteInterceptor.java | 2 +- .../graphql/web/WebInterceptorTests.java | 2 +- 7 files changed, 32 insertions(+), 61 deletions(-) delete mode 100644 spring-graphql/src/main/java/org/springframework/graphql/web/WebInterceptorChain.java diff --git a/spring-graphql/src/main/java/org/springframework/graphql/client/GraphQlClientInterceptor.java b/spring-graphql/src/main/java/org/springframework/graphql/client/GraphQlClientInterceptor.java index eb1f52ea..a1b9597a 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/client/GraphQlClientInterceptor.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/client/GraphQlClientInterceptor.java @@ -84,7 +84,7 @@ public interface GraphQlClientInterceptor { /** * Delegate to the rest of the chain to perform the request. - * @param request the request to perform. + * @param request the request to perform * @return {@code Mono} with the response * @see GraphQlClient.RequestSpec#execute() */ 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 index ca184aa2..5b0f4be0 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/web/DefaultWebGraphQlHandlerBuilder.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/web/DefaultWebGraphQlHandlerBuilder.java @@ -88,12 +88,12 @@ class DefaultWebGraphQlHandlerBuilder implements WebGraphQlHandler.Builder { @Override public WebGraphQlHandler build() { - WebInterceptorChain endOfChain = + WebInterceptor.Chain endOfChain = request -> this.service.execute(request).map(WebGraphQlResponse::new); - WebInterceptorChain chain = this.interceptors.stream() + WebInterceptor.Chain chain = this.interceptors.stream() .reduce(WebInterceptor::andThen) - .map(interceptor -> (WebInterceptorChain) (request) -> interceptor.intercept(request, endOfChain)) + .map(interceptor -> (WebInterceptor.Chain) (request) -> interceptor.intercept(request, endOfChain)) .orElse(endOfChain); return new WebGraphQlHandler() { diff --git a/spring-graphql/src/main/java/org/springframework/graphql/web/WebInterceptor.java b/spring-graphql/src/main/java/org/springframework/graphql/web/WebInterceptor.java index d6c187ae..053f002c 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/web/WebInterceptor.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/web/WebInterceptor.java @@ -25,10 +25,10 @@ import org.springframework.graphql.ExecutionGraphQlService; import org.springframework.util.Assert; /** - * Interceptor for the handling of GraphQL over HTTP or GraphQL over WebSocket - * requests. Exposes the details of the underlying HTTP request or WebSocket - * handshake, the decoded GraphQL request, and allows customization of the - * {@link ExecutionInput} and the resulting {@link ExecutionResult}. + * Interceptor for server handling of GraphQL over HTTP or WebSocket requests, + * providing access info about the underlying HTTP request or WebSocket + * handshake, and allowing customization of the {@link ExecutionInput} and + * the {@link ExecutionResult}. * *

Interceptors are typically declared as beans in Spring configuration and * ordered as defined in {@link ObjectProvider#orderedStream()}. @@ -42,16 +42,13 @@ import org.springframework.util.Assert; public interface WebInterceptor { /** - * Intercept a request and delegate to the rest of the chain that consists - * of other interceptors followed by a - * {@link ExecutionGraphQlService} that executes the - * request through the GraphQL Java. - * @param request provides access to GraphQL request and allows customization - * of the {@link ExecutionInput} for {@link graphql.GraphQL}. - * @param chain the rest of the chain to handle the request + * Intercept a request and delegate to the rest of the chain including other + * interceptors and a {@link ExecutionGraphQlService}. + * @param request the request to execute + * @param chain the rest of the chain to execute the request * @return a {@link Mono} with the response */ - Mono intercept(WebGraphQlRequest request, WebInterceptorChain chain); + Mono intercept(WebGraphQlRequest request, Chain chain); /** * Return a new {@link WebInterceptor} that invokes the current interceptor @@ -62,9 +59,24 @@ public interface WebInterceptor { default WebInterceptor andThen(WebInterceptor interceptor) { Assert.notNull(interceptor, "WebInterceptor is required"); return (request, chain) -> { - WebInterceptorChain nextChain = nextRequest -> interceptor.intercept(nextRequest, chain); + Chain nextChain = nextRequest -> interceptor.intercept(nextRequest, chain); return intercept(request, nextChain); }; } + + /** + * Contract for delegation to the rest of the chain. + */ + interface Chain { + + /** + * Delegate to the rest of the chain to execute the request. + * @param request the request to execute + * the {@link ExecutionInput} for {@link graphql.GraphQL}. + * @return {@code Mono} with the response + */ + Mono next(WebGraphQlRequest request); + + } } diff --git a/spring-graphql/src/main/java/org/springframework/graphql/web/WebInterceptorChain.java b/spring-graphql/src/main/java/org/springframework/graphql/web/WebInterceptorChain.java deleted file mode 100644 index 34ddc18a..00000000 --- a/spring-graphql/src/main/java/org/springframework/graphql/web/WebInterceptorChain.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 2002-2022 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 graphql.ExecutionInput; -import reactor.core.publisher.Mono; - -import org.springframework.graphql.ExecutionGraphQlService; - -/** - * Allows a {@link WebInterceptor} to invoke the rest of the chain. - * - * @author Rossen Stoyanchev - * @since 1.0.0 - */ -public interface WebInterceptorChain { - - /** - * Delegate to the rest of the chain that consists of other interceptors - * followed by a {@link ExecutionGraphQlService} that - * executes the request through the GraphQL Java. - * @param request provides access to GraphQL request and allows customizing - * the {@link ExecutionInput} for {@link graphql.GraphQL}. - * @return {@code Mono} with the response - */ - Mono next(WebGraphQlRequest request); - -} diff --git a/spring-graphql/src/main/java/org/springframework/graphql/web/WebSocketInterceptor.java b/spring-graphql/src/main/java/org/springframework/graphql/web/WebSocketInterceptor.java index 0a1b7c1e..e3a79d6b 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/web/WebSocketInterceptor.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/web/WebSocketInterceptor.java @@ -31,7 +31,7 @@ import reactor.core.publisher.Mono; public interface WebSocketInterceptor extends WebInterceptor { @Override - default Mono intercept(WebGraphQlRequest request, WebInterceptorChain chain) { + default Mono intercept(WebGraphQlRequest request, Chain chain) { return chain.next(request); } diff --git a/spring-graphql/src/test/java/org/springframework/graphql/web/ConsumeOneAndNeverCompleteInterceptor.java b/spring-graphql/src/test/java/org/springframework/graphql/web/ConsumeOneAndNeverCompleteInterceptor.java index a9c0539c..8d25dc76 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/web/ConsumeOneAndNeverCompleteInterceptor.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/web/ConsumeOneAndNeverCompleteInterceptor.java @@ -23,7 +23,7 @@ import reactor.core.publisher.Mono; public class ConsumeOneAndNeverCompleteInterceptor implements WebInterceptor { @Override - public Mono intercept(WebGraphQlRequest request, WebInterceptorChain chain) { + public Mono intercept(WebGraphQlRequest request, Chain chain) { return chain.next(request).map(response -> response.transform(builder -> { Object originalData = response.getData(); if (originalData instanceof Publisher) { 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 e2063609..9c73d5c7 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 @@ -110,7 +110,7 @@ public class WebInterceptorTests { } @Override - public Mono intercept(WebGraphQlRequest request, WebInterceptorChain chain) { + public Mono intercept(WebGraphQlRequest request, Chain chain) { this.sb.append(":pre").append(this.order); return chain.next(request) .map((response) -> {