Search in sources :

Example 11 with WebOutput

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

the class QuerydslDataFetcherTests method shouldFetchSingleItemsWithDtoProjection.

@Test
void shouldFetchSingleItemsWithDtoProjection() {
    Book book = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    mockRepository.save(book);
    DataFetcher<?> fetcher = QuerydslDataFetcher.builder(mockRepository).projectAs(BookDto.class).single();
    WebGraphQlHandler handler = graphQlSetup("bookById", fetcher).toWebGraphQlHandler();
    Mono<WebOutput> outputMono = handler.handleRequest(input("{ bookById(id: 42) {name}}"));
    Book actualBook = GraphQlResponse.from(outputMono).toEntity("bookById", Book.class);
    assertThat(actualBook.getName()).isEqualTo("The book is: Hitchhiker's Guide to the Galaxy");
}
Also used : WebOutput(org.springframework.graphql.web.WebOutput) Author(org.springframework.graphql.Author) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) Test(org.junit.jupiter.api.Test)

Example 12 with WebOutput

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

the class QuerydslDataFetcherTests method shouldReactivelyFetchSingleItems.

@Test
void shouldReactivelyFetchSingleItems() {
    ReactiveMockRepository mockRepository = mock(ReactiveMockRepository.class);
    Book book = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    when(mockRepository.findBy(any(), any())).thenReturn(Mono.just(book));
    Consumer<GraphQlSetup> tester = setup -> {
        Mono<WebOutput> outputMono = setup.toWebGraphQlHandler().handleRequest(input("{ bookById(id: 1) {name}}"));
        Book actualBook = GraphQlResponse.from(outputMono).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 13 with WebOutput

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

the class QuerydslDataFetcherTests method shouldFavorExplicitWiring.

@Test
void shouldFavorExplicitWiring() {
    MockRepository mockRepository = mock(MockRepository.class);
    Book book = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    when(mockRepository.findBy(any(), any())).thenReturn(Optional.of(book));
    // 1) Automatic registration only
    WebGraphQlHandler handler = graphQlSetup(mockRepository).toWebGraphQlHandler();
    Mono<WebOutput> outputMono = handler.handleRequest(input("{ bookById(id: 1) {name}}"));
    Book actualBook = GraphQlResponse.from(outputMono).toEntity("bookById", Book.class);
    assertThat(actualBook.getName()).isEqualTo("Hitchhiker's Guide to the Galaxy");
    // 2) Automatic registration and explicit wiring
    handler = graphQlSetup(mockRepository).queryFetcher("bookById", env -> new Book(53L, "Breaking Bad", new Author(0L, "", "Heisenberg"))).toWebGraphQlHandler();
    outputMono = handler.handleRequest(input("{ bookById(id: 1) {name}}"));
    actualBook = GraphQlResponse.from(outputMono).toEntity("bookById", Book.class);
    assertThat(actualBook.getName()).isEqualTo("Breaking Bad");
}
Also used : WebOutput(org.springframework.graphql.web.WebOutput) Author(org.springframework.graphql.Author) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) Test(org.junit.jupiter.api.Test)

Example 14 with WebOutput

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

the class QueryByExampleDataFetcherJpaTests method shouldFavorExplicitWiring.

@Test
void shouldFavorExplicitWiring() {
    BookJpaRepository mockRepository = mock(BookJpaRepository.class);
    Book book = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    when(mockRepository.findBy(any(), any())).thenReturn(Optional.of(book));
    // 1) Automatic registration only
    WebGraphQlHandler handler = graphQlSetup(mockRepository).toWebGraphQlHandler();
    Mono<WebOutput> outputMono = handler.handleRequest(input("{ bookById(id: 1) {name}}"));
    Book actualBook = GraphQlResponse.from(outputMono).toEntity("bookById", Book.class);
    assertThat(actualBook.getName()).isEqualTo("Hitchhiker's Guide to the Galaxy");
    // 2) Automatic registration and explicit wiring
    handler = graphQlSetup(mockRepository).queryFetcher("bookById", env -> new Book(53L, "Breaking Bad", new Author(0L, "", "Heisenberg"))).toWebGraphQlHandler();
    outputMono = handler.handleRequest(input("{ bookById(id: 1) {name}}"));
    actualBook = GraphQlResponse.from(outputMono).toEntity("bookById", Book.class);
    assertThat(actualBook.getName()).isEqualTo("Breaking Bad");
}
Also used : WebOutput(org.springframework.graphql.web.WebOutput) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) Test(org.junit.jupiter.api.Test)

Example 15 with WebOutput

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

the class QueryByExampleDataFetcherJpaTests method shouldFetchSingleItemsWithDtoProjection.

@Disabled("Pending https://github.com/spring-projects/spring-data-jpa/issues/2327")
@Test
void shouldFetchSingleItemsWithDtoProjection() {
    Book book = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
    repository.save(book);
    DataFetcher<?> fetcher = QueryByExampleDataFetcher.builder(repository).projectAs(BookDto.class).single();
    WebGraphQlHandler handler = graphQlSetup("bookById", fetcher).toWebGraphQlHandler();
    Mono<WebOutput> outputMono = handler.handleRequest(input("{ bookById(id: 42) {name}}"));
    Book actualBook = GraphQlResponse.from(outputMono).toEntity("bookById", Book.class);
    assertThat(actualBook.getName()).isEqualTo("The book is: Hitchhiker's Guide to the Galaxy");
}
Also used : WebOutput(org.springframework.graphql.web.WebOutput) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Aggregations

WebOutput (org.springframework.graphql.web.WebOutput)18 Test (org.junit.jupiter.api.Test)17 WebGraphQlHandler (org.springframework.graphql.web.WebGraphQlHandler)17 Author (org.springframework.graphql.Author)9 URI (java.net.URI)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 List (java.util.List)6 WebInput (org.springframework.graphql.web.WebInput)6 HttpHeaders (org.springframework.http.HttpHeaders)6 Nullable (org.springframework.lang.Nullable)6 Flux (reactor.core.publisher.Flux)6 Mono (reactor.core.publisher.Mono)6 Predicate (com.querydsl.core.types.Predicate)5 DataFetcher (graphql.schema.DataFetcher)5 Optional (java.util.Optional)5 Consumer (java.util.function.Consumer)5 Collectors (java.util.stream.Collectors)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 ArgumentCaptor (org.mockito.ArgumentCaptor)5