Auto-configure schema printer endpoint

This commit configuresa new endpoint for printing in text format the
resolved GraphQL schema.
This endpoint is exposed by default under "/graphql/schema" and must be
enabled with "spring.graphql.schema.printer=true".

See gh-29140
This commit is contained in:
Brian Clozel
2021-12-21 08:33:05 +01:00
parent b38d04556e
commit ff9a421786
5 changed files with 57 additions and 4 deletions

View File

@@ -38,6 +38,8 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.test.web.reactive.server.WebTestClient;
import static org.hamcrest.Matchers.containsString;
/**
* Tests for {@link GraphQlWebFluxAutoConfiguration}
*
@@ -51,8 +53,8 @@ class GraphQlWebFluxAutoConfigurationTests {
.withConfiguration(AutoConfigurations.of(HttpHandlerAutoConfiguration.class, WebFluxAutoConfiguration.class,
CodecsAutoConfiguration.class, JacksonAutoConfiguration.class, GraphQlAutoConfiguration.class,
GraphQlWebFluxAutoConfiguration.class))
.withUserConfiguration(DataFetchersConfiguration.class, CustomWebInterceptor.class)
.withPropertyValues("spring.main.web-application-type=reactive");
.withUserConfiguration(DataFetchersConfiguration.class, CustomWebInterceptor.class).withPropertyValues(
"spring.main.web-application-type=reactive", "spring.graphql.schema.printer.enabled=true");
@Test
void simpleQueryShouldWork() {
@@ -92,6 +94,13 @@ class GraphQlWebFluxAutoConfigurationTests {
});
}
@Test
void shouldExposeSchemaEndpoint() {
testWithWebClient((client) -> client.get().uri("/schema").accept(MediaType.ALL).exchange()
.expectStatus().isOk().expectHeader().contentType(MediaType.TEXT_PLAIN).expectBody(String.class)
.value(containsString("type Book")));
}
private void testWithWebClient(Consumer<WebTestClient> consumer) {
this.contextRunner.run((context) -> {
WebTestClient client = WebTestClient.bindToApplicationContext(context).configureClient()

View File

@@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.graphql.servlet;
import graphql.schema.idl.TypeRuntimeWiring;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
@@ -56,8 +57,8 @@ class GraphQlWebMvcAutoConfigurationTests {
AutoConfigurations.of(DispatcherServletAutoConfiguration.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, JacksonAutoConfiguration.class,
GraphQlAutoConfiguration.class, GraphQlWebMvcAutoConfiguration.class))
.withUserConfiguration(DataFetchersConfiguration.class, CustomWebInterceptor.class)
.withPropertyValues("spring.main.web-application-type=servlet");
.withUserConfiguration(DataFetchersConfiguration.class, CustomWebInterceptor.class).withPropertyValues(
"spring.main.web-application-type=servlet", "spring.graphql.schema.printer.enabled=true");
@Test
void simpleQueryShouldWork() {
@@ -99,6 +100,13 @@ class GraphQlWebMvcAutoConfigurationTests {
});
}
@Test
void shouldExposeSchemaEndpoint() {
testWith((mockMvc) -> mockMvc.perform(get("/graphql/schema")).andExpect(status().isOk())
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
.andExpect(content().string(Matchers.containsString("type Book"))));
}
private void testWith(MockMvcConsumer mockMvcConsumer) {
this.contextRunner.run((context) -> {
MediaType mediaType = MediaType.APPLICATION_JSON;