From a514704b3f593ddecf7d6d457d50b1fdaae5f30e Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 10 Dec 2021 10:24:07 +0000 Subject: [PATCH] Add missing autoconfig for Query By Example See gh-191 --- ...ebFluxQueryByExampleAutoConfiguration.java | 70 +++++++++++++++++ ...aphQlWebFluxQuerydslAutoConfiguration.java | 2 +- ...WebMvcQueryByExampleAutoConfiguration.java | 75 +++++++++++++++++++ 3 files changed, 146 insertions(+), 1 deletion(-) create mode 100644 graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebFluxQueryByExampleAutoConfiguration.java create mode 100644 graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebMvcQueryByExampleAutoConfiguration.java diff --git a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebFluxQueryByExampleAutoConfiguration.java b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebFluxQueryByExampleAutoConfiguration.java new file mode 100644 index 00000000..0e2be46d --- /dev/null +++ b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebFluxQueryByExampleAutoConfiguration.java @@ -0,0 +1,70 @@ +/* + * Copyright 2002-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.graphql.boot.data; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import graphql.GraphQL; + +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor; +import org.springframework.graphql.boot.GraphQlAutoConfiguration; +import org.springframework.graphql.boot.GraphQlSourceBuilderCustomizer; +import org.springframework.graphql.data.query.QueryByExampleDataFetcher; +import org.springframework.graphql.execution.GraphQlSource; + +/** + * {@link EnableAutoConfiguration Auto-configuration} that creates a + * {@link GraphQlSourceBuilderCustomizer}s to detect Spring Data repositories + * with Query By Example support and register them as {@code DataFetcher}s for any + * queries with a matching return type. + * + * @author Rossen Stoyanchev + * @since 1.0.0 + * @see QueryByExampleDataFetcher#autoRegistrationTypeVisitor(List, List) + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) +@ConditionalOnClass({GraphQL.class, ReactiveQueryByExampleExecutor.class }) +@ConditionalOnBean(GraphQlSource.class) +@AutoConfigureAfter(GraphQlAutoConfiguration.class) +public class GraphQlWebFluxQueryByExampleAutoConfiguration { + + @Bean + public GraphQlSourceBuilderCustomizer reactiveQueryByExampleRegistrar( + ObjectProvider> executorsProvider) { + + return builder -> { + List> executors = + executorsProvider.stream().collect(Collectors.toList()); + + if (!executors.isEmpty()) { + builder.typeVisitors(Collections.singletonList( + QueryByExampleDataFetcher.autoRegistrationTypeVisitor(Collections.emptyList(), executors))); + } + }; + } + +} diff --git a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebFluxQuerydslAutoConfiguration.java b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebFluxQuerydslAutoConfiguration.java index 34e1bf79..71323cc4 100644 --- a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebFluxQuerydslAutoConfiguration.java +++ b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebFluxQuerydslAutoConfiguration.java @@ -49,7 +49,7 @@ import org.springframework.graphql.execution.GraphQlSource; */ @Configuration(proxyBeanMethods = false) @ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.REACTIVE) -@ConditionalOnClass({GraphQL.class, QuerydslPredicateExecutor.class }) +@ConditionalOnClass({GraphQL.class, ReactiveQuerydslPredicateExecutor.class }) @ConditionalOnBean(GraphQlSource.class) @AutoConfigureAfter(GraphQlAutoConfiguration.class) public class GraphQlWebFluxQuerydslAutoConfiguration { diff --git a/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebMvcQueryByExampleAutoConfiguration.java b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebMvcQueryByExampleAutoConfiguration.java new file mode 100644 index 00000000..c9cc8dbb --- /dev/null +++ b/graphql-spring-boot-starter/src/main/java/org/springframework/graphql/boot/data/GraphQlWebMvcQueryByExampleAutoConfiguration.java @@ -0,0 +1,75 @@ +/* + * Copyright 2002-2021 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. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.graphql.boot.data; + +import java.util.Collections; +import java.util.List; +import java.util.stream.Collectors; + +import graphql.GraphQL; + +import org.springframework.beans.factory.ObjectProvider; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.repository.query.QueryByExampleExecutor; +import org.springframework.data.repository.query.ReactiveQueryByExampleExecutor; +import org.springframework.graphql.boot.GraphQlAutoConfiguration; +import org.springframework.graphql.boot.GraphQlSourceBuilderCustomizer; +import org.springframework.graphql.data.query.QueryByExampleDataFetcher; +import org.springframework.graphql.execution.GraphQlSource; + +/** + * {@link EnableAutoConfiguration Auto-configuration} that creates a + * {@link GraphQlSourceBuilderCustomizer}s to detect Spring Data repositories + * with Query By Example support and register them as {@code DataFetcher}s for + * any queries with a matching return type. + * + * @author Rossen Stoyanchev + * @since 1.0.0 + * @see QueryByExampleDataFetcher#autoRegistrationTypeVisitor(List, List) + */ +@Configuration(proxyBeanMethods = false) +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) +@ConditionalOnClass({GraphQL.class, QueryByExampleExecutor.class }) +@ConditionalOnBean(GraphQlSource.class) +@AutoConfigureAfter(GraphQlAutoConfiguration.class) +public class GraphQlWebMvcQueryByExampleAutoConfiguration { + + @Bean + public GraphQlSourceBuilderCustomizer queryByExampleRegistrar( + ObjectProvider> executorsProvider, + ObjectProvider> reactiveExecutorsProvider) { + + return builder -> { + List> executors = + executorsProvider.stream().collect(Collectors.toList()); + + List> reactiveExecutors = + reactiveExecutorsProvider.stream().collect(Collectors.toList()); + + if (!executors.isEmpty()) { + builder.typeVisitors(Collections.singletonList( + QueryByExampleDataFetcher.autoRegistrationTypeVisitor(executors, reactiveExecutors))); + } + }; + } + +}