use of org.springframework.graphql.Book in project spring-graphql by spring-projects.
the class ContextValueMethodArgumentResolverTests method resolveMissing.
@Test
@SuppressWarnings({ "unchecked", "ConstantConditions" })
void resolveMissing() {
GraphQLContext context = GraphQLContext.newContext().build();
// Required
assertThatIllegalStateException().isThrownBy(() -> resolveValue(context, context, 0)).withMessage("Missing required context value for method 'handle' parameter 0");
// Not required
assertThat(resolveValue(context, context, 2)).isNull();
// Optional
Optional<Book> actual = (Optional<Book>) resolveValue(context, context, 3);
assertThat(actual.isPresent()).isFalse();
}
use of org.springframework.graphql.Book in project spring-graphql by spring-projects.
the class DefaultBatchLoaderRegistryTests method batchLoader.
@Test
void batchLoader() throws Exception {
AtomicReference<String> valueRef = new AtomicReference<>();
this.batchLoaderRegistry.forTypePair(Long.class, Book.class).withOptions(// DataLoader invoked immediately
options -> options.setBatchingEnabled(false)).registerBatchLoader((ids, environment) -> Flux.deferContextual(contextView -> {
valueRef.set(contextView.get("key"));
return Flux.fromIterable(ids).map(BookSource::getBook);
}));
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");
}
Aggregations