Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit bf3daea

Browse files
authored
Merge pull request #794 from firatkucuk/graphiql-static-resources
Graphiql static resource loading fix
2 parents b764ba6 + 38ee170 commit bf3daea

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

graphql-spring-boot-autoconfigure/src/main/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLAutoConfiguration.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.REACTIVE;
44
import static org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type.SERVLET;
55

6+
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
67
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
78
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
89
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
910
import org.springframework.boot.context.properties.EnableConfigurationProperties;
1011
import org.springframework.context.annotation.Bean;
1112
import org.springframework.context.annotation.Configuration;
13+
import org.springframework.core.io.ClassPathResource;
14+
import org.springframework.web.reactive.function.server.RouterFunction;
15+
import org.springframework.web.reactive.function.server.RouterFunctions;
16+
import org.springframework.web.reactive.function.server.ServerResponse;
17+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
18+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
19+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;;
1220

1321
/**
1422
* @author Andrew Potter
@@ -31,4 +39,27 @@ ServletGraphiQLController servletGraphiQLController(GraphiQLProperties propertie
3139
ReactiveGraphiQLController reactiveGraphiQLController(GraphiQLProperties properties) {
3240
return new ReactiveGraphiQLController(properties);
3341
}
42+
43+
@Bean
44+
@ConditionalOnWebApplication(type = REACTIVE)
45+
@ConditionalOnExpression("'${graphql.graphiql.cdn.enabled:false}' == 'false'")
46+
public RouterFunction<ServerResponse> graphiqlStaticFilesRouter() {
47+
48+
return RouterFunctions.resources(
49+
"/vendor/graphiql/**", new ClassPathResource("static/vendor/graphiql/"));
50+
}
51+
52+
@Configuration
53+
@EnableWebMvc
54+
@ConditionalOnWebApplication(type = SERVLET)
55+
@ConditionalOnExpression("'${graphql.graphiql.cdn.enabled:false}' == 'false'")
56+
public static class GraphiQLWebMvcResourceConfiguration implements WebMvcConfigurer {
57+
58+
@Override
59+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
60+
61+
registry.addResourceHandler("/vendor/graphiql/**")
62+
.addResourceLocations(new ClassPathResource("static/vendor/graphiql/"));
63+
}
64+
}
3465
}

graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/graphiql/GraphiQLEnabledTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
import org.springframework.test.web.servlet.MockMvc;
2121

2222
@ExtendWith(SpringExtension.class)
23-
@SpringBootTest(classes = {GraphiQLAutoConfiguration.class})
23+
@SpringBootTest(classes = {
24+
GraphiQLAutoConfiguration.class,
25+
GraphiQLAutoConfiguration.GraphiQLWebMvcResourceConfiguration.class
26+
})
2427
@AutoConfigureMockMvc
2528
@TestPropertySource("classpath:enabled-config.properties")
2629
class GraphiQLEnabledTest {
@@ -42,4 +45,15 @@ void graphiqlShouldBeAvailableAtDefaultEndpoint() throws Exception {
4245
.andExpect(content().string(not(is(emptyString()))))
4346
.andReturn();
4447
}
48+
49+
@Test
50+
void graphiqlResourceShouldBeAvailableAtDefaultEndpoint() throws Exception {
51+
52+
mockMvc
53+
.perform(get("/vendor/graphiql/favicon.ico"))
54+
.andExpect(status().isOk())
55+
.andExpect(content().contentTypeCompatibleWith("image/x-icon"))
56+
.andExpect(content().string(not(is(emptyString()))))
57+
.andReturn();
58+
}
4559
}

graphql-spring-boot-autoconfigure/src/test/java/graphql/kickstart/autoconfigure/editor/graphiql/ReactiveGraphiQLEnabledTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,20 @@ void graphiqlShouldBeAvailableAtDefaultEndpoint() throws Exception {
5050
log.info("{}", responseBody);
5151
assertThat(document.select("head title").text()).isEqualTo("GraphiQL");
5252
}
53+
54+
@Test
55+
void graphiqlResourceShouldBeAvailableAtDefaultEndpoint() {
56+
final String responseBody =
57+
webTestClient
58+
.get()
59+
.uri("/vendor/graphiql/favicon.ico")
60+
.exchange()
61+
.expectStatus()
62+
.isOk()
63+
.expectBody(String.class)
64+
.returnResult()
65+
.getResponseBody();
66+
67+
assertThat(responseBody).isNotNull();
68+
}
5369
}

0 commit comments

Comments
 (0)