Move WebInterceptorChain into WebInterceptor

Shorter name on method declaration within the interceptor, also for
consistency with GraphQlClientInterceptor.
This commit is contained in:
rstoyanchev
2022-03-21 08:52:01 +00:00
parent bb6280863a
commit c0f97d3f9b
7 changed files with 32 additions and 61 deletions

View File

@@ -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()
*/

View File

@@ -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() {

View File

@@ -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}.
*
* <p>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<WebGraphQlResponse> intercept(WebGraphQlRequest request, WebInterceptorChain chain);
Mono<WebGraphQlResponse> 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<WebGraphQlResponse> next(WebGraphQlRequest request);
}
}

View File

@@ -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<WebGraphQlResponse> next(WebGraphQlRequest request);
}

View File

@@ -31,7 +31,7 @@ import reactor.core.publisher.Mono;
public interface WebSocketInterceptor extends WebInterceptor {
@Override
default Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, WebInterceptorChain chain) {
default Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
return chain.next(request);
}

View File

@@ -23,7 +23,7 @@ import reactor.core.publisher.Mono;
public class ConsumeOneAndNeverCompleteInterceptor implements WebInterceptor {
@Override
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, WebInterceptorChain chain) {
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
return chain.next(request).map(response -> response.transform(builder -> {
Object originalData = response.getData();
if (originalData instanceof Publisher) {

View File

@@ -110,7 +110,7 @@ public class WebInterceptorTests {
}
@Override
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, WebInterceptorChain chain) {
public Mono<WebGraphQlResponse> intercept(WebGraphQlRequest request, Chain chain) {
this.sb.append(":pre").append(this.order);
return chain.next(request)
.map((response) -> {