use of org.springframework.graphql.Book in project spring-graphql by spring-projects.
the class DefaultBatchLoaderRegistryTests method mappedBatchLoader.
@Test
void mappedBatchLoader() throws Exception {
AtomicReference<String> valueRef = new AtomicReference<>();
this.batchLoaderRegistry.forTypePair(Long.class, Book.class).withOptions(// DataLoader invoked immediately
options -> options.setBatchingEnabled(false)).registerMappedBatchLoader((ids, environment) -> Mono.deferContextual(contextView -> {
valueRef.set(contextView.get("key"));
return Flux.fromIterable(ids).map(BookSource::getBook).collectMap(Book::getId, Function.identity());
}));
GraphQLContext graphQLContext = initGraphQLContext(Context.of("key", "value"));
this.batchLoaderRegistry.registerDataLoaders(this.dataLoaderRegistry, graphQLContext);
Map<String, DataLoader<?, ?>> map = this.dataLoaderRegistry.getDataLoadersMap();
assertThat(map).hasSize(1).containsKey(Book.class.getName());
// Invoke DataLoader to check the context
((DataLoader<Long, Book>) map.get(Book.class.getName())).load(1L).get();
assertThat(valueRef.get()).isEqualTo("value");
}
use of org.springframework.graphql.Book in project spring-graphql by spring-projects.
the class GraphQlArgumentInitializerTests method shouldInstantiateNestedBean.
@Test
void shouldInstantiateNestedBean() throws Exception {
String payload = "{\"book\": { \"name\": \"test name\", \"author\": { \"firstName\": \"Jane\", \"lastName\": \"Spring\"} } }";
DataFetchingEnvironment environment = initEnvironment(payload);
Object result = initializer.initializeArgument(environment, "book", ResolvableType.forClass(Book.class));
assertThat(result).isNotNull().isInstanceOf(Book.class);
assertThat(result).hasFieldOrPropertyWithValue("name", "test name");
assertThat(((Book) result).getAuthor()).isNotNull().hasFieldOrPropertyWithValue("firstName", "Jane").hasFieldOrPropertyWithValue("lastName", "Spring");
}
use of org.springframework.graphql.Book 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.Book 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");
}
use of org.springframework.graphql.Book in project spring-graphql by spring-projects.
the class ContextValueMethodArgumentResolverTests method resolveOptional.
@Test
@SuppressWarnings({ "unchecked", "ConstantConditions", "OptionalGetWithoutIsPresent" })
void resolveOptional() {
GraphQLContext context = GraphQLContext.newContext().of("optionalBook", this.book).build();
Optional<Book> actual = (Optional<Book>) resolveValue(context, context, 3);
assertThat(actual.get()).isSameAs(this.book);
}
Aggregations