From 32b65e85aee452e69c6e96820da86885d3761708 Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 8 Sep 2023 14:58:24 +0200 Subject: [PATCH] Add config property for GraphQL Schema Mapping Inspection This commit adds a new `spring.graphql.schema.inspection.enabled` property, which is `true` by default. This property enables the logging at the INFO level of the GraphQL Schema inspection report. During startup, Spring for GraphQL will inspect the schema and report fields and registrations that are unmapped in the application. Closes gh-36252 --- .../graphql/GraphQlAutoConfiguration.java | 3 +++ .../graphql/GraphQlProperties.java | 24 +++++++++++++++++++ .../GraphQlAutoConfigurationTests.java | 9 +++++++ 3 files changed, 36 insertions(+) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java index a42ef90a10d..886e597a522 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java @@ -104,6 +104,9 @@ public class GraphQlAutoConfiguration { .exceptionResolvers(exceptionResolvers.orderedStream().toList()) .subscriptionExceptionResolvers(subscriptionExceptionResolvers.orderedStream().toList()) .instrumentation(instrumentations.orderedStream().toList()); + if (properties.getSchema().getInspection().isEnabled()) { + builder.inspectSchemaMappings(logger::info); + } if (!properties.getSchema().getIntrospection().isEnabled()) { builder.configureRuntimeWiring(this::enableIntrospection); } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java index 1ea766a8be6..68ddfb07a72 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java @@ -79,6 +79,8 @@ public class GraphQlProperties { */ private String[] fileExtensions = new String[] { ".graphqls", ".gqls" }; + private final Inspection inspection = new Inspection(); + private final Introspection introspection = new Introspection(); private final Printer printer = new Printer(); @@ -105,6 +107,10 @@ public class GraphQlProperties { .toArray(String[]::new); } + public Inspection getInspection() { + return this.inspection; + } + public Introspection getIntrospection() { return this.introspection; } @@ -113,6 +119,24 @@ public class GraphQlProperties { return this.printer; } + public static class Inspection { + + /** + * Whether schema should be compared to the application to detect missing + * mappings. + */ + private boolean enabled = true; + + public boolean isEnabled() { + return this.enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + } + public static class Introspection { /** diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java index 100cd9ddda0..3b7182e18ab 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java @@ -30,6 +30,7 @@ import graphql.schema.visibility.DefaultGraphqlFieldVisibility; import graphql.schema.visibility.NoIntrospectionGraphqlFieldVisibility; import org.assertj.core.api.InstanceOfAssertFactories; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.aot.hint.RuntimeHints; import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; @@ -37,6 +38,8 @@ import org.springframework.boot.autoconfigure.AutoConfigurations; import org.springframework.boot.autoconfigure.graphql.GraphQlAutoConfiguration.GraphQlResourcesRuntimeHints; import org.springframework.boot.autoconfigure.task.TaskExecutionAutoConfiguration; import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.boot.test.system.CapturedOutput; +import org.springframework.boot.test.system.OutputCaptureExtension; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ByteArrayResource; @@ -56,6 +59,7 @@ import static org.mockito.Mockito.mock; /** * Tests for {@link GraphQlAutoConfiguration}. */ +@ExtendWith(OutputCaptureExtension.class) class GraphQlAutoConfigurationTests { private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() @@ -158,6 +162,11 @@ class GraphQlAutoConfigurationTests { }); } + @Test + void schemaInspectionShouldBeEnabledByDefault(CapturedOutput output) { + this.contextRunner.run((context) -> assertThat(output).contains("GraphQL schema inspection")); + } + @Test void fieldIntrospectionShouldBeEnabledByDefault() { this.contextRunner.run((context) -> {