From 38ee170fff9df899ea0e0b3be9f100f1831c4e5e Mon Sep 17 00:00:00 2001 From: firatkucuk Date: Tue, 22 Mar 2022 22:03:48 +0100 Subject: [PATCH] fix(#793): graphiql static resource loading --- .../graphiql/GraphiQLAutoConfiguration.java | 31 +++++++++++++++++++ .../editor/graphiql/GraphiQLEnabledTest.java | 16 +++++++++- .../graphiql/ReactiveGraphiQLEnabledTest.java | 16 ++++++++++ 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLAutoConfiguration.java b/graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLAutoConfiguration.java index 32d79668..919f0d18 100644 --- a/graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLAutoConfiguration.java +++ b/graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLAutoConfiguration.java @@ -3,12 +3,20 @@ import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.REACTIVE; import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.SERVLET; +import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.web.reactive.function.server.RouterFunction; +import org.springframework.web.reactive.function.server.RouterFunctions; +import org.springframework.web.reactive.function.server.ServerResponse; +import org.springframework.web.servlet.config.annotation.EnableWebMvc; +import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;; /** * @author Andrew Potter @@ -31,4 +39,27 @@ ServletGraphiQLController servletGraphiQLController(GraphiQLProperties propertie ReactiveGraphiQLController reactiveGraphiQLController(GraphiQLProperties properties) { return new ReactiveGraphiQLController(properties); } + + @Bean + @ConditionalOnWebApplication(type = REACTIVE) + @ConditionalOnExpression("'${graphql.graphiql.cdn.enabled:false}' == 'false'") + public RouterFunction graphiqlStaticFilesRouter() { + + return RouterFunctions.resources( + "/vendor/graphiql/**", new ClassPathResource("static/vendor/graphiql/")); + } + + @Configuration + @EnableWebMvc + @ConditionalOnWebApplication(type = SERVLET) + @ConditionalOnExpression("'${graphql.graphiql.cdn.enabled:false}' == 'false'") + public static class GraphiQLWebMvcResourceConfiguration implements WebMvcConfigurer { + + @Override + public void addResourceHandlers(ResourceHandlerRegistry registry) { + + registry.addResourceHandler("/vendor/graphiql/**") + .addResourceLocations(new ClassPathResource("static/vendor/graphiql/")); + } + } } diff --git a/graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLEnabledTest.java b/graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLEnabledTest.java index 454834fe..93e54157 100644 --- a/graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLEnabledTest.java +++ b/graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLEnabledTest.java @@ -20,7 +20,10 @@ import org.springframework.test.web.servlet.MockMvc; @ExtendWith(SpringExtension.class) -@SpringBootTest(classes = {GraphiQLAutoConfiguration.class}) +@SpringBootTest(classes = { + GraphiQLAutoConfiguration.class, + GraphiQLAutoConfiguration.GraphiQLWebMvcResourceConfiguration.class +}) @AutoConfigureMockMvc @TestPropertySource("classpath:enabled-config.properties") class GraphiQLEnabledTest { @@ -42,4 +45,15 @@ void graphiqlShouldBeAvailableAtDefaultEndpoint() throws Exception { .andExpect(content().string(not(is(emptyString())))) .andReturn(); } + + @Test + void graphiqlResourceShouldBeAvailableAtDefaultEndpoint() throws Exception { + + mockMvc + .perform(get("/vendor/graphiql/favicon.ico")) + .andExpect(status().isOk()) + .andExpect(content().contentTypeCompatibleWith("image/x-icon")) + .andExpect(content().string(not(is(emptyString())))) + .andReturn(); + } } diff --git a/graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/graphiql/ReactiveGraphiQLEnabledTest.java b/graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/graphiql/ReactiveGraphiQLEnabledTest.java index e33e91af..8dbc0443 100644 --- a/graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/graphiql/ReactiveGraphiQLEnabledTest.java +++ b/graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/graphiql/ReactiveGraphiQLEnabledTest.java @@ -50,4 +50,20 @@ void graphiqlShouldBeAvailableAtDefaultEndpoint() throws Exception { log.info("{}", responseBody); assertThat(document.select("head title").text()).isEqualTo("GraphiQL"); } + + @Test + void graphiqlResourceShouldBeAvailableAtDefaultEndpoint() { + final String responseBody = + webTestClient + .get() + .uri("/vendor/graphiql/favicon.ico") + .exchange() + .expectStatus() + .isOk() + .expectBody(String.class) + .returnResult() + .getResponseBody(); + + assertThat(responseBody).isNotNull(); + } }