GraphQL API get list of files related to commit

I’m trying to get the list of files that were changed in a commit, I can see that the only connection that the commit node has with the files is the commitResourcePath.

Is there any way to get the list of files from that commitResourcePath?

This is my GraphQL query:

{
  repository(owner: "bialikover", name: "taco-or-not") {
    ref(qualifiedName: "master") {
      target {
        ... on Commit {
          id
          history(first: 5) {
            pageInfo {
              hasNextPage
            }
            edges {
              node {
                changedFiles
                messageHeadline
                oid
                message
                author {
                  name
                  email
                  date
                }
                commitResourcePath
              }
            }
          }
        }
      }
    }
  }
}

and this is the output:

{
  "data": {
    "repository": {
      "ref": {
        "target": {
          "id": "MDY6Q29tbWl0MTg0OTkwMzEyOjUxODVlYzVlMTBjYWY3N2U0NmU3YjRkMGRlOWM1ZjI4NGI5ZTFjZjg=",
          "history": {
            "pageInfo": {
              "hasNextPage": false
            },
            "edges": [
              {
                "node": {
                  "changedFiles": 1,
                  "messageHeadline": "update readme with demo url",
                  "oid": "5185ec5e10caf77e46e7b4d0de9c5f284b9e1cf8",
                  "message": "update readme with demo url",
                  "author": {
                    "name": "Adán G. Galván González",
                    "email": "bialikoer@gmail.com",
                    "date": "2019-05-09T11:56:56.000-07:00"
                  },
                  "commitResourcePath": "/bialikover/taco-or-not/commit/5185ec5e10caf77e46e7b4d0de9c5f284b9e1cf8"
                }
              },
              {
                "node": {
                  "changedFiles": 1,
                  "messageHeadline": "update dockerfile",
                  "oid": "5b1e1bbd5d39578816be7a05f93e32d8cb09e756",
                  "message": "update dockerfile",
                  "author": {
                    "name": "Adán G. Galván González",
                    "email": "bialikoer@gmail.com",
                    "date": "2019-05-08T18:12:24.000-07:00"
                  },
                  "commitResourcePath": "/bialikover/taco-or-not/commit/5b1e1bbd5d39578816be7a05f93e32d8cb09e756"
                }
              },
              {
                "node": {
                  "changedFiles": 18,
                  "messageHeadline": "init project with taco",
                  "oid": "451bab389b290a0e166cc6b01e887c075f2c8084",
                  "message": "init project with taco",
                  "author": {
                    "name": "Adan G Galvan Gonzalez",
                    "email": "bialikoer@gmail.com",
                    "date": "2019-05-05T01:25:59.000-07:00"
                  },
                  "commitResourcePath": "/bialikover/taco-or-not/commit/451bab389b290a0e166cc6b01e887c075f2c8084"
                }
              }
            ]
          }
        }
      }
    }
  }
}

Ideally I’d like to get the filenames that were changed, it doesn’t matter if I do a second

request and agregate these to the current output object.

One solution is using api v3 to get the list of files changed with the commit id but want to double check if there is an easier way with GraphQL API

Any ideas? 

3 Likes

Unfortunately, the information you’re looking for isn’t currently available via the GraphQL API. You’d have to grab that information via the REST API at this time.

My response is a bit too late, I had the same issue and I have seen this post when I was looking for a solution. So, I created a solution to this problem by using both REST API and GraphQL API. I wrote a blog post about it (https://mohamedsahbi.com/2019/12/24/fix-a-gap-in-github-api-v4/). I hope it will help anyone that is having the same issue.

2 Likes

Is there an update on whether retrieving a list of changed files (ideally with info on addition, deletion, etc.) via the GitHub GraphQL API is now available? Any news? This seems to be pretty basic functionality. Thanks!

No, there are no updates on this. And while it may seem like basic functionality on the surface, there are a number of considerations around maintaining performance and preventing people from accidentally creating queries that won’t return because they take too long to process.

1 Like

@lee-dohm: Thank you for your quick response! :slightly_smiling_face:

preventing people from accidentally creating queries that won’t return because they take too long to process

So even requiring a first: or last: somewhere in the theoretical query for changed files wouldn’t solve the problem?

Given what you’ve stated, should I expect that there will not be support for retrieving the list of changed files in a commit in the GitHub GraphQL API?

If so, and if I have a list of commits for which I want to retrieve their lists of changed files, I would have to send a REST query for each commit in the form of https://api.github.com/repos/[owner]/[repository name]/commits/[commit sha]. Would this be the most efficient way to do it? Or is there a better way?

I can’t state if or when support for retrieving the list of changed files in a commit will or will not be in the GitHub GraphQL API.

In my experience, the most efficient way to get detailed information about the commits in a git repository is virtually always cloning the repository itself and querying git directly.

1 Like