From e798a00d489e7409dc860a07f7a56e2d684ef6cb Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Wed, 17 Jan 2024 17:28:58 +0000 Subject: [PATCH] Refactor how SchemaGenerator is invoked Closes gh-878 --- .../AbstractGraphQlSourceBuilder.java | 25 +++++++++++++++---- ...ultSchemaResourceGraphQlSourceBuilder.java | 20 ++++++++++++--- 2 files changed, 36 insertions(+), 9 deletions(-) diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java index d8e7beeb..57b67dc1 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/AbstractGraphQlSourceBuilder.java @@ -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> private final List instrumentations = new ArrayList<>(); - private Consumer graphQlConfigurers = (builder) -> { - }; + @Nullable + private Consumer graphQlConfigurer; @Override @@ -88,7 +90,8 @@ abstract class AbstractGraphQlSourceBuilder> @Override public B configureGraphQl(Consumer 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> 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> 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. diff --git a/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultSchemaResourceGraphQlSourceBuilder.java b/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultSchemaResourceGraphQlSourceBuilder.java index 65bddfe2..2c946379 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultSchemaResourceGraphQlSourceBuilder.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/execution/DefaultSchemaResourceGraphQlSourceBuilder.java @@ -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 schemaReportConsumer; + @Nullable + private Consumer 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); + } + } + }