carlism
carlism

Reputation: 158

How do I get file contents from github apiv4 on the default branch?

I've had a lot of success pulling README.md content from the github v4 syntax found in this issue as follows:

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

My issue comes when defaultBranchRef.name is not master. We can expect this to be the case more frequently moving forward as people move away from that naming convention for various reasons.

How do I change the expression to reference the repo's default branch name if I don't know it till I query? Or must I make 2 queries per repo?

Upvotes: 1

Views: 421

Answers (1)

Bertrand Martel
Bertrand Martel

Reputation: 45503

You can use HEAD:[path] as expression to get the default branch :

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

Upvotes: 4

Related Questions