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 69e92eee206..be698e1c963 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 @@ -315,11 +315,24 @@ public class GraphQlProperties { public static class Sse { + /** + * How frequently keep-alive messages should be sent. + */ + private Duration keepAlive; + /** * Time required for concurrent handling to complete. */ private Duration timeout; + public Duration getKeepAlive() { + return this.keepAlive; + } + + public void setKeepAlive(Duration keepAlive) { + this.keepAlive = keepAlive; + } + public Duration getTimeout() { return this.timeout; } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfiguration.java index 72fc4b2751e..8dddf6d5ae4 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfiguration.java @@ -94,7 +94,8 @@ public class GraphQlWebFluxAutoConfiguration { @Bean @ConditionalOnMissingBean public GraphQlSseHandler graphQlSseHandler(WebGraphQlHandler webGraphQlHandler, GraphQlProperties properties) { - return new GraphQlSseHandler(webGraphQlHandler, properties.getHttp().getSse().getTimeout()); + return new GraphQlSseHandler(webGraphQlHandler, properties.getHttp().getSse().getTimeout(), + properties.getHttp().getSse().getKeepAlive()); } @Bean 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 f5c7edee715..110e2a5afb1 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 @@ -98,7 +98,8 @@ public class GraphQlWebMvcAutoConfiguration { @Bean @ConditionalOnMissingBean public GraphQlSseHandler graphQlSseHandler(WebGraphQlHandler webGraphQlHandler, GraphQlProperties properties) { - return new GraphQlSseHandler(webGraphQlHandler, properties.getHttp().getSse().getTimeout()); + return new GraphQlSseHandler(webGraphQlHandler, properties.getHttp().getSse().getTimeout(), + properties.getHttp().getSse().getKeepAlive()); } @Bean diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfigurationTests.java index 2c15f2b1249..a07d1128807 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/reactive/GraphQlWebFluxAutoConfigurationTests.java @@ -42,6 +42,7 @@ import org.springframework.graphql.execution.RuntimeWiringConfigurer; import org.springframework.graphql.server.WebGraphQlHandler; import org.springframework.graphql.server.WebGraphQlInterceptor; import org.springframework.graphql.server.webflux.GraphQlHttpHandler; +import org.springframework.graphql.server.webflux.GraphQlSseHandler; import org.springframework.graphql.server.webflux.GraphQlWebSocketHandler; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; @@ -271,6 +272,24 @@ class GraphQlWebFluxAutoConfigurationTests { }); } + @Test + void shouldConfigureSseTimeout() { + this.contextRunner.withPropertyValues("spring.graphql.http.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 shouldConfigureSseKeepAlive() { + this.contextRunner.withPropertyValues("spring.graphql.http.sse.keep-alive=5s").run((context) -> { + assertThat(context).hasSingleBean(GraphQlSseHandler.class); + GraphQlSseHandler handler = context.getBean(GraphQlSseHandler.class); + assertThat(handler).hasFieldOrPropertyWithValue("keepAliveDuration", Duration.ofSeconds(5)); + }); + } + @Test void routerFunctionShouldHaveOrderZero() { this.contextRunner.withUserConfiguration(CustomRouterFunctions.class).run((context) -> { 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 5ae695374f8..e8b9bb8f8ee 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 @@ -108,6 +108,15 @@ class GraphQlWebMvcAutoConfigurationTests { }); } + @Test + void shouldConfigureSseKeepAlive() { + this.contextRunner.withPropertyValues("spring.graphql.http.sse.keep-alive=5s").run((context) -> { + assertThat(context).hasSingleBean(GraphQlSseHandler.class); + GraphQlSseHandler handler = context.getBean(GraphQlSseHandler.class); + assertThat(handler).hasFieldOrPropertyWithValue("keepAliveDuration", Duration.ofSeconds(5)); + }); + } + @Test void simpleQueryShouldWork() { withMockMvc((mvc) -> {