6

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!

user5435999
  • 113
  • 4
  • 10

2 Answers2

15

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
      }
    }
  }
}
machour
  • 704
  • 1
  • 7
  • 16
  • Thanks for the answer. I saw that only 5 fields are listed under Git object: abbreviatedOid (String!) | commitResourcePath (URI!) | commitUrl (URI!) | oid (GitObjectID!) | repository (Repository!) and none of them are blobs. Can you show me how exactly you get to the blob? thank you! – user5435999 Sep 18 '17 at 19:07
  • Marked! Thanks a lot – user5435999 Sep 25 '17 at 18:09
  • 2
    Note that this is case sensitive - so this has a caveat of not supporting readme files that are lowercase. Does anyone know how to make this case insensitive? – Secret Nov 05 '17 at 16:56
  • 2
    GitHub started using "main" as the default branch name. You should use HEAD:README.md instead of master:README.md – aabuhijleh May 18 '21 at 10:35
  • @aabuhijleh it depends on when the repository was created. – machour May 19 '21 at 11:29
  • Regarding Github enterprise: This query totally works for me using my personal git-hub but I get a null object back for GHE, any ideas on how to make it work there? GHE Endpoint: https://github.comcast.com/api/graphql – jWolf Nov 21 '22 at 19:39
3

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