GraphQL: Get list of tags for repository

I’m trying to get the list of tags for a repo, and for each tag get the name, message, tagger info and target commit oid.

With the v3 REST API I could have done this but very crudely, with essentially N+1 requests to get the target commit info for each tag.

The v4 API seems much more promising, but when I try this:

query($repoName:String!, $repoOwner:String!) {
  repository(name: $repoName, owner: $repoOwner) {
    refs(refPrefix: "refs/tags/", last: 100) {
      nodes {
        name
        target {
          __typename
          ... on Tag {
            oid
            name
            tag_message: message
            tagger {
              email
              name
            }
            target {
              oid
            }
          }
          ... on Commit {
            commit_message: message
          }
        }
      }
    }
  }
}

I notice that some refs/tags/ refs are not of type Tag - but of type Commit and have some of the data I need missing. In some repositories I cannot find any Tag refs even though I see these tags do exist.

Am I doing something wrong here?

1 Like

Hi there @shevron o/

Unsure if you’re still needing assistance here, but if you are, I will do my best :crossed_fingers:

As to why you’re receiving Commit types, it’s because of:

          ... on Commit {
            commit_message: message
          }

You can simply remove this, re-run, and only return tag types.

However, it sounds like you’re having a much more inconsistent experience over all with your usage and I’m curious if you could expand on that. You mentioned:

In some repositories I cannot find any Tag refs even though I see these tags do exist.

…and I would be curious to know if this is from a public repository that you can link to?

As a pure shot in the dark, I would assume that whatever authentication you’re using, doesn’t provide proper scopes to pull that data. Just a guess, and probably wrong, but beyond permissions, I’m not sure why else we’d be missing tags, unless maybe they exist only on a specific branch?

Let us know if you’re still having trouble in calling the GraphQL API in such a way, or if you’ve found your solution already.