How to get Readme.MD from Github Graphql API?

3.1k views Asked by At

The v3 has a specific API for retrieving the readme.md file. But in the new V4 GraphQL, there is no such field in the Repository Object.

Does anyone know how to retrieve the readme file?

Thanks!

2

There are 2 answers

6
machour On BEST ANSWER

There is not yet a specific entity to get the README.md file, but you can retrieve it as you would normally retrieve any other file:

{
  repository(owner: "gitpoint", name: "git-point") {
    object(expression: "master:README.md") {
      ... on Blob {
        text
      }
    }
  }
}
0
user2891028 On

It looks like because the GitObject implements Blob, you can use the "... on" syntax to access it's properties, which will contain the contents of the object.

In order to access the object in question, pass in the branch and filename with the extension in the format "branch:filename.ext" and retrieve the Blob from the result, and the text from that.

Multiple objects can be retrieved simultaneously, allowing you to check for alternate casings, such as lowercase "readme.md" names. Simply provide aliases for the objects. Example below.

   {
    repository(owner: "owner", name: "name") {
      upCase: object(expression: "master:README.md") {
        ... on Blob {
          text
        }
      }
      object(expression: "master:readme.md") {
        ... on Blob {
          text
        }
      }
      otherFile: object(expression: "master:index.js") {
        ... on Blob {
          text
        }
      }
    }

This may help explain the "... on" syntax. https://graphql.github.io/graphql-spec/June2018/#sec-Inline-Fragments