java - 用于反应器网络的 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 方法和时,这适用于响应列表,但批处理:
- 已弃用,当返回通量时 - 这是对块的调用。
- 需要返回列表,而不是通量
例如,如何实现 BatchLoader 和更新模式 - 对于 Post?我读了这个老问题,但不明白https://github.com/leangen/graphql-spqr/issues/182和https://github.com/leangen/graphql-spqr/issues/178
解决方案
推荐阅读
- python - 列表结构比较
- python - 有异常的三元运算符?
- python - 如何卸载使用 .tar.gz 安装的 python 包?
- ios - 使用第三方开发证书和配置文件在物理设备上调试 iOS
- corda - 运行“build/nodes/runnodes”时出现控制台错误
- git - 两个不同历史的 git 遥控器
- vue.js - 为什么 Vue 在这种情况下不响应?
- python - 如果没有任何改变,如何避免建造新车轮
- python - 为什么 lxml 不允许在整个 ElementTree 上使用相对 XPath 表达式?
- powershell - 通过 bat 文件运行 powershell 命令时出现问题