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");
}
Aggregations