GraphQL API returning a specific file's text at a commit

Hi,

I am running into a problem getting a specific text file’s contents at a certain commit using GraphQL

  1. I can get the latest version of the file using the Repository Object

    query = {
    repository(owner: “” name:""){
    object(expression: “:”){
    … on Blob{
    text
    }
    }
    }
    }

  2. I can get all the files in the root directory at a certain commit

    query = {
    repository(owner: “” name:""){
    object(oid:""){
    … on Commit{
    oid
    tree{
    entries{
    name
    object{
    … on Blob{
    text
    }
    }
    }
    }
    }
    }
    }
    }

But I cannot figure out how to only get the file I am interested in.  Is this possible? And if so, what would the query look like?

Hi @benjaminwinokur!

I believe this can be done by mashing up your first and second query by using the OID and the path to the file, something like this:

query {
  repository(owner: "blah", name: "blah") {
    folder: object(expression: "$OID:README.md") {
      ... on Blob {
        text
      }
    }
  }
}

Using this I was able to get the contents of the README at any commit, let me know if that works for you!

1 Like