【问题标题】:Github GraphQL to recursively list all files in the directoryGithub GraphQL 递归列出目录下的所有文件
【发布时间】:2020-04-18 02:04:58
【问题描述】:

我想使用 GraphQL Github API 递归地列出目录中包含的所有文件。现在我的查询看起来像这样:

{
  search(first:1, type: REPOSITORY, query: "language:C") {
    edges {
      node {
        ... on Repository {
          name
          descriptionHTML
          stargazers {
            totalCount
          }
          forks {
            totalCount
          }
          object(expression: "master:") {
            ... on Tree {
              entries {
                name
                type
              }
            }
          }
        }
      }
    }
  }
}

但是,这只给了我第一级目录内容,特别是一些结果对象又是树。有没有办法调整查询,使其再次递归列出树的内容?

【问题讨论】:

  • 我很高兴看到您的问题,既因为您的示例解决了我在使用 GraphQL 时遇到的问题,又因为我认为我知道解决方案。可悲的是,当我尝试使用“片段”时,它抛出了一个关于“片段 [片段名称] 包含无限循环的错误。所以现在我也在焦急地等待这个问题的答案。

标签: github graphql github-api


【解决方案1】:

在 GraphQL 中无法递归迭代。但是,您可以使用查询变量以编程方式执行此操作:

query TestQuery($branch: GitObjectID) {
 search(first: 1, type: REPOSITORY, query: "language:C") {
    edges {
      node {
        ... on Repository {
          object(expression: "master:", oid: $branch) {
            ... on Tree {
              entries {
                oid
                name
                type
              }
            }
          }
        }
      }
    }
  }
}

null 的值开始,然后从那里开始。

【讨论】:

    【解决方案2】:

    工作example

    更多信息:https://docs.sourcegraph.com/api/graphql/examples

    但这可能会在近功能中发生变化。例如最新的 github 版本是 v4 https://developer.github.com/v4/explorer/

    【讨论】:

      【解决方案3】:
      {
        search(first: 1, type: REPOSITORY, query: "language:C") {
          edges {
            node {
              ... on Repository {
                name
                descriptionHTML
                stargazers {
                  totalCount
                }
                forks {
                  totalCount
                }
                object(expression: "master:") {
                  ... on Tree {
                    entries {
                      name
                      object {
                        ... on Tree {
                          entries {
                            name
                            object {
                              ... on Tree {
                                entries {
                                  name
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
      
      

      【讨论】:

      • 请添加几句话来解释你在做什么,对我来说它看起来并不递归。看起来你只进了几个层次
      猜你喜欢
      • 2010-10-19
      • 2017-10-31
      • 2018-07-13
      • 2010-10-04
      • 2010-10-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-11
      相关资源
      最近更新 更多