Search in sources :

Example 1 with BookSource

use of org.springframework.graphql.BookSource 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");
}
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) BookSource(org.springframework.graphql.BookSource) GraphQLContext(graphql.GraphQLContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 2 with BookSource

use of org.springframework.graphql.BookSource 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");
}
Also used : GraphQlResponse(org.springframework.graphql.GraphQlResponse) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BookSource(org.springframework.graphql.BookSource) GraphQlSetup(org.springframework.graphql.GraphQlSetup) Mono(reactor.core.publisher.Mono) DataLoader(org.dataloader.DataLoader) Collectors(java.util.stream.Collectors) Book(org.springframework.graphql.Book) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) Author(org.springframework.graphql.Author) List(java.util.List) Map(java.util.Map) GraphQlService(org.springframework.graphql.GraphQlService) RequestOutput(org.springframework.graphql.RequestOutput) TestRequestInput(org.springframework.graphql.TestRequestInput) RequestOutput(org.springframework.graphql.RequestOutput) Book(org.springframework.graphql.Book) Author(org.springframework.graphql.Author) Map(java.util.Map) GraphQlService(org.springframework.graphql.GraphQlService) Test(org.junit.jupiter.api.Test)

Example 3 with BookSource

use of org.springframework.graphql.BookSource 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

Map (java.util.Map)3 DataLoader (org.dataloader.DataLoader)3 Test (org.junit.jupiter.api.Test)3 Book (org.springframework.graphql.Book)3 BookSource (org.springframework.graphql.BookSource)3 Flux (reactor.core.publisher.Flux)3 Mono (reactor.core.publisher.Mono)3 ExecutionInput (graphql.ExecutionInput)2 GraphQLContext (graphql.GraphQLContext)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 Context (reactor.util.context.Context)2 ContextView (reactor.util.context.ContextView)2 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1