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
This commit is contained in:
Brian Clozel 2023-09-08 14:58:24 +02:00
parent 7802ca172e
commit 32b65e85ae
3 changed files with 36 additions and 0 deletions

View File

@ -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);
}

View File

@ -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 {
/**

View File

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