Search in sources :

Example 1 with GraphQlSetup

use of org.springframework.graphql.GraphQlSetup 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)

Example 2 with GraphQlSetup

use of org.springframework.graphql.GraphQlSetup in project spring-graphql by spring-projects.

the class QuerydslDataFetcherTests method shouldReactivelyFetchMultipleItems.

@Test
void shouldReactivelyFetchMultipleItems() {
    ReactiveMockRepository mockRepository = mock(ReactiveMockRepository.class);
    Book book1 = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    Book book2 = new Book(53L, "Breaking Bad", new Author(0L, "", "Heisenberg"));
    when(mockRepository.findBy(any(), any())).thenReturn(Flux.just(book1, book2));
    Consumer<GraphQlSetup> tester = setup -> {
        Mono<WebOutput> outputMono = setup.toWebGraphQlHandler().handleRequest(input("{ books {name}}"));
        List<String> names = GraphQlResponse.from(outputMono).toList("books", Book.class).stream().map(Book::getName).collect(Collectors.toList());
        assertThat(names).containsExactlyInAnyOrder("Breaking Bad", "Hitchhiker's Guide to the Galaxy");
    };
    // explicit wiring
    tester.accept(graphQlSetup("books", QuerydslDataFetcher.builder(mockRepository).many()));
    // auto registration
    tester.accept(graphQlSetup(mockRepository));
}
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) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) Author(org.springframework.graphql.Author) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 3 with GraphQlSetup

use of org.springframework.graphql.GraphQlSetup in project spring-graphql by spring-projects.

the class QuerydslDataFetcherTests method shouldFetchSingleItems.

@Test
void shouldFetchSingleItems() {
    Book book = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    mockRepository.save(book);
    Consumer<GraphQlSetup> tester = setup -> {
        Mono<WebOutput> output = setup.toWebGraphQlHandler().handleRequest(input("{ bookById(id: 42) {name}}"));
        Book actualBook = GraphQlResponse.from(output).toEntity("bookById", Book.class);
        assertThat(actualBook.getName()).isEqualTo(book.getName());
    };
    // explicit wiring
    tester.accept(graphQlSetup("bookById", QuerydslDataFetcher.builder(mockRepository).single()));
    // auto registration
    tester.accept(graphQlSetup(mockRepository));
}
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) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) Author(org.springframework.graphql.Author) Test(org.junit.jupiter.api.Test)

Example 4 with GraphQlSetup

use of org.springframework.graphql.GraphQlSetup in project spring-graphql by spring-projects.

the class QuerydslDataFetcherTests method shouldFetchMultipleItems.

@Test
void shouldFetchMultipleItems() {
    Book book1 = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    Book book2 = new Book(53L, "Breaking Bad", new Author(0L, "", "Heisenberg"));
    mockRepository.saveAll(Arrays.asList(book1, book2));
    Consumer<GraphQlSetup> tester = graphQlSetup -> {
        Mono<WebOutput> output = graphQlSetup.toWebGraphQlHandler().handleRequest(input("{ books {name}}"));
        List<String> names = GraphQlResponse.from(output).toList("books", Book.class).stream().map(Book::getName).collect(Collectors.toList());
        assertThat(names).containsExactlyInAnyOrder(book1.getName(), book2.getName());
    };
    // explicit wiring
    tester.accept(graphQlSetup("books", QuerydslDataFetcher.builder(mockRepository).many()));
    // auto registration
    tester.accept(graphQlSetup(mockRepository));
}
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) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) Author(org.springframework.graphql.Author) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 5 with GraphQlSetup

use of org.springframework.graphql.GraphQlSetup in project spring-graphql by spring-projects.

the class QuerydslDataFetcherTests method shouldApplyCustomizerInRepository.

@Test
void shouldApplyCustomizerInRepository() {
    MockWithCustomizerRepository repository = repositoryFactory.getRepository(MockWithCustomizerRepository.class);
    Book book1 = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    Book book2 = new Book(53L, "Breaking Bad", new Author(0L, "", "Heisenberg"));
    repository.saveAll(Arrays.asList(book1, book2));
    Consumer<GraphQlSetup> tester = graphQlSetup -> {
        Mono<WebOutput> output = graphQlSetup.toWebGraphQlHandler().handleRequest(input("{ books {name}}"));
        List<String> names = GraphQlResponse.from(output).toList("books", Book.class).stream().map(Book::getName).collect(Collectors.toList());
        assertThat(names).containsExactlyInAnyOrder(book1.getName(), book2.getName());
    };
    // explicit wiring
    tester.accept(graphQlSetup("books", QuerydslDataFetcher.builder(mockRepository).many()));
    // auto registration
    tester.accept(graphQlSetup(mockRepository));
}
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) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) Author(org.springframework.graphql.Author) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

Predicate (com.querydsl.core.types.Predicate)6 DataFetcher (graphql.schema.DataFetcher)6 URI (java.net.URI)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 List (java.util.List)6 Optional (java.util.Optional)6 Consumer (java.util.function.Consumer)6 Collectors (java.util.stream.Collectors)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 Test (org.junit.jupiter.api.Test)6 ArgumentCaptor (org.mockito.ArgumentCaptor)6 Mockito.any (org.mockito.Mockito.any)6 Mockito.mock (org.mockito.Mockito.mock)6 Mockito.verify (org.mockito.Mockito.verify)6 Mockito.when (org.mockito.Mockito.when)6 Value (org.springframework.beans.factory.annotation.Value)6 KeyValueTemplate (org.springframework.data.keyvalue.core.KeyValueTemplate)6 KeyValueRepositoryFactory (org.springframework.data.keyvalue.repository.support.KeyValueRepositoryFactory)6 MapKeyValueAdapter (org.springframework.data.map.MapKeyValueAdapter)6