use of org.springframework.graphql.Author in project spring-graphql by spring-projects.
the class SchemaMappingInvocationTests method queryWithScalarArgument.
@Test
void queryWithScalarArgument() {
String document = "{ " + " bookById(id:\"1\") { " + " id" + " name" + " author {" + " firstName" + " lastName" + " }" + " }" + "}";
Mono<RequestOutput> resultMono = graphQlService().execute(TestRequestInput.forDocument(document));
Book book = GraphQlResponse.from(resultMono).toEntity("bookById", Book.class);
assertThat(book.getId()).isEqualTo(1);
assertThat(book.getName()).isEqualTo("Nineteen Eighty-Four");
Author author = book.getAuthor();
assertThat(author.getFirstName()).isEqualTo("George");
assertThat(author.getLastName()).isEqualTo("Orwell");
}
use of org.springframework.graphql.Author in project spring-graphql by spring-projects.
the class QuerydslDataFetcherTests method shouldFetchSingleItemsWithInterfaceProjection.
@Test
void shouldFetchSingleItemsWithInterfaceProjection() {
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(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");
}
use of org.springframework.graphql.Author in project spring-graphql by spring-projects.
the class QuerydslDataFetcherTests method shouldApplyCustomizerViaBuilder.
@Test
void shouldApplyCustomizerViaBuilder() {
MockRepository mockRepository = mock(MockRepository.class);
DataFetcher<Iterable<Book>> fetcher = QuerydslDataFetcher.builder(mockRepository).customizer((QuerydslBinderCustomizer<QBook>) (bindings, book) -> bindings.bind(book.name).firstOptional((path, value) -> value.map(path::startsWith))).many();
graphQlSetup("books", fetcher).toWebGraphQlHandler().handleRequest(input("{ books(name: \"H\", author: \"Doug\") {name}}")).block();
ArgumentCaptor<Predicate> predicateCaptor = ArgumentCaptor.forClass(Predicate.class);
verify(mockRepository).findBy(predicateCaptor.capture(), any());
Predicate predicate = predicateCaptor.getValue();
assertThat(predicate).isEqualTo(QBook.book.name.startsWith("H").and(QBook.book.author.eq("Doug")));
}
use of org.springframework.graphql.Author in project spring-graphql by spring-projects.
the class QuerydslDataFetcherTests method shouldReactivelyFetchMultipleItems.
@Test
void shouldReactivelyFetchMultipleItems() {
ReactiveMockRepository mockRepository = mock(ReactiveMockRepository.class);
Book book1 = new Book(42L, "Hitchhiker's Guide to the Galaxy", new Author(0L, "Douglas", "Adams"));
Book book2 = new Book(53L, "Breaking Bad", new Author(0L, "", "Heisenberg"));
when(mockRepository.findBy(any(), any())).thenReturn(Flux.just(book1, book2));
Consumer<GraphQlSetup> tester = setup -> {
Mono<WebOutput> outputMono = setup.toWebGraphQlHandler().handleRequest(input("{ books {name}}"));
List<String> names = GraphQlResponse.from(outputMono).toList("books", Book.class).stream().map(Book::getName).collect(Collectors.toList());
assertThat(names).containsExactlyInAnyOrder("Breaking Bad", "Hitchhiker's Guide to the Galaxy");
};
// explicit wiring
tester.accept(graphQlSetup("books", QuerydslDataFetcher.builder(mockRepository).many()));
// auto registration
tester.accept(graphQlSetup(mockRepository));
}
use of org.springframework.graphql.Author 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