Search in sources :

Example 6 with WebGraphQlHandler

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

the class QueryByExampleDataFetcherMongoDbTests method shouldFetchSingleItemsWithInterfaceProjection.

@Test
void shouldFetchSingleItemsWithInterfaceProjection() {
    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(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 7 with WebGraphQlHandler

use of org.springframework.graphql.web.WebGraphQlHandler 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 8 with WebGraphQlHandler

use of org.springframework.graphql.web.WebGraphQlHandler 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 9 with WebGraphQlHandler

use of org.springframework.graphql.web.WebGraphQlHandler 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 10 with WebGraphQlHandler

use of org.springframework.graphql.web.WebGraphQlHandler 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

Test (org.junit.jupiter.api.Test)13 WebGraphQlHandler (org.springframework.graphql.web.WebGraphQlHandler)13 WebOutput (org.springframework.graphql.web.WebOutput)11 Author (org.springframework.graphql.Author)3 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 Collections (java.util.Collections)2 List (java.util.List)2 Map (java.util.Map)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 BiConsumer (java.util.function.BiConsumer)2 Assertions.as (org.assertj.core.api.Assertions.as)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 InstanceOfAssertFactories (org.assertj.core.api.InstanceOfAssertFactories)2 GraphQlSetup (org.springframework.graphql.GraphQlSetup)2 ConsumeOneAndNeverCompleteInterceptor (org.springframework.graphql.web.ConsumeOneAndNeverCompleteInterceptor)2 WebInterceptor (org.springframework.graphql.web.WebInterceptor)2 WebSocketHandlerTestSupport (org.springframework.graphql.web.WebSocketHandlerTestSupport)2 WebSocketInterceptor (org.springframework.graphql.web.WebSocketInterceptor)2 GraphQlMessage (org.springframework.graphql.web.support.GraphQlMessage)2