【问题标题】:How to batch GitHub GraphQL API queries?如何批处理 GitHub GraphQL API 查询?
【发布时间】:2017-02-01 16:16:42
【问题描述】:

如何将多个查询批处理成对 GitHub 的 GraphQL API 的单个请求?

例如,您如何将这 2 个查询批处理为一个请求并接收一个响应?这种技术是否适用于更多查询(比如 200 个)?

{
  repositoryOwner(login:"rails") {
    repository(name:"rails") {
      description
      homepageURL
    }
  }
}

{
  repositoryOwner(login:"github") {
    repository(name:"graphql-client") {
      description
      homepageURL
    }
  }
}

(可以在https://developer.github.com/early-access/graphql/explorer/ 试用 GitHub GraphQL API)

【问题讨论】:

    标签: github-api graphql


    【解决方案1】:

    您需要将两个字段的调用包装在一个查询中:

    {
      repositoryOwner(login:"rails") {
        repository(name:"rails") {
          description
          homepageURL
        }
      } 
      repositoryOwner(login:"github") {
        repository(name:"graphql-client") {
          description
          homepageURL
        }
      }
    }
    

    这仍然会失败,因为现在输出中有两个具有相同名称(repositoryOwner)的字段,因此您需要为它们起别名:

    {
       rails: repositoryOwner(login:"rails") {
         repository(name:"rails") {
           description
           homepageURL
         }
       } 
       graphql_client: repositoryOwner(login:"github") {
         repository(name:"graphql-client") {
           description
           homepageURL
         }
       }
     }
    

    this explanation

    如果您可以为每个查询生成一个唯一的别名,那么是的,这种技术应该可以正常工作。

    【讨论】:

      猜你喜欢
      • 2017-12-27
      • 2018-08-21
      • 2023-03-08
      • 2018-04-22
      • 2019-10-12
      • 1970-01-01
      • 1970-01-01
      • 2021-01-15
      • 1970-01-01
      相关资源
      最近更新 更多