【问题标题】:Display more than 100 entries through GraphQL API通过 GraphQL API 显示 100 多个条目
【发布时间】:2018-08-20 14:05:19
【问题描述】:

我已经厌倦了在 github grpahQL API 中使用 endCursor 和 hasNextPage 来获取 100 多个数据。我使用的查询是:

query {
      organization(login:"XXX") {
                   repository(name:"XX") {
                   pullRequests(first:100, states:[OPEN], after: "XXX" ) {
        pageInfo{
          hasNextPage
          endCursor
        }
      }
    }

它正在工作。但是为了访问更多细节,需要进行迭代分页。谁能提供一种有效的替代方案来以编程方式在 GraphQL API 中遍历所有页面?

【问题讨论】:

标签: graphql github-api github-graphql


【解决方案1】:

从 Simon Willison 的“Paginating through the GitHub GraphQL API with Python”中汲取灵感,这是我为查询分页所做的工作:

query {
  node(id: "PROJECT_ID") {
    ... on ProjectNext {
      items(first: 100 after: CURSOR) {
        pageInfo {
          hasNextPage
          endCursor
        }
        nodes {
          title
          fieldValues(first: 8) {
            nodes {
              value
            }
          }
          content {
            ... on Issue {
              number
              labels(first: 50) {
                nodes {
                  name
}}}}}}}}}

在我的 Python 代码中,我在 PROJECT_ID 中拼接了一个变量,该变量设置为我正在引用的项目 ID。

最初将光标after: CURSOR替换为"",然后在下一页我设置cursor = 'after:\\"' + response["data"]["node"]["items"]["pageInfo"]["endCursor"] + '\\"'

我的完整代码在我的 dump_cards 实用程序的 atdumpmemex 模块中。

这里的关键是获取pageInfo以及其他相关节点,然后在每次hasNextPage为真时获取endCursor,以便将其输入到下一次迭代的查询中。

pageInfo 看起来像:

"pageInfo": {
  "hasNextPage": false,
  "endCursor": "Y3Vyc29yOnYyOpHOAAhOsg=="
}

目前 endCursor 是 base64 编码的 cursor:v2:XYZ,但不要依赖它,因为 GitHub 已将其他 ID 从 base64 编码转移到其他方案。

【讨论】:

    【解决方案2】:

    使用 endCursor 进行分页是一种解决方案。与 REST API 一样,您无法务实地遍历所有页面。正如您指定的那样,您可以指定 endCursor 并在有下一页时遍历

    【讨论】:

      猜你喜欢
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-02
      • 1970-01-01
      • 1970-01-01
      • 2022-08-13
      相关资源
      最近更新 更多