5

I'm new to the GraphQL API for Github. I'm trying to get the commit message and pushedAt date on each file, which you typically see on the root webpage of any github repo showing all the files for the branch (e.g. https://github.com/Alamofire/Alamofire ).

.github    Updates for Xcode 13.3 (#3576)                   3 days ago
Tests      Fix 2020 and 2021 Deprecation Warnings (#3555)   last month

My graphql seems to be returning nothing ("commitData": {}) in the Explorer for the commit data. What am I doing wrong?

fragment Files on Tree {
  entries {
    oid
    name
    type
    path
    extension    
    metaData: object {
      ... on Blob {
        byteSize
        isBinary
      }
    }
    commitData: object {
      ... on Commit {
        oid
        pushedDate
        message
      }
    }
  }
}

query Files($owner: String!, $repo: String!, $branch: String!) {
  repository(owner: $owner, name: $repo) {
    ... on Repository {
      object(expression: $branch) {
        ... on Tree {
          ...Files
        }
      }
    }
  }
}

Query Variables:

{
  "repo": "Alamofire",
  "branch": "HEAD:",
  "owner": "Alamofire"
}

1 Answer 1

2

This is expected. The TreeEntry object: GitObject does not expose commit information. Rather, it returns a Blob type (for file objects) or Tree type (for directory paths). How do we know this? The documentation for object ("Entry file object") provides a vague hint, but we can be sure by introspecting the GraphQL __typename meta field to get the object type name:

metaData: object {
  __typename
}

For a file entry, this results in:

"metaData": {
      "__typename": "Blob"
    }

Even though Commit also implements the GitObject interface, the TreeEntry type does not return it. In other words, you can't get there from here. Using this API you need a two step query, it seems.

Sign up to request clarification or add additional context in comments.

1 Comment

So the GitLab GraphQL interface is equally useless as the GitLab REST API: Trivial questions like the one from the OP cannot be answered, just like the simple question about the modification timestamp of a file (e.g. "which was the last commit for a file?"). Quite embarrassing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.