Skip to content

Commit 1b8515a

Browse files
authored
fix: resolve default GraphQLSchemaConfigurer bean race condition in autoconfigure (#312)
1 parent 19a15c8 commit 1b8515a

File tree

3 files changed

+27
-28
lines changed

3 files changed

+27
-28
lines changed

graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaAutoConfiguration.java

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,15 @@
22

33
import graphql.GraphQL;
44
import graphql.schema.GraphQLSchema;
5-
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
66
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
77
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
88
import org.springframework.boot.context.properties.EnableConfigurationProperties;
99
import org.springframework.context.annotation.Bean;
1010
import org.springframework.context.annotation.Configuration;
1111
import org.springframework.context.annotation.PropertySource;
1212
import org.springframework.context.annotation.PropertySources;
13-
import org.springframework.util.CollectionUtils;
1413

15-
import java.util.ArrayList;
1614
import java.util.List;
1715

1816
@Configuration
@@ -22,32 +20,29 @@
2220
@PropertySource("classpath:com/introproventures/graphql/jpa/query/boot/autoconfigure/default.properties"),
2321
@PropertySource(value = "classpath:graphql-jpa-autoconfigure.properties", ignoreResourceNotFound = true)
2422
})
23+
@AutoConfigureAfter(GraphQLSchemaBuilderAutoConfiguration.class)
2524
public class GraphQLSchemaAutoConfiguration {
2625

27-
private final List<GraphQLSchemaConfigurer> graphQLSchemaConfigurers = new ArrayList<>();
28-
29-
@Autowired(required = true)
30-
public void setGraphQLSchemaConfigurers(List<GraphQLSchemaConfigurer> configurers) {
31-
if (!CollectionUtils.isEmpty(configurers)) {
32-
graphQLSchemaConfigurers.addAll(configurers);
33-
}
26+
@Bean
27+
@ConditionalOnMissingBean
28+
public GraphQLShemaRegistration graphQLShemaRegistration() {
29+
return new GraphQLShemaRegistrationImpl();
3430
}
35-
31+
3632
@Bean
3733
@ConditionalOnMissingBean(GraphQLSchema.class)
38-
public GraphQLSchemaFactoryBean graphQLSchemaFactoryBean(GraphQLJpaQueryProperties properties) {
39-
GraphQLShemaRegistrationImpl graphQLShemaRegistration = new GraphQLShemaRegistrationImpl();
40-
34+
public GraphQLSchemaFactoryBean graphQLSchemaFactoryBean(GraphQLJpaQueryProperties properties,
35+
GraphQLShemaRegistration graphQLShemaRegistration,
36+
List<GraphQLSchemaConfigurer> graphQLSchemaConfigurers) {
4137
for (GraphQLSchemaConfigurer configurer : graphQLSchemaConfigurers) {
4238
configurer.configure(graphQLShemaRegistration);
4339
}
44-
40+
4541
return new GraphQLSchemaFactoryBean(graphQLShemaRegistration.getManagedGraphQLSchemas())
4642
.setQueryName(properties.getName())
4743
.setQueryDescription(properties.getDescription());
48-
49-
44+
45+
5046
};
51-
52-
47+
5348
}

graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLSchemaBuilderAutoConfiguration.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
import com.introproventures.graphql.jpa.query.schema.RestrictedKeysProvider;
55
import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
66
import graphql.GraphQL;
7+
import org.springframework.beans.factory.InitializingBean;
78
import org.springframework.beans.factory.ObjectProvider;
89
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
9-
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
1010
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
1111
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
1212
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
@@ -16,11 +16,11 @@
1616
import org.springframework.context.annotation.Configuration;
1717

1818
import javax.persistence.EntityManagerFactory;
19+
import java.util.List;
1920

2021
@Configuration
2122
@ConditionalOnClass({GraphQL.class, GraphQLSchemaBuilder.class})
2223
@ConditionalOnProperty(name="spring.graphql.jpa.query.enabled", havingValue="true", matchIfMissing=true)
23-
@AutoConfigureBefore(GraphQLSchemaAutoConfiguration.class)
2424
@AutoConfigureAfter(HibernateJpaAutoConfiguration.class)
2525
public class GraphQLSchemaBuilderAutoConfiguration {
2626
@Bean
@@ -36,11 +36,15 @@ public GraphQLSchemaBuilder graphQLJpaSchemaBuilder(final EntityManagerFactory e
3636
}
3737

3838
@Bean
39-
@ConditionalOnMissingBean
40-
public GraphQLSchemaConfigurer graphQLJpaQuerySchemaConfigurer(GraphQLSchemaBuilder graphQLSchemaBuilder) {
41-
42-
return (registry) -> {
43-
registry.register(graphQLSchemaBuilder.build());
39+
@ConditionalOnMissingBean(GraphQLSchemaConfigurer.class)
40+
InitializingBean defaultGraphQLSchemaBuilderConfigurer(GraphQLShemaRegistration graphQLShemaRegistration,
41+
GraphQLSchemaBuilder graphQLSchemaBuilder,
42+
List<GraphQLSchemaConfigurer> configurers) {
43+
return () -> {
44+
if (configurers.isEmpty()) {
45+
graphQLShemaRegistration.register(graphQLSchemaBuilder.build());
46+
}
4447
};
4548
}
49+
4650
}

graphql-jpa-query-autoconfigure/src/main/java/com/introproventures/graphql/jpa/query/autoconfigure/GraphQLShemaRegistration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
public interface GraphQLShemaRegistration {
66

7-
public void register(GraphQLSchema graphQLSchema);
8-
7+
void register(GraphQLSchema graphQLSchema);
98

9+
GraphQLSchema[] getManagedGraphQLSchemas();
1010
}

0 commit comments

Comments
 (0)