Search in sources :

Example 6 with Book

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();
}
Also used : Optional(java.util.Optional) Book(org.springframework.graphql.Book) GraphQLContext(graphql.GraphQLContext) Test(org.junit.jupiter.api.Test)

Example 7 with Book

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");
}
Also used : DataLoaderRegistry(org.dataloader.DataLoaderRegistry) ContextView(reactor.util.context.ContextView) StatisticsCollector(org.dataloader.stats.StatisticsCollector) Context(reactor.util.context.Context) BookSource(org.springframework.graphql.BookSource) NoOpStatisticsCollector(org.dataloader.stats.NoOpStatisticsCollector) Mono(reactor.core.publisher.Mono) DataLoader(org.dataloader.DataLoader) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Book(org.springframework.graphql.Book) ExecutionInput(graphql.ExecutionInput) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) GraphQLContext(graphql.GraphQLContext) Map(java.util.Map) AssertionsForInterfaceTypes.assertThat(org.assertj.core.api.AssertionsForInterfaceTypes.assertThat) DataLoader(org.dataloader.DataLoader) Book(org.springframework.graphql.Book) GraphQLContext(graphql.GraphQLContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)7 Book (org.springframework.graphql.Book)7 GraphQLContext (graphql.GraphQLContext)4 Map (java.util.Map)3 DataLoader (org.dataloader.DataLoader)3 BookSource (org.springframework.graphql.BookSource)3 Flux (reactor.core.publisher.Flux)3 Mono (reactor.core.publisher.Mono)3 ExecutionInput (graphql.ExecutionInput)2 Optional (java.util.Optional)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Function (java.util.function.Function)2 AssertionsForInterfaceTypes.assertThat (org.assertj.core.api.AssertionsForInterfaceTypes.assertThat)2 DataLoaderRegistry (org.dataloader.DataLoaderRegistry)2 NoOpStatisticsCollector (org.dataloader.stats.NoOpStatisticsCollector)2 StatisticsCollector (org.dataloader.stats.StatisticsCollector)2 Author (org.springframework.graphql.Author)2 RequestOutput (org.springframework.graphql.RequestOutput)2 Context (reactor.util.context.Context)2 ContextView (reactor.util.context.ContextView)2