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
This commit is contained in:
Brian Clozel 2025-01-20 08:04:27 +01:00
parent 44f5fb2a4f
commit 2dabd11211
2 changed files with 18 additions and 4 deletions

View File

@ -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:
* <ul>
* <li>schema files in the {@link GraphQlProperties configured locations}</li>
* <li>or infrastructure beans such as {@link GraphQlSourceBuilderCustomizer}</li>
* <li>or {@link GraphQlSourceBuilderCustomizer} beans</li>
* <li>or a {@link GraphQlSource} bean</li>
* </ul>
*
* @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));
}

View File

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