spring-graphql icon indicating copy to clipboard operation
spring-graphql copied to clipboard

BatchMapping annotation does not do parallel execution for data loaders out of the box for Java 17

Open kaus02 opened this issue 1 year ago • 5 comments

BatchMapping annotation has been recently introduced as a substitute to configuring BatchLoaderRegistry and using annotation SchemaMapping. In the docs it is not specified that solely using the BatchMapping annotation does not do a parallel execution for field resolvers, how do we enable parallel fetch for field resolvers when using batchmapping and Java 17.

kaus02 avatar Dec 04 '24 19:12 kaus02

@rstoyanchev if you can please help here

kaus02 avatar Dec 04 '24 20:12 kaus02

Documentation mentions following line

@SchemaMapping and @BatchMapping methods can return Callable that is submitted to an Executor such as the Spring Framework VirtualThreadTaskExecutor. To enable this, you must configure an Executor on AnnotatedControllerConfigurer.

Can we have an example of this , how to "configure an Executor on AnnotatedControllerConfigurer"? As I'm creating the bean like following and the code is not executing on different threads, even after using both schemamapping and batchmapping annotation.

@Bean
public DefaultBatchLoaderRegistry batchLoaderRegistry() {
    return new DefaultBatchLoaderRegistry();
}

@Bean // <1>
public AnnotatedControllerConfigurer annotatedControllerConfigurer() {
    AnnotatedControllerConfigurer annotatedControllerConfigurer= new AnnotatedControllerConfigurer();
    annotatedControllerConfigurer.setExecutor(new VirtualThreadTaskExecutor());
    return annotatedControllerConfigurer;
}
@Bean // <2>
public ExecutionGraphQlService executionGraphQlService() {
    GraphQlSource graphQlSource = GraphQlSource.schemaResourceBuilder() // <3>
            .schemaResources(new ClassPathResource("graphql/schema.graphqls"))
            .configureRuntimeWiring(annotatedControllerConfigurer())
            .build();
    DefaultExecutionGraphQlService service = new DefaultExecutionGraphQlService(graphQlSource);
    service.addDataLoaderRegistrar(batchLoaderRegistry());
    return service;
}

kaus02 avatar Dec 04 '24 21:12 kaus02

Generally, if running on Java < 21, all controller methods (schema and batch mapping) with an imperative method signature execute synchronously, and you would need to return a Callable to switch threads. On Java 21+ we support invoking methods with imperative signature asynchronously transparently in virtual threads. This is in the reference docs, but I'm open to specific suggestions on how to improve it.

The Boot auto-config plugs in an executor if there is one configured as expected in a Boot application.

rstoyanchev avatar Jan 28 '25 19:01 rstoyanchev

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

spring-projects-issues avatar Feb 04 '25 19:02 spring-projects-issues

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

spring-projects-issues avatar Feb 11 '25 19:02 spring-projects-issues