Search in sources :

Example 1 with Book

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");
}
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 Book

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");
}
Also used : Book(org.springframework.graphql.Book) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) Test(org.junit.jupiter.api.Test)

Example 3 with Book

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");
}
Also used : RequestOutput(org.springframework.graphql.RequestOutput) Book(org.springframework.graphql.Book) Author(org.springframework.graphql.Author) Test(org.junit.jupiter.api.Test)

Example 4 with Book

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");
}
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 5 with Book

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