13

I want to use the GraphQL Github API to recursively list all files contained in the directory. Right now my query looks like this:

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

However, this only gives me only the first level of directory contents, in particular some of the resulting objects are again trees. Is there a way to adjust the query, such that it recursively list the contents of tree again?

xpt
  • 20,363
  • 37
  • 127
  • 216
Sleik
  • 341
  • 4
  • 11
  • 1
    I was excited to see your question, both because your example solved an issue I was having with GraphQL AND because I thought I knew of a solution. Sadly, when I attempted to use a "fragment," it threw an error about "Fragment [fragment name] contains an infinite loop. So now I'm anxiously awaiting an answer to this question, too. – REW Oct 10 '17 at 18:03

3 Answers3

11

There is no way to recursively iterate in GraphQL. However, you can do so programmatically using a query variable:

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

Start with a value of null and go from there.

Scriptonomy
  • 3,975
  • 1
  • 15
  • 23
2

working example

More info: https://docs.sourcegraph.com/api/graphql/examples

But probably this will change in the near feature. For example latest github version is v4 https://developer.github.com/v4/explorer/

FDisk
  • 8,493
  • 2
  • 47
  • 52
  • It still works, however in the Github GraphQl Explorer I get two errors with your example: `"Field 'repository' is missing required arguments: owner"` and `"Field 'commit' doesn't exist on type 'Repository'"` – Timo Sep 01 '22 at 05:57
  • The first error from my comment one hour ago can be solved by using `owner`, but the second one seems not to be cleared. Another query is needed to work with the explorer? – Timo Sep 01 '22 at 07:48
-1
{
  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
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

  • 1
    Please add a few words explaining what you are doing, to me it does not look recursive. It looks like you only go a few more levels deep – Simson Sep 19 '19 at 10:34