【发布时间】:2017-11-25 03:52:08
【问题描述】:
我想澄清在 Apollo + GraphQL 中我应该为解析器函数使用哪种方法
让我们假设以下架构:
type Post {
id: Int
text: String
upVotes: Int
}
type Author{
name: String
posts: [Post]
}
schema {
query: Author
}
ApoloGraphql tutorial 建议这样的解析器映射:
{Query:{
author(_, args) {
return author.findAll()
}
}
},
Author {
posts: (author) => author.getPosts(),
}
据我所知,关于帖子的每个逻辑,例如get author with posts where the count of post upVotes > args.upVotes,必须在author 方法中处理。这为我们提供了以下解析器映射:
{Query:{
author(_, args) {
return author.findAll({
include:[model: Post]
where: {//post upVotes > args.upVotes}
})
}
},
Author {
posts: (author) => author.getPosts(),
}
调用author,将首先在一个联合查询中选择具有帖子的作者,其中帖子大于args.upVotes。然后它将再次选择该作者的帖子,因为Author ... getPosts()
从技术上讲,我可以通过删除 Author 来达到相同的结果,因为帖子已经包含在小的 author 方法中。
我有以下问题:
-
我需要这份声明吗?在哪些情况下?
Author { posts: (author) => author.getPosts(), } 如果没有,那么我如何确定是否请求了帖子字段,以便 我可以使帖子有条件地包含,不仅取决于 参数,还包括请求的字段?
如果是,哪些帖子将包含最终结果?来自的帖子 include 语句,还是 getPosts()?
【问题讨论】:
标签: graphql apollo graphql-js react-apollo