11

I am using GraphQL to get some data from a list of repositories using Github's GraphQL (v4) API. I want to get a list of the latest commits from a repository, no matter what is the commit's branch/tag/ref.

For now I am doing the following to get the list of commits from a certain repository:

... on Repository{
    refs(refPrefix:"refs/",orderBy:$refOrder,first:1){
        edges{
            node{
                ... on Ref{
                    target{
                        ... on Commit{
                            history(first:10){
                                totalCount
                                edges{
                                    node{
                                        ... on Commit{
                                            committedDate
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

Where $refOrder is an object I am sending together with the request and it is defined below:

{
    "refOrder": {
        "direction": "DESC",
        "field": "TAG_COMMIT_DATE"
    }
}

This piece of code is working, but not retrieving the results I want. The response comes back with a list of commits, but not necessarily the last commits from the repository. When I go to the repository page and click on "Commits", I usually see a list of commits that are more recent than what I got as results from my API call.

What am I missing? Should I try a different refPrefix or orderBy argument? I have already tried "master" as the refPrefix, but faced the same problem.

FTM
  • 1,324
  • 10
  • 29
  • 1
    Learned so much in the last few days about GraphQL that I summarized my endeavor with a post on Medium: https://medium.com/@fabiomolinar/using-githubs-graphql-to-retrieve-a-list-of-repositories-their-commits-and-some-other-stuff-ccbbb4e96d78 – FTM Jan 16 '18 at 20:27

2 Answers2

10

Just realized that what I was looking for is a field which exists in the Repository object called defaultBranchRef. Using this field I was able to retrieve the data I was looking for.

My query now looks like this:

... on Repository{
    defaultBranchRef{
        target{
            ... on Commit{
                history(first:10){
                    edges{
                        node{
                            ... on Commit{
                                committedDate
                            }
                        }
                    }
                }
            }
        }
    }
}
FTM
  • 1,324
  • 10
  • 29
5

If you are also interested in getting the latest commits for all branches (not just the default branch), you can request reference with prefix refs/heads/ :

{
  repository(owner: "bertrandmartel", name: "callflow-workshop") {
    refs(refPrefix: "refs/heads/", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, first: 100) {
      edges {
        node {
          ... on Ref {
            name
            target {
              ... on Commit {
                history(first: 2) {
                  edges {
                    node {
                      ... on Commit {
                        committedDate
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

In your case using refs/ also gave you tag ref.

Try it in the explorer

Bertrand Martel
  • 32,363
  • 15
  • 95
  • 118