首页 > 解决方案 > 用于反应器网络的 Graphql SPQR 批量加载器

问题描述

如何使用 AsyncExecutionStrategy 为 queryExecutionStrategy 设置 batchLoader?

例如我有 3 个实体:用户发表评论

配置

@Configuration
public class AppConfig {

  @Bean
  GraphQL graphQL(GraphQLSchema schema) {
    return GraphQL.newGraphQL(schema)
      .queryExecutionStrategy(new AsyncExecutionStrategy(new SimpleDataFetcherExceptionHandler()))
      .mutationExecutionStrategy(new AsyncSerialExecutionStrategy(new SimpleDataFetcherExceptionHandler()))
      .build();
  }
}

图式

@Component
@GraphQLApi
@AllArgsConstructor
public class SimpleApi {
  private final SimpleService simpleService;

  @GraphQLQuery
  public Flux<User> findUsers() {
    return simpleService.findLast3Users();
  }
  @GraphQLQuery
  public Flux<Post> findPosts(@GraphQLContext List<User> users) {
    var userIds = users.stream()
      .map(User::getId)
      .collect(Collectors.toList());
    return simpleService.findPostByUserId(userIds);
  }
  @GraphQLQuery
  public Flux<Post> findComments(@GraphQLContext List<Post> posts) {
    var userIds = posts.stream()
      .map(Post::getId)
      .collect(Collectors.toList());
    return simpleService.findCommentsByPostId(userIds);
  }
}

在我的 pom.xml 中包括

    <dependency>
      <groupId>io.leangen.graphql</groupId>
      <artifactId>graphql-spqr-spring-boot-starter</artifactId>
      <version>0.0.6</version>
    </dependency>
    <dependency>
      <groupId>io.leangen.graphql</groupId>
      <artifactId>spqr</artifactId>
      <version>0.11.1</version>
    </dependency>

当我更改 queryExecutionStrategy(BatchedExecutionStrategy),将 @Batched 标记为 @GraphQLQuery 方法和时,这适用于响应列表,但批处理:

  1. 已弃用,当返回通量时 - 这是对块的调用。
  2. 需要返回列表,而不是通量

例如,如何实现 BatchLoader 和更新模式 - 对于 Post?我读了这个老问题,但不明白https://github.com/leangen/graphql-spqr/issues/182https://github.com/leangen/graphql-spqr/issues/178

标签: javaspring-bootmavengraphqlgraphql-spqr

解决方案


推荐阅读