首页 > 解决方案 > 如何通过没有边的后代过滤 GraphQL 结果?

问题描述

我刚开始研究 GraphQL,我想知道是否有一种方法可以过滤没有任何节点的结果。这是一个相对简单的示例查询:

query { 
  organization(login:"GitHub") { 
    repositories(first: 20) {
      edges {
        node {
          name
          pullRequests(first: 5, states: OPEN){
            edges {
              node {
                title
                author{
                  login
                }
                updatedAt
              }
            }
          }
        }
      }
    }
  }
}

这是查询返回的结果的子集:

{
  "data": {
    "organization": {
      "repositories": {
        "edges": [
          {
            "node": {
              "name": "gitignore",
              "pullRequests": {
                "edges": [
                  {
                    "node": {
                      "title": "Create new CodeComposerStudio.gitignore",
                      "author": {
                        "login": "wolf99"
                      },
                      "updatedAt": "2017-07-26T20:31:53Z"
                    }
                  },
                  {
                    "node": {
                      "title": "Create PVS.gitignore",
                      "author": {
                        "login": "cesaramh"
                      },
                      "updatedAt": "2017-05-01T19:42:07Z"
                    }
                  },
                  {
                    "node": {
                      "title": "gitignore for Magic Software Enterprises product xpa ",
                      "author": {
                        "login": "tommes"
                      },
                      "updatedAt": "2017-05-01T19:41:53Z"
                    }
                  },
                  {
                    "node": {
                      "title": "Create PSoC.gitignore",
                      "author": {
                        "login": "dbrwn"
                      },
                      "updatedAt": "2017-05-01T19:41:39Z"
                    }
                  },
                  {
                    "node": {
                      "title": "add ThinkPHP gitignore file",
                      "author": {
                        "login": "swumao"
                      },
                      "updatedAt": "2017-05-01T19:40:53Z"
                    }
                  }
                ]
              }
            }
          },
          {
            "node": {
              "name": "dmca",
              "pullRequests": {
                "edges": []
              }
            }
          }
        ]
      }
    }
  }
}

所以我想知道是否有办法修改我的查询,这样它就不会返回名为的节点,dmca因为pullRequests.

标签: graphql

解决方案


如果您使用的是 githubs graphql api,那么似乎没有办法过滤这些边,但是如果您正在实现 graphql 服务器,那么就有可能知道边节点是什么,从而在边解析器中过滤它


推荐阅读