Refactor how SchemaGenerator is invoked

Closes gh-878
This commit is contained in:
rstoyanchev
2024-01-17 17:28:58 +00:00
parent e054fe13cf
commit e798a00d48
2 changed files with 36 additions and 9 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -31,6 +31,8 @@ import graphql.schema.GraphQLTypeVisitor;
import graphql.schema.SchemaTransformer;
import graphql.schema.SchemaTraverser;
import org.springframework.lang.Nullable;
/**
* Implementation of {@link GraphQlSource.Builder} that leaves it to subclasses
@@ -52,8 +54,8 @@ abstract class AbstractGraphQlSourceBuilder<B extends GraphQlSource.Builder<B>>
private final List<Instrumentation> instrumentations = new ArrayList<>();
private Consumer<GraphQL.Builder> graphQlConfigurers = (builder) -> {
};
@Nullable
private Consumer<GraphQL.Builder> graphQlConfigurer;
@Override
@@ -88,7 +90,8 @@ abstract class AbstractGraphQlSourceBuilder<B extends GraphQlSource.Builder<B>>
@Override
public B configureGraphQl(Consumer<GraphQL.Builder> configurer) {
this.graphQlConfigurers = this.graphQlConfigurers.andThen(configurer);
this.graphQlConfigurer = (this.graphQlConfigurer != null ?
this.graphQlConfigurer.andThen(configurer) : configurer);
return self();
}
@@ -112,7 +115,7 @@ abstract class AbstractGraphQlSourceBuilder<B extends GraphQlSource.Builder<B>>
builder = builder.instrumentation(new ChainedInstrumentation(this.instrumentations));
}
this.graphQlConfigurers.accept(builder);
applyGraphQlConfigurers(builder);
return new FixedGraphQlSource(builder.build(), schema);
}
@@ -147,6 +150,18 @@ abstract class AbstractGraphQlSourceBuilder<B extends GraphQlSource.Builder<B>>
return schema.transformWithoutTypes(builder -> builder.codeRegistry(outputCodeRegistry));
}
/**
* Protected method to apply the
* {@link #configureGraphQl(Consumer) configured graphQlConfigurer}'s.
* Subclasses can use this to customize {@link GraphQL.Builder} further.
* @since 1.2.5
*/
protected void applyGraphQlConfigurers(GraphQL.Builder builder) {
if (this.graphQlConfigurer != null) {
this.graphQlConfigurer.accept(builder);
}
}
/**
* {@link GraphQlSource} with fixed {@link GraphQL} and {@link GraphQLSchema} instances.

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -27,6 +27,7 @@ import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import graphql.GraphQL;
import graphql.language.InterfaceTypeDefinition;
import graphql.language.UnionTypeDefinition;
import graphql.schema.GraphQLSchema;
@@ -75,6 +76,9 @@ final class DefaultSchemaResourceGraphQlSourceBuilder
@Nullable
private Consumer<SchemaReport> schemaReportConsumer;
@Nullable
private Consumer<GraphQLSchema> schemaReportRunner;
@Override
public DefaultSchemaResourceGraphQlSourceBuilder schemaResources(Resource... resources) {
@@ -147,11 +151,10 @@ final class DefaultSchemaResourceGraphQlSourceBuilder
// visitors may transform the schema, for example to add Connection types.
if (this.schemaReportConsumer != null) {
configureGraphQl(builder -> {
GraphQLSchema schema = builder.build().getGraphQLSchema();
this.schemaReportRunner = schema -> {
SchemaReport report = SchemaMappingInspector.inspect(schema, runtimeWiring);
this.schemaReportConsumer.accept(report);
});
};
}
return (this.schemaFactory != null ?
@@ -196,4 +199,13 @@ final class DefaultSchemaResourceGraphQlSourceBuilder
return (this.typeResolver != null ? this.typeResolver : new ClassNameTypeResolver());
}
@Override
protected void applyGraphQlConfigurers(GraphQL.Builder builder) {
super.applyGraphQlConfigurers(builder);
if (this.schemaReportRunner != null) {
GraphQLSchema schema = builder.build().getGraphQLSchema();
this.schemaReportRunner.accept(schema);
}
}
}