githubgraphqlgithub-apigithub-api-v4

How can I access github repository contents using graphql (v4 api)?


If I use github v3 api to access the directory contents of a public repository using the following query:

curl https://api.github.com/repos/w3c/webappsec/contents/

what is the equivalent in graphql?

I can get for example the description of the repository by sending the following to: https://api.github.com/graphql

query TestQuery{
    repository(owner:"w3c" name:"webappsec"){
      description
    }
  }

But how can I get the contents of a repository's directory?


Solution

  • You can use object(expression: "branch_name:") and list the tree entries:

    {
      repository(owner: "w3c", name: "webappsec") {
        object(expression: "master:") {
          ... on Tree {
            entries {
              name
              type
              mode
            }
          }
        }
      }
    }
    

    Try it in the explorer