GitHub GraphQL to Read Repository Contents

1.2k views Asked by At

I'm looking for a way to use the GitHub GraphQL to read repository contents (paths) and then provide a second query to grab the contents of the full path. I started heading down this path for the second query, and it's failing. The former is the more important issue for me right now.

query{
  viewer {
    login
    name
    repository(name:"myrepo") {
      id
      descriptionHTML
        object(expression: "branch:readme.md") {
          id
        }
      }
    }
  }
}
1

There are 1 answers

2
machour On BEST ANSWER

You had an extra } in your query, this is why it was failing. You also want to replace "branch" with the actual branch name ("master" for example)

Here's a complete example that will also get you the file contents:

{
  viewer {
    login
    name
    repository(name: "git-point-playground") {
      id
      descriptionHTML
      object(expression: "master:README.md") {
        id
        ... on Blob {
          text
        }
      }
    }
  }
}