From 2dabd11211d937631c4e4651053aeb340dafda5f Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Mon, 20 Jan 2025 08:04:27 +0100 Subject: [PATCH] GraphQL should not back off when GrapQlSource is present Prior to this commit, the GraphQL auto-configuration that defines the infrastructure beans for base support would only be active when: * GraphQL schema files are detected in the configured locations * or if GraphQlSourceBuilderCustomizer beans are present This would allow some "code first" approaches, but not situations where developers contribute their own `GraphQlSource`. This commit ensures that the auto-configuration is processed even if the application only contributes a custom `GraphQlSource` bean. Closes gh-33096 --- .../graphql/DefaultGraphQlSchemaCondition.java | 14 ++++++++++++-- .../graphql/GraphQlAutoConfigurationTests.java | 8 ++++++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/DefaultGraphQlSchemaCondition.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/DefaultGraphQlSchemaCondition.java index 77ead8392c..a8f23e6441 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/DefaultGraphQlSchemaCondition.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/graphql/DefaultGraphQlSchemaCondition.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2023 the original author or authors. + * Copyright 2012-2025 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. @@ -34,13 +34,15 @@ import org.springframework.core.io.Resource; import org.springframework.core.io.support.ResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternUtils; import org.springframework.core.type.AnnotatedTypeMetadata; +import org.springframework.graphql.execution.GraphQlSource; /** * {@link Condition} that checks whether a GraphQL schema has been defined in the * application. This is looking for: * * * @author Brian Clozel @@ -82,6 +84,14 @@ class DefaultGraphQlSchemaCondition extends SpringBootCondition implements Confi else { messages.add((message.didNotFind("GraphQlSourceBuilderCustomizer").atAll())); } + String[] graphqlSourceBeans = beanFactory.getBeanNamesForType(GraphQlSource.class, false, false); + if (graphqlSourceBeans.length != 0) { + match = true; + messages.add(message.found("graphqlSource").items(Arrays.asList(graphqlSourceBeans))); + } + else { + messages.add((message.didNotFind("GraphQlSource").atAll())); + } return new ConditionOutcome(match, ConditionMessage.of(messages)); } 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 ffe7c386f5..b6a99df276 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 @@ -124,10 +124,14 @@ class GraphQlAutoConfigurationTests { } @Test - void shouldBackOffWithCustomGraphQlSource() { + void shouldUseCustomGraphQlSource() { this.contextRunner.withUserConfiguration(CustomGraphQlSourceConfiguration.class).run((context) -> { assertThat(context).getBeanNames(GraphQlSource.class).containsOnly("customGraphQlSource"); - assertThat(context).hasSingleBean(GraphQlProperties.class); + assertThat(context).hasSingleBean(GraphQlProperties.class) + .hasSingleBean(BatchLoaderRegistry.class) + .hasSingleBean(ExecutionGraphQlService.class) + .hasSingleBean(AnnotatedControllerConfigurer.class) + .hasSingleBean(EncodingCursorStrategy.class); }); }