From 13ce59ab281e03667207ae1ac742d23fff6fecac Mon Sep 17 00:00:00 2001 From: Dmytro Nosan Date: Fri, 1 Nov 2024 12:03:17 +0200 Subject: [PATCH] Support timeout property for GraphQL over SSE --- .../graphql/GraphQlProperties.java | 23 +++++++++++++++++++ .../GraphQlWebMvcAutoConfiguration.java | 6 +++-- .../GraphQlWebMvcAutoConfigurationTests.java | 10 ++++++++ 3 files changed, 37 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java index b39692f83f07..4324d47668fd 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java @@ -43,6 +43,8 @@ public class GraphQlProperties { private final Rsocket rsocket = new Rsocket(); + private final Sse sse = new Sse(); + public Graphiql getGraphiql() { return this.graphiql; } @@ -67,6 +69,10 @@ public Rsocket getRsocket() { return this.rsocket; } + public Sse getSse() { + return this.sse; + } + public static class Schema { /** @@ -265,4 +271,21 @@ public void setMapping(String mapping) { } + public static class Sse { + + /** + * Time required for concurrent handling to complete. + */ + private Duration timeout; + + public Duration getTimeout() { + return this.timeout; + } + + public void setTimeout(Duration timeout) { + this.timeout = timeout; + } + + } + } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfiguration.java index c36823761e8e..f1004026d806 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfiguration.java @@ -37,6 +37,7 @@ import org.springframework.boot.autoconfigure.graphql.GraphQlAutoConfiguration; import org.springframework.boot.autoconfigure.graphql.GraphQlCorsProperties; import org.springframework.boot.autoconfigure.graphql.GraphQlProperties; +import org.springframework.boot.autoconfigure.graphql.GraphQlProperties.Sse; import org.springframework.boot.autoconfigure.http.HttpMessageConverters; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; @@ -97,8 +98,9 @@ public GraphQlHttpHandler graphQlHttpHandler(WebGraphQlHandler webGraphQlHandler @Bean @ConditionalOnMissingBean - public GraphQlSseHandler graphQlSseHandler(WebGraphQlHandler webGraphQlHandler) { - return new GraphQlSseHandler(webGraphQlHandler); + public GraphQlSseHandler graphQlSseHandler(WebGraphQlHandler webGraphQlHandler, GraphQlProperties properties) { + Sse sse = properties.getSse(); + return new GraphQlSseHandler(webGraphQlHandler, sse.getTimeout()); } @Bean diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfigurationTests.java index 9f6a455ab07a..9bd6618804fa 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/servlet/GraphQlWebMvcAutoConfigurationTests.java @@ -41,6 +41,7 @@ import org.springframework.graphql.server.WebGraphQlHandler; import org.springframework.graphql.server.WebGraphQlInterceptor; import org.springframework.graphql.server.webmvc.GraphQlHttpHandler; +import org.springframework.graphql.server.webmvc.GraphQlSseHandler; import org.springframework.graphql.server.webmvc.GraphQlWebSocketHandler; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -78,6 +79,15 @@ void shouldContributeDefaultBeans() { .doesNotHaveBean(GraphQlWebSocketHandler.class)); } + @Test + void shouldConfigureSseTimeout() { + this.contextRunner.withPropertyValues("spring.graphql.sse.timeout=10s").run((context) -> { + assertThat(context).hasSingleBean(GraphQlSseHandler.class); + GraphQlSseHandler handler = context.getBean(GraphQlSseHandler.class); + assertThat(handler).hasFieldOrPropertyWithValue("timeout", Duration.ofSeconds(10)); + }); + } + @Test void simpleQueryShouldWork() { withMockMvc((mvc) -> {