Skip to content

Commit

Permalink
Add builder for WebGraphQlHandler
Browse files Browse the repository at this point in the history
To allow adding more options when creating a WebGraphQlHandler.

See gh-53
  • Loading branch information
rstoyanchev committed May 28, 2021
1 parent eb88697 commit ca18d03
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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));
};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,11 +272,12 @@ private GraphQlWebSocketHandler initWebSocketHandler() throws Exception {
}

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(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,10 @@ private GraphQlWebSocketHandler initWebSocketHandler(
@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)));
Expand Down

0 comments on commit ca18d03

Please sign in to comment.