Search in sources :

Example 1 with GraphQlService

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

the class BatchLoadingTests method batchLoader.

@Test
void batchLoader() {
    String document = "{ " + "  booksByCriteria(criteria: {author:\"Orwell\"}) { " + "    author {" + "      firstName, " + "      lastName " + "    }" + "  }" + "}";
    this.registry.forTypePair(Long.class, Author.class).registerBatchLoader((ids, env) -> Flux.fromIterable(ids).map(BookSource::getAuthor));
    GraphQlService service = GraphQlSetup.schemaResource(BookSource.schema).queryFetcher("booksByCriteria", env -> {
        Map<String, Object> criteria = env.getArgument("criteria");
        String authorName = (String) criteria.get("author");
        return BookSource.findBooksByAuthor(authorName).stream().map(book -> new Book(book.getId(), book.getName(), book.getAuthorId())).collect(Collectors.toList());
    }).dataFetcher("Book", "author", env -> {
        Book book = env.getSource();
        DataLoader<Long, Author> dataLoader = env.getDataLoader(Author.class.getName());
        return dataLoader.load(book.getAuthorId());
    }).dataLoaders(this.registry).toGraphQlService();
    Mono<RequestOutput> resultMono = service.execute(TestRequestInput.forDocument(document));
    List<Book> books = GraphQlResponse.from(resultMono).toList("booksByCriteria", Book.class);
    assertThat(books).hasSize(2);
    Author author = books.get(0).getAuthor();
    assertThat(author).isNotNull();
    assertThat(author.getFirstName()).isEqualTo("George");
    assertThat(author.getLastName()).isEqualTo("Orwell");
}
Also used : GraphQlResponse(org.springframework.graphql.GraphQlResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BookSource(org.springframework.graphql.BookSource) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) DataLoader(org.dataloader.DataLoader) Collectors(java.util.stream.Collectors) Book(org.springframework.graphql.Book) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) Author(org.springframework.graphql.Author) List(java.util.List) Map(java.util.Map) GraphQlService(org.springframework.graphql.GraphQlService) RequestOutput(org.springframework.graphql.RequestOutput) TestRequestInput(org.springframework.graphql.TestRequestInput) RequestOutput(org.springframework.graphql.RequestOutput) Book(org.springframework.graphql.Book) Author(org.springframework.graphql.Author) Map(java.util.Map) GraphQlService(org.springframework.graphql.GraphQlService) Test(org.junit.jupiter.api.Test)

Aggregations

List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 DataLoader (org.dataloader.DataLoader)1 Test (org.junit.jupiter.api.Test)1 Author (org.springframework.graphql.Author)1 Book (org.springframework.graphql.Book)1 BookSource (org.springframework.graphql.BookSource)1 GraphQlResponse (org.springframework.graphql.GraphQlResponse)1 GraphQlService (org.springframework.graphql.GraphQlService)1 GraphQlSetup (org.springframework.graphql.GraphQlSetup)1 RequestOutput (org.springframework.graphql.RequestOutput)1 TestRequestInput (org.springframework.graphql.TestRequestInput)1 Flux (reactor.core.publisher.Flux)1 Mono (reactor.core.publisher.Mono)1