Avoided repeated Resource resolution of graphiql page

This commit is contained in:
Rossen Stoyanchev
2020-11-16 21:46:35 +00:00
parent 41333b6c3e
commit 1dc38c8323
2 changed files with 18 additions and 8 deletions

View File

@@ -26,6 +26,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.graphql.WebFluxGraphQLHandler;
import org.springframework.http.MediaType;
@@ -50,11 +51,15 @@ public class WebFluxGraphQLAutoConfiguration {
}
@Bean
public RouterFunction<ServerResponse> graphQLQueryEndpoint(ResourceLoader resourceLoader, WebFluxGraphQLHandler handler,
GraphQLProperties graphQLProperties) {
public RouterFunction<ServerResponse> graphQLQueryEndpoint(
ResourceLoader resourceLoader, WebFluxGraphQLHandler handler, GraphQLProperties properties) {
String path = properties.getPath();
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
return RouterFunctions.route()
.GET(graphQLProperties.getPath(), req -> ServerResponse.ok().bodyValue(resourceLoader.getResource("classpath:graphiql/index.html")))
.POST(graphQLProperties.getPath(), accept(MediaType.APPLICATION_JSON).and(contentType(MediaType.APPLICATION_JSON)), handler)
.GET(path, req -> ServerResponse.ok().bodyValue(resource))
.POST(path, accept(MediaType.APPLICATION_JSON).and(contentType(MediaType.APPLICATION_JSON)), handler)
.build();
}

View File

@@ -26,6 +26,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.graphql.WebMvcGraphQLHandler;
import org.springframework.http.MediaType;
@@ -50,11 +51,15 @@ public class WebMvcGraphQLAutoConfiguration {
}
@Bean
public RouterFunction<ServerResponse> graphQLQueryEndpoint(ResourceLoader resourceLoader, WebMvcGraphQLHandler handler,
GraphQLProperties graphQLProperties) {
public RouterFunction<ServerResponse> graphQLQueryEndpoint(
ResourceLoader resourceLoader, WebMvcGraphQLHandler handler, GraphQLProperties properties) {
String path = properties.getPath();
Resource resource = resourceLoader.getResource("classpath:graphiql/index.html");
return RouterFunctions.route()
.GET(graphQLProperties.getPath(), req -> ServerResponse.ok().body(resourceLoader.getResource("classpath:graphiql/index.html")))
.POST(graphQLProperties.getPath(), contentType(MediaType.APPLICATION_JSON).and(accept(MediaType.APPLICATION_JSON)), handler)
.GET(path, req -> ServerResponse.ok().body(resource))
.POST(path, contentType(MediaType.APPLICATION_JSON).and(accept(MediaType.APPLICATION_JSON)), handler)
.build();
}