Skip to content

Commit

Permalink
GraphQL should not back off when GrapQlSource is present
Browse files Browse the repository at this point in the history
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 spring-projectsgh-33096
  • Loading branch information
bclozel committed Jan 20, 2025
1 parent 44f5fb2 commit 2dabd11
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -34,13 +34,15 @@
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
Expand Down Expand Up @@ -82,6 +84,14 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,14 @@ void shouldConfigureAdditionalSchemaFiles() {
}

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

Expand Down

0 comments on commit 2dabd11

Please sign in to comment.