|
15 | 15 | */
|
16 | 16 | package com.introproventures.graphql.jpa.query.boot.autoconfigure;
|
17 | 17 |
|
18 |
| -import javax.persistence.EntityManager; |
| 18 | +import java.util.function.Supplier; |
| 19 | + |
19 | 20 | import javax.persistence.EntityManagerFactory;
|
20 | 21 |
|
| 22 | +import org.springframework.beans.factory.ObjectProvider; |
| 23 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
21 | 24 | import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
22 | 25 | import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
23 | 26 | import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
24 | 27 | import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
|
| 28 | +import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; |
25 | 29 | import org.springframework.context.annotation.Bean;
|
26 | 30 | import org.springframework.context.annotation.Configuration;
|
27 | 31 |
|
28 | 32 | import com.introproventures.graphql.jpa.query.autoconfigure.GraphQLSchemaConfigurer;
|
| 33 | +import com.introproventures.graphql.jpa.query.schema.GraphQLExecutionInputFactory; |
29 | 34 | import com.introproventures.graphql.jpa.query.schema.GraphQLExecutor;
|
| 35 | +import com.introproventures.graphql.jpa.query.schema.GraphQLExecutorContextFactory; |
30 | 36 | import com.introproventures.graphql.jpa.query.schema.GraphQLSchemaBuilder;
|
31 | 37 | import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutor;
|
| 38 | +import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaExecutorContextFactory; |
32 | 39 | import com.introproventures.graphql.jpa.query.schema.impl.GraphQLJpaSchemaBuilder;
|
33 | 40 |
|
34 | 41 | import graphql.GraphQL;
|
| 42 | +import graphql.GraphQLContext; |
| 43 | +import graphql.execution.instrumentation.Instrumentation; |
35 | 44 | import graphql.schema.GraphQLSchema;
|
| 45 | +import graphql.schema.visibility.GraphqlFieldVisibility; |
36 | 46 |
|
37 | 47 | @Configuration
|
38 | 48 | @ConditionalOnClass(GraphQL.class)
|
39 | 49 | @ConditionalOnProperty(name="spring.graphql.jpa.query.enabled", havingValue="true", matchIfMissing=true)
|
| 50 | +@AutoConfigureAfter(HibernateJpaAutoConfiguration.class) |
40 | 51 | public class GraphQLJpaQueryAutoConfiguration {
|
41 | 52 |
|
42 |
| - @Bean |
43 |
| - @ConditionalOnMissingBean |
44 |
| - @ConditionalOnSingleCandidate(EntityManagerFactory.class) |
45 |
| - public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManagerFactory entityManagerFactory) { |
46 |
| - return new GraphQLJpaSchemaBuilder(entityManagerFactory.createEntityManager()); |
47 |
| - } |
48 |
| - |
49 |
| - @Bean |
50 |
| - @ConditionalOnMissingBean |
51 |
| - public GraphQLSchemaConfigurer graphQLJpaQuerySchemaConfigurer(GraphQLSchemaBuilder graphQLSchemaBuilder) { |
52 |
| - |
53 |
| - return (registry) -> { |
54 |
| - registry.register(graphQLSchemaBuilder.build()); |
55 |
| - }; |
56 |
| - } |
57 |
| - |
58 | 53 | @Configuration
|
59 | 54 | public static class DefaultGraphQLJpaQueryConfiguration {
|
| 55 | + |
| 56 | + @Bean |
| 57 | + @ConditionalOnMissingBean |
| 58 | + @ConditionalOnSingleCandidate(EntityManagerFactory.class) |
| 59 | + public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManagerFactory entityManagerFactory) { |
| 60 | + return new GraphQLJpaSchemaBuilder(entityManagerFactory.createEntityManager()); |
| 61 | + } |
60 | 62 |
|
61 | 63 | @Bean
|
62 |
| - @ConditionalOnMissingBean(GraphQLExecutor.class) |
63 |
| - public GraphQLExecutor graphQLExecutor(GraphQLSchema graphQLSchema) { |
64 |
| - return new GraphQLJpaExecutor(graphQLSchema); |
| 64 | + @ConditionalOnMissingBean |
| 65 | + public GraphQLSchemaConfigurer graphQLJpaQuerySchemaConfigurer(GraphQLSchemaBuilder graphQLSchemaBuilder) { |
| 66 | + |
| 67 | + return (registry) -> { |
| 68 | + registry.register(graphQLSchemaBuilder.build()); |
| 69 | + }; |
| 70 | + } |
| 71 | + |
| 72 | + @Bean |
| 73 | + @ConditionalOnMissingBean |
| 74 | + public GraphQLExecutorContextFactory graphQLExecutorContextFactory(ObjectProvider<GraphQLExecutionInputFactory> graphQLExecutionInputFactory, |
| 75 | + ObjectProvider<Supplier<GraphqlFieldVisibility>> graphqlFieldVisibility, |
| 76 | + ObjectProvider<Supplier<Instrumentation>> instrumentation, |
| 77 | + ObjectProvider<Supplier<GraphQLContext>> graphqlContext) { |
| 78 | + GraphQLJpaExecutorContextFactory bean = new GraphQLJpaExecutorContextFactory(); |
| 79 | + |
| 80 | + graphQLExecutionInputFactory.ifAvailable(bean::withExecutionInputFactory); |
| 81 | + graphqlFieldVisibility.ifAvailable(bean::withGraphqlFieldVisibility); |
| 82 | + instrumentation.ifAvailable(bean::withInstrumentation); |
| 83 | + graphqlContext.ifAvailable(bean::withGraphqlContext); |
| 84 | + |
| 85 | + return bean; |
65 | 86 | }
|
66 | 87 |
|
67 | 88 | @Bean
|
68 |
| - @ConditionalOnMissingBean(GraphQLSchemaBuilder.class) |
69 |
| - public GraphQLSchemaBuilder graphQLSchemaBuilder(final EntityManager entityManager) { |
70 |
| - return new GraphQLJpaSchemaBuilder(entityManager); |
| 89 | + @ConditionalOnMissingBean |
| 90 | + public GraphQLExecutor graphQLExecutor(GraphQLSchema graphQLSchema, |
| 91 | + GraphQLExecutorContextFactory graphQLExecutorContextFactory) { |
| 92 | + return new GraphQLJpaExecutor(graphQLSchema, |
| 93 | + graphQLExecutorContextFactory); |
71 | 94 | }
|
72 |
| - |
| 95 | + |
73 | 96 | }
|
74 | 97 | }
|
0 commit comments