Search in sources :

Example 16 with WebOutput

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

the class QueryByExampleDataFetcherReactiveMongoDbTests method shouldFetchSingleItemsReactivelyWithInterfaceProjection.

@Test
void shouldFetchSingleItemsReactivelyWithInterfaceProjection() {
    Book book = new Book("42", "Hitchhiker's Guide to the Galaxy", new Author("0", "Douglas", "Adams"));
    repository.save(book).block();
    DataFetcher<?> fetcher = QueryByExampleDataFetcher.builder(repository).projectAs(BookProjection.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("Hitchhiker's Guide to the Galaxy by Douglas Adams");
}
Also used : WebOutput(org.springframework.graphql.web.WebOutput) WebGraphQlHandler(org.springframework.graphql.web.WebGraphQlHandler) Test(org.junit.jupiter.api.Test)

Example 17 with WebOutput

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

the class QueryByExampleDataFetcherMongoDbTests method shouldFetchSingleItemsWithDtoProjection.

@Test
void shouldFetchSingleItemsWithDtoProjection() {
    Book book = new Book("42", "Hitchhiker's Guide to the Galaxy", new Author("0", "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)

Example 18 with WebOutput

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

the class QueryByExampleDataFetcherMongoDbTests method shouldFavorExplicitWiring.

@Test
void shouldFavorExplicitWiring() {
    BookMongoRepository mockRepository = mock(BookMongoRepository.class);
    Book book = new Book("42", "Hitchhiker's Guide to the Galaxy", new Author("0", "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("53", "Breaking Bad", new Author("0", "", "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)

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