Graphql 基于批量查询的postgraphile服务模式拼接
我有两个目标:Graphql 基于批量查询的postgraphile服务模式拼接,graphql,apollo-server,graphql-tools,apollo-federation,postgraphile,Graphql,Apollo Server,Graphql Tools,Apollo Federation,Postgraphile,我有两个目标: 在任何有趣的数据库上启动postgraphile服务 创建一个网关,将这些服务组合到一个仍然性能良好的图中(特别是,我对批量查询感兴趣) 从我所做的阅读来看,似乎有两种主要方法: 从graphql工具 来自阿波罗联盟 我已经尝试了每种方法,但我目前正在使用模式缝合方法。假设我有一个服务公开的用户和另一个服务公开的帖子: [user subschema] type User { id: ID! name: String! } [post subschema] type P
graphql工具
[user subschema]
type User {
id: ID!
name: String!
}
[post subschema]
type Post {
id: ID!
userId: ID!
content: String!
}
现在让我们假设我想查询所有用户,并获得每个用户的帖子列表。如果这两个都是由同一个服务实现的,Postgraphile将足够聪明,可以将请求批处理在一起。但由于他们的服务不同,我需要做一些工作来实现这一点:
delegateToSchema
更新为batchDelegateBySchema
FavoritesByUserID
resolver,它接收用户ID列表并返回每个ID的收藏夹batch:true
,并在postgraphile服务上配置了enableQueryBatching:true
batchPosts: {
selectionSet: "{ id }",
resolve(user, args, context, info) {
return batchDelegateToSchema({
schema: postsSubSchema,
operation: "query",
fieldName: "postsByUserIds",
key: user.id,
argsFromKeys: (ids) => ({ ids }),
valuesFromResults: (results, keys) => {
return keys.map((id) => {
return results.filter((f) => f.user_id === id);
});
},
context,
info,
});
}
}