Browse Source

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
pull/37272/head
Brian Clozel 2 years ago
parent
commit
32b65e85ae
  1. 3
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java
  2. 24
      spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java
  3. 9
      spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfigurationTests.java

3
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java

@ -104,6 +104,9 @@ public class GraphQlAutoConfiguration { @@ -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);
}

24
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/GraphQlProperties.java

@ -79,6 +79,8 @@ public class GraphQlProperties { @@ -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 { @@ -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 { @@ -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 {
/**

9
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; @@ -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; @@ -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; @@ -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 { @@ -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) -> {

Loading…
Cancel
Save