githubgraphqlgithub-apigithub-graphql

Github GraphQL - Getting a repository's list of commits


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.


Solution

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