3

Using GitHub GraphQL API (v.4) I would like to get all the branch names existing on a given repository.

My attempt

{
  repository(name: "my-repository", owner: "my-account") {
    ... on Ref {
      name
    }
  }
}

returns error:

{'data': None, 'errors': [{'message': "Fragment on Ref can't be spread inside Repository", 'locations': [{'line': 4, 'column': 13}]}]}
Javide
  • 2,477
  • 5
  • 45
  • 61

1 Answers1

10

Here's how to retrieve 10 branches from a repo:

{
  repository(name: "git-point", owner: "gitpoint") {
    refs(first: 10, , refPrefix:"refs/heads/") {
      nodes {
        name
      }
    }
  }
}

PS: You usually use spread when dealing with an Union type (like IssueTimeline for example, which is composed of different kind of objects, so you can spread on a particular object type to query specific fields.


You might need to use pagination to get all branches

dan1st
  • 12,568
  • 8
  • 34
  • 67
machour
  • 704
  • 1
  • 7
  • 16
  • Is there a way to get **all** branches of a repository instead of `first: 10`? The API gives me an error if I don't supply a `first` argument to `refs`, but how do I tell the API that I want all branches? – hpy Jul 11 '20 at 22:19