Skip to content

Commit 7ba34cb

Browse files
feat(graphql-java-kickstart#5): the default GraphQLAnnotations instance is exposed as bean
If no custom implementation is provided.
1 parent 7b77cde commit 7ba34cb

File tree

2 files changed

+38
-10
lines changed

2 files changed

+38
-10
lines changed

graphql-kickstart-spring-boot-autoconfigure-graphql-annotations/src/main/java/graphql/kickstart/graphql/annotations/GraphQLAnnotationsAutoConfiguration.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.reflections.Reflections;
2222
import org.reflections.ReflectionsException;
2323
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2425
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2526
import org.springframework.context.annotation.Bean;
2627
import org.springframework.context.annotation.Configuration;
@@ -32,7 +33,6 @@
3233
import java.util.Set;
3334

3435
import static graphql.annotations.AnnotationsSchemaCreator.newAnnotationsSchema;
35-
import static java.util.Objects.isNull;
3636

3737
@Configuration
3838
@AutoConfigureBefore({GraphQLJavaToolsAutoConfiguration.class})
@@ -43,12 +43,17 @@ public class GraphQLAnnotationsAutoConfiguration {
4343

4444
private final GraphQLAnnotationsProperties graphQLAnnotationsProperties;
4545
private final Optional<Relay> relay;
46-
private final Optional<GraphQLAnnotations> customAnnotationProcessor;
4746
private final List<TypeFunction> typeFunctions;
4847
private final List<GraphQLScalarType> customScalarTypes;
4948

5049
@Bean
51-
public GraphQLSchema graphQLSchema() {
50+
@ConditionalOnMissingBean
51+
public GraphQLAnnotations graphQLAnnotations() {
52+
return new GraphQLAnnotations();
53+
}
54+
55+
@Bean
56+
public GraphQLSchema graphQLSchema(final GraphQLAnnotations graphQLAnnotations) {
5257
log.info("Using GraphQL Annotations library to build the schema. Schema definition files will be ignored.");
5358
log.info("GraphQL classes are searched in the following package (including subpackages): {}",
5459
graphQLAnnotationsProperties.getBasePackage());
@@ -73,14 +78,12 @@ public GraphQLSchema graphQLSchema() {
7378
if (!customScalarTypes.isEmpty()) {
7479
builder.typeFunction(new GraphQLScalarTypeFunction(customScalarTypes));
7580
}
76-
customAnnotationProcessor.ifPresent(graphQLAnnotations -> {
77-
log.info("Registering custom GraphQL annotations processor {}", graphQLAnnotations.getClass());
78-
builder.setAnnotationsProcessor(graphQLAnnotations);
79-
});
80-
if (isNull(builder.getGraphQLAnnotations())) {
81-
// before setting a relay, we have to set the annotation processor, otherwise a NPE will occur
82-
builder.setAnnotationsProcessor(new GraphQLAnnotations());
81+
if (graphQLAnnotations.getClass().equals(GraphQLAnnotations.class)) {
82+
log.info("Using default GraphQL Annotation processor.");
83+
} else {
84+
log.info("Using custom annotation process of type {}", graphQLAnnotations.getClass());
8385
}
86+
builder.setAnnotationsProcessor(graphQLAnnotations);
8487
relay.ifPresent(r -> {
8588
log.info("Registering relay {}", r.getClass());
8689
builder.setRelay(r);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package graphql.kickstart.graphql.annotations;
2+
3+
import graphql.annotations.processor.GraphQLAnnotations;
4+
import org.junit.jupiter.api.DisplayName;
5+
import org.junit.jupiter.api.Test;
6+
import org.springframework.beans.factory.annotation.Autowired;
7+
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.context.ApplicationContext;
9+
import org.springframework.test.context.ActiveProfiles;
10+
11+
import static org.assertj.core.api.Assertions.assertThatCode;
12+
13+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
14+
@ActiveProfiles({"test", "query-test"})
15+
public class GraphQLAnnotationsBeanTest {
16+
17+
@Autowired
18+
private ApplicationContext applicationContext;
19+
20+
@Test
21+
@DisplayName("Should expose the GraphQL Annotations bean.")
22+
void testThatGraphQLAnnotationsBeanExists() {
23+
assertThatCode(() -> applicationContext.getBean(GraphQLAnnotations.class)).doesNotThrowAnyException();
24+
}
25+
}

0 commit comments

Comments
 (0)