graphql:按嵌套字段排序

Pan*_*ani 9 graphql

假设我有 2 个表:
- 用户(id、名称、帖子)
- 帖子(id、消息、用户)

如何按用户名(desc)获取前 10 个帖子订单?

这是我的架构的样子:

var PostType = new GraphQLObjectType({
  name: "Post",
  fields: () => ({
    id: { type: GraphQLInt },
    message: { type: GraphQLString },
    user: {
      type: UserType,
      args: {
        orderBy: { type: sortType }
      },
      resolve(parent, args) {
        console.info("Post resolve called.");
        return userMap[parent.user];
      }
    }
  })
});

var RootQuery = new GraphQLObjectType({
  name: "RootQueryType",
  fields: {
    allPosts: {
      type: new GraphQLList(PostType),
      resolve(parentValue, args) {
        console.info("allPosts resolve called.");
        return postData;
      }
    }
  }
});
Run Code Online (Sandbox Code Playgroud)

和查询:

{
  allPosts {
    message
    user (orderBy: {field: "name", direction: ASC}) {
      name
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

有什么办法,我可以在 allPosts 解析器函数之前调用用户解析器函数吗?因为,我试图获取按名称排序的 10 个用户,然后将帖子 ID 传递给 allPosts 解析器。

Dan*_*den 5

GraphQL 字段以自上而下的方式解析。这意味着allPosts首先解决,然后是messageuser字段(同时),然后是name字段。这是必须发生的,因为“父”或根字段的解析值决定了随后作为根值传递给其子字段的解析器的值。信息从“较高”解析器流向“较低”解析器,但反之则不然。

orderBy在这里的争论可能应该是场上的争论allPosts而不是user现场的争论。这样做有两个原因:(1)从概念上讲,无论排序标准如何,您都会对返回的帖子进行排序allPosts- 按照惯例,将排序放在那里才有意义;(2) 解析器需要的参数可能allPosts多于解析器需要的参数user

为了使上述工作正常进行,您可能需要修改识别排序标准的方式(例如创建field一条路径)。user.name您可能还需要“提升”将用户填充到解析器中的逻辑allPosts。例如:

resolve(parentValue, { sortBy: { path, direction } }) {
  const data = postData.map(post => {
    post.user = userMap[post.user]
    return post
  });
  // using lodash
  return orderBy(data, [(post) => get(post, path)], [direction])
}
Run Code Online (Sandbox Code Playgroud)

通过解析作为第四个参数传递给解析器函数的对象可以确定请求中其他字段(包括参数)的选择集。info但这很痛苦,我不知道这个特殊情况是否真的证明这样做是合理的。您可以在此答案中阅读有关该方法的更多信息。