How to get Readme.MD from Github Graphql API?

Viewed 2636

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 Answers

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

Related