Github GraphQL以递归方式列出目录中的所有文件

时间:2017-10-10 14:41:44

标签: github graphql github-api

我想使用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
              }
            }
          }
        }
      }
    }
  }
}

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

3 个答案:

答案 0 :(得分:3)

无法在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开始,然后从那里开始。

答案 1 :(得分:1)

工作example

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

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

答案 2 :(得分:0)

{
  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
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

相关问题