Skip to content

Commit

Permalink
Update EntityMapping test to use batch mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Mar 20, 2024
1 parent 47b873a commit 3da533d
Showing 1 changed file with 11 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import graphql.GraphqlErrorBuilder;
import graphql.schema.DataFetchingEnvironment;
import org.junit.jupiter.api.Test;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
Expand All @@ -38,9 +39,11 @@
import org.springframework.graphql.TestExecutionGraphQlService;
import org.springframework.graphql.TestExecutionRequest;
import org.springframework.graphql.data.method.annotation.Argument;
import org.springframework.graphql.data.method.annotation.BatchMapping;
import org.springframework.graphql.data.method.annotation.GraphQlExceptionHandler;
import org.springframework.graphql.data.method.annotation.SchemaMapping;
import org.springframework.graphql.data.method.annotation.support.AnnotatedControllerConfigurer;
import org.springframework.graphql.execution.BatchLoaderRegistry;
import org.springframework.graphql.execution.DefaultBatchLoaderRegistry;
import org.springframework.graphql.execution.ErrorType;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -130,8 +133,11 @@ private static void assertError(ResponseHelper helper, int i, String errorType,
}

private TestExecutionGraphQlService graphQlService() {
BatchLoaderRegistry registry = new DefaultBatchLoaderRegistry();

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AuthorController.class);
context.registerBean(BatchLoaderRegistry.class, () -> registry);
context.refresh();

AnnotatedControllerConfigurer configurer = new AnnotatedControllerConfigurer();
Expand All @@ -145,6 +151,7 @@ private TestExecutionGraphQlService graphQlService() {
return GraphQlSetup.schemaResource(federationSchema)
.runtimeWiring(configurer)
.schemaFactory(schemaFactory::createGraphQLSchema)
.dataLoaders(registry)
.toGraphQlService();
}

Expand All @@ -169,10 +176,9 @@ public Book book(@Argument int id, Map<String, Object> map) {
};
}

@SchemaMapping
public Author author(Book book) {
long id = book.getId();
return BookSource.getBook(id).getAuthor();
@BatchMapping
public Flux<Author> author(List<Book> books) {
return Flux.fromIterable(books).map(book -> BookSource.getBook(book.getId()).getAuthor());
}

@GraphQlExceptionHandler
Expand Down

0 comments on commit 3da533d

Please sign in to comment.