Search in sources :

Example 1 with QuerydslBinderCustomizer

use of org.springframework.data.querydsl.binding.QuerydslBinderCustomizer in project spring-data-commons by spring-projects.

the class QuerydslPredicateArgumentResolverSupport method getPredicate.

Predicate getPredicate(MethodParameter parameter, MultiValueMap<String, String> queryParameters) {
    MergedAnnotations annotations = MergedAnnotations.from(parameter.getParameter());
    MergedAnnotation<QuerydslPredicate> predicateAnnotation = annotations.get(QuerydslPredicate.class);
    TypeInformation<?> domainType = extractTypeInfo(parameter, predicateAnnotation).getRequiredActualType();
    Optional<Class<? extends QuerydslBinderCustomizer<?>>> bindingsAnnotation = // 
    predicateAnnotation.getValue("bindings").map(CastUtils::cast);
    QuerydslBindings bindings = // 
    bindingsAnnotation.map(// 
    it -> bindingsFactory.createBindingsFor(domainType, it)).orElseGet(() -> bindingsFactory.createBindingsFor(domainType));
    return predicateBuilder.getPredicate(domainType, queryParameters, bindings);
}
Also used : HandlerMethodArgumentResolver(org.springframework.web.method.support.HandlerMethodArgumentResolver) QuerydslBinderCustomizer(org.springframework.data.querydsl.binding.QuerydslBinderCustomizer) MultiValueMap(org.springframework.util.MultiValueMap) TypeInformation(org.springframework.data.util.TypeInformation) MergedAnnotation(org.springframework.core.annotation.MergedAnnotation) QuerydslPredicateBuilder(org.springframework.data.querydsl.binding.QuerydslPredicateBuilder) ClassTypeInformation(org.springframework.data.util.ClassTypeInformation) MergedAnnotations(org.springframework.core.annotation.MergedAnnotations) MethodParameter(org.springframework.core.MethodParameter) Optional(java.util.Optional) Nullable(org.springframework.lang.Nullable) ResolvableType(org.springframework.core.ResolvableType) QuerydslBindingsFactory(org.springframework.data.querydsl.binding.QuerydslBindingsFactory) Predicate(com.querydsl.core.types.Predicate) Method(java.lang.reflect.Method) ConversionService(org.springframework.core.convert.ConversionService) QuerydslPredicate(org.springframework.data.querydsl.binding.QuerydslPredicate) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) CastUtils(org.springframework.data.util.CastUtils) Assert(org.springframework.util.Assert) QuerydslBinderCustomizer(org.springframework.data.querydsl.binding.QuerydslBinderCustomizer) QuerydslPredicate(org.springframework.data.querydsl.binding.QuerydslPredicate) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) MergedAnnotations(org.springframework.core.annotation.MergedAnnotations) CastUtils(org.springframework.data.util.CastUtils)

Example 2 with QuerydslBinderCustomizer

use of org.springframework.data.querydsl.binding.QuerydslBinderCustomizer in project spring-graphql by spring-projects.

the class QuerydslDataFetcherTests method shouldApplyCustomizerViaBuilder.

@Test
void shouldApplyCustomizerViaBuilder() {
    MockRepository mockRepository = mock(MockRepository.class);
    DataFetcher<Iterable<Book>> fetcher = QuerydslDataFetcher.builder(mockRepository).customizer((QuerydslBinderCustomizer<QBook>) (bindings, book) -> bindings.bind(book.name).firstOptional((path, value) -> value.map(path::startsWith))).many();
    graphQlSetup("books", fetcher).toWebGraphQlHandler().handleRequest(input("{ books(name: \"H\", author: \"Doug\") {name}}")).block();
    ArgumentCaptor<Predicate> predicateCaptor = ArgumentCaptor.forClass(Predicate.class);
    verify(mockRepository).findBy(predicateCaptor.capture(), any());
    Predicate predicate = predicateCaptor.getValue();
    assertThat(predicate).isEqualTo(QBook.book.name.startsWith("H").and(QBook.book.author.eq("Doug")));
}
Also used : Arrays(java.util.Arrays) GraphQlRepository(org.springframework.graphql.data.GraphQlRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BookSource(org.springframework.graphql.BookSource) GraphQlSetup(org.springframework.graphql.GraphQlSetup) WebOutput(org.springframework.graphql.web.WebOutput) CrudRepository(org.springframework.data.repository.CrudRepository) ReactiveQuerydslPredicateExecutor(org.springframework.data.querydsl.ReactiveQuerydslPredicateExecutor) Value(org.springframework.beans.factory.annotation.Value) Author(org.springframework.graphql.Author) ArgumentCaptor(org.mockito.ArgumentCaptor) Repository(org.springframework.data.repository.Repository) KeyValueRepositoryFactory(org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory) DataFetcher(graphql.schema.DataFetcher) Nullable(org.springframework.lang.Nullable) URI(java.net.URI) GraphQlResponse(org.springframework.graphql.GraphQlResponse) QuerydslPredicateExecutor(org.springframework.data.querydsl.QuerydslPredicateExecutor) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) RuntimeWiringConfigurer(org.springframework.graphql.execution.RuntimeWiringConfigurer) QuerydslBinderCustomizer(org.springframework.data.querydsl.binding.QuerydslBinderCustomizer) HttpHeaders(org.springframework.http.HttpHeaders) Mono(reactor.core.publisher.Mono) MapKeyValueAdapter(org.springframework.data.map.MapKeyValueAdapter) Mockito.when(org.mockito.Mockito.when) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) List(java.util.List) WebInput(org.springframework.graphql.web.WebInput) KeyValueTemplate(org.springframework.data.keyvalue.core.KeyValueTemplate) Optional(java.util.Optional) Predicate(com.querydsl.core.types.Predicate) Mockito.any(org.mockito.Mockito.any) Collections(java.util.Collections) QuerydslBindings(org.springframework.data.querydsl.binding.QuerydslBindings) Mockito.mock(org.mockito.Mockito.mock) QuerydslBinderCustomizer(org.springframework.data.querydsl.binding.QuerydslBinderCustomizer) Predicate(com.querydsl.core.types.Predicate) Test(org.junit.jupiter.api.Test)

Aggregations

Predicate (com.querydsl.core.types.Predicate)2 Optional (java.util.Optional)2 QuerydslBinderCustomizer (org.springframework.data.querydsl.binding.QuerydslBinderCustomizer)2 QuerydslBindings (org.springframework.data.querydsl.binding.QuerydslBindings)2 Nullable (org.springframework.lang.Nullable)2 DataFetcher (graphql.schema.DataFetcher)1 Method (java.lang.reflect.Method)1 URI (java.net.URI)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Test (org.junit.jupiter.api.Test)1 ArgumentCaptor (org.mockito.ArgumentCaptor)1 Mockito.any (org.mockito.Mockito.any)1 Mockito.mock (org.mockito.Mockito.mock)1 Mockito.verify (org.mockito.Mockito.verify)1 Mockito.when (org.mockito.Mockito.when)1