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

Graphiql static resource loading fix #794

Merged
merged 1 commit into from
Apr 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<ServerResponse> 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/"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}