Javide
Javide

Reputation: 2637

How to fetch GitHub branch names using GraphQL

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}]}]}

Upvotes: 3

Views: 1782

Answers (1)

machour
machour

Reputation: 734

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

Upvotes: 10

Related Questions