|
| 1 | +package graphql.kickstart.spring.rsocket.boot; |
| 2 | + |
| 3 | +import static graphql.kickstart.execution.GraphQLObjectMapper.newBuilder; |
| 4 | + |
| 5 | +import graphql.execution.instrumentation.dataloader.DataLoaderDispatcherInstrumentationOptions; |
| 6 | +import graphql.kickstart.execution.BatchedDataLoaderGraphQLBuilder; |
| 7 | +import graphql.kickstart.execution.GraphQLInvoker; |
| 8 | +import graphql.kickstart.execution.GraphQLObjectMapper; |
| 9 | +import graphql.kickstart.execution.config.DefaultGraphQLSchemaProvider; |
| 10 | +import graphql.kickstart.execution.config.GraphQLBuilder; |
| 11 | +import graphql.kickstart.execution.config.GraphQLSchemaProvider; |
| 12 | +import graphql.kickstart.execution.config.ObjectMapperProvider; |
| 13 | +import graphql.kickstart.spring.error.ErrorHandlerSupplier; |
| 14 | +import graphql.kickstart.spring.error.GraphQLErrorStartupListener; |
| 15 | +import graphql.kickstart.spring.rsocket.DefaultGraphQLSpringRSocketRootObjectBuilder; |
| 16 | +import graphql.kickstart.spring.rsocket.DefaultGraphQlSpringRSocketContextBuilder; |
| 17 | +import graphql.kickstart.spring.rsocket.DefaultGraphQlSpringRSocketInvocationInputFactory; |
| 18 | +import graphql.kickstart.spring.rsocket.GraphQLSpringRSocketRootObjectBuilder; |
| 19 | +import graphql.kickstart.spring.rsocket.GraphQlMessageHandler; |
| 20 | +import graphql.kickstart.spring.rsocket.GraphQlSpringRSocketContextBuilder; |
| 21 | +import graphql.kickstart.spring.rsocket.GraphQlSpringRSocketInvocationInputFactory; |
| 22 | +import graphql.kickstart.tools.boot.GraphQLJavaToolsAutoConfiguration; |
| 23 | +import graphql.schema.GraphQLSchema; |
| 24 | +import java.util.function.Supplier; |
| 25 | +import org.springframework.beans.factory.ObjectProvider; |
| 26 | +import org.springframework.beans.factory.annotation.Autowired; |
| 27 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 28 | +import org.springframework.boot.autoconfigure.AutoConfigureBefore; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 31 | +import org.springframework.boot.autoconfigure.rsocket.RSocketServerAutoConfiguration; |
| 32 | +import org.springframework.context.annotation.Bean; |
| 33 | +import org.springframework.context.annotation.Configuration; |
| 34 | +import org.springframework.context.annotation.Import; |
| 35 | +import org.springframework.context.annotation.PropertySource; |
| 36 | + |
| 37 | +@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection") |
| 38 | +@Configuration |
| 39 | +@ConditionalOnBean(GraphQLSchema.class) |
| 40 | +@AutoConfigureAfter(GraphQLJavaToolsAutoConfiguration.class) |
| 41 | +@AutoConfigureBefore(RSocketServerAutoConfiguration.class) |
| 42 | +@Import(GraphQlMessageHandler.class) |
| 43 | +@PropertySource("classpath:graphql.properties") |
| 44 | +public class GraphQlSpringRSocketAutoConfiguration { |
| 45 | + |
| 46 | + @Bean |
| 47 | + @ConditionalOnMissingBean |
| 48 | + public ErrorHandlerSupplier errorHandlerSupplier() { |
| 49 | + return new ErrorHandlerSupplier(null); |
| 50 | + } |
| 51 | + |
| 52 | + @Bean |
| 53 | + public GraphQLErrorStartupListener graphQLErrorStartupListener( |
| 54 | + ErrorHandlerSupplier errorHandlerSupplier) { |
| 55 | + return new GraphQLErrorStartupListener(errorHandlerSupplier, true); |
| 56 | + } |
| 57 | + |
| 58 | + @Bean |
| 59 | + @ConditionalOnMissingBean |
| 60 | + public GraphQLObjectMapper graphQLObjectMapper( |
| 61 | + ObjectProvider<ObjectMapperProvider> provider, ErrorHandlerSupplier errorHandlerSupplier) { |
| 62 | + GraphQLObjectMapper.Builder builder = newBuilder(); |
| 63 | + builder.withGraphQLErrorHandler(errorHandlerSupplier); |
| 64 | + provider.ifAvailable(builder::withObjectMapperProvider); |
| 65 | + return builder.build(); |
| 66 | + } |
| 67 | + |
| 68 | + @Bean |
| 69 | + @ConditionalOnMissingBean |
| 70 | + public GraphQlSpringRSocketContextBuilder graphQLSpringWebfluxContextBuilder() { |
| 71 | + return new DefaultGraphQlSpringRSocketContextBuilder(); |
| 72 | + } |
| 73 | + |
| 74 | + @Bean |
| 75 | + @ConditionalOnMissingBean |
| 76 | + public GraphQLSpringRSocketRootObjectBuilder graphQLSpringWebfluxRootObjectBuilder() { |
| 77 | + return new DefaultGraphQLSpringRSocketRootObjectBuilder(); |
| 78 | + } |
| 79 | + |
| 80 | + @Bean |
| 81 | + @ConditionalOnMissingBean |
| 82 | + public GraphQLSchemaProvider graphQLSchemaProvider(GraphQLSchema schema) { |
| 83 | + return new DefaultGraphQLSchemaProvider(schema); |
| 84 | + } |
| 85 | + |
| 86 | + @Bean |
| 87 | + @ConditionalOnMissingBean |
| 88 | + public GraphQlSpringRSocketInvocationInputFactory graphQLSpringInvocationInputFactory( |
| 89 | + GraphQLSchemaProvider graphQLSchemaProvider, |
| 90 | + @Autowired(required = false) GraphQlSpringRSocketContextBuilder contextBuilder, |
| 91 | + @Autowired(required = false) GraphQLSpringRSocketRootObjectBuilder rootObjectBuilder) { |
| 92 | + return new DefaultGraphQlSpringRSocketInvocationInputFactory( |
| 93 | + graphQLSchemaProvider, contextBuilder, rootObjectBuilder); |
| 94 | + } |
| 95 | + |
| 96 | + @Bean |
| 97 | + @ConditionalOnMissingBean |
| 98 | + public GraphQLBuilder graphQLBuilder() { |
| 99 | + return new GraphQLBuilder(); |
| 100 | + } |
| 101 | + |
| 102 | + @Bean |
| 103 | + @ConditionalOnMissingBean |
| 104 | + public BatchedDataLoaderGraphQLBuilder batchedDataLoaderGraphQLBuilder( |
| 105 | + @Autowired(required = false) |
| 106 | + Supplier<DataLoaderDispatcherInstrumentationOptions> optionsSupplier) { |
| 107 | + return new BatchedDataLoaderGraphQLBuilder(optionsSupplier); |
| 108 | + } |
| 109 | + |
| 110 | + @Bean |
| 111 | + @ConditionalOnMissingBean |
| 112 | + public GraphQLInvoker graphQLInvoker( |
| 113 | + GraphQLBuilder graphQLBuilder, |
| 114 | + BatchedDataLoaderGraphQLBuilder batchedDataLoaderGraphQLBuilder) { |
| 115 | + return new GraphQLInvoker(graphQLBuilder, batchedDataLoaderGraphQLBuilder); |
| 116 | + } |
| 117 | +} |
0 commit comments