GraphQL getting filename,file content and commit date

I’am able to get filename, file content and commit date from github using graphql by three queries.

following are the querys :

# for getting filenames
query = """
        {
          repository(owner: \""""+ownerVal+"""\", name: \""""+repoVal+"""\") {
            object(expression: \""""+branchVal+":"+folderVal+"""\") {
          ... on Tree{
                entries{
                  name
                }
              }
            }
          }
        }
        """


# for getting file content
query = """
    {
      repository(owner: \""""+ownerVal+"""\", name: \""""+repoVal+"""\") {
        object(expression: \""""+branchVal+":"+folderVal+"/"+data['name']+"""\") {
             ... on Blob {
            text
          }

        }
      }
    }
    """




# for getting commit date
query = """
        {
          repository(owner: \""""+ownerVal+"""\", name: \""""+repoVal+"""\") {
            object(expression: \""""+branchVal+"""\") {
              ... on Commit {
                blame(path: \""""+folderVal+"/"+data['name']+"""\") {
                  ranges {
                    commit {
                      committedDate
                    }
                  }
                }
              }
            }
          }
        }
        """

How to get these three details by running one query? 

ie, for each file in github i need to get filename, file content and commit date of the file.

1 Like

From what I understand, you can’t get all three details by running one query. Since object is a field of repository, you can’t request it multiple times and the expressions don’t match.

I hope that helps!

Hey @anvinraj!

I believe the GraphQL API can do some of you’re looking for by using a feature called aliasing, which allows you to give connections a unique name, and execute multiple of the same connections in a single query. Take your example run against one of my Repositories:

query {
  repository(owner: "nickvanw", name: "ircx") {
    commit: object(expression: "49cff0c1f801176c4cdf774252a5803f25cdb815") {
      ... on Commit {
        oid
      }
    }

    content:object(expression: "master:README.md") {
      ... on Blob {
        text
      }
    }

    filename: object(expression: "master:example/") {
      ... on Tree {
        entries {
          name
        }
      }
    }
  }
}

This returns all three of them, in one query:

{
  "data": {
    "repository": {
      "commit": {
        "oid": "49cff0c1f801176c4cdf774252a5803f25cdb815"
      },
      "content": {
        "text": "ircx\n====\n[![Build Status](https://travis-ci.org/nickvanw/ircx.svg?branch=master)](https://travis-ci.org/nickvanw/ircx)\n\nircx is a very basic IRC bot written on top of the wonderfully small [sorcix/irc](https://github.com/sorcix/irc) library. It's designed to be a small building block, a small example of one way to use the library.\n\nUsing it is very simple, see [the example](example/main.go).\n\nThis example doesn't do anything other than connect to specified channels and idle, but it's trivial to add additional handlers for any IRC event you want.\n\nContext can be passed around by creating custom Handlers and Senders and using them, versus the default sender created, and an empty handler struct.\n"
      },
      "filename": {
        "entries": [
          {
            "name": "main.go"
          }
        ]
      }
    }
  }
}

The only limitation in this is that you cannot use the output of one query into an input field in the same query. However, I do think that there are some optimizations to your query that can be done. For example, I believe you can go directly from the Tree to the object contents, by doing something like this:

query {
  repository(owner: "...", name: "...") {
    folder: object(expression: "master:path/to/folder/") {
      ... on Tree {
        entries {
          oid
          object {
            ... on Blob {
              text
            }
          }
          name
        }
      }
    }
  }
}

Let me know if this makes sense, and if anything else looks amiss! I would also recommend using GraphQL Variables, which would allow you to paramaterize some of what you’re doing, instead of string concatenation.

Let me know if you have any more questions!

10 Likes
{
  repository(owner:"imstack", name:"taskcluster-repoifier" ) {
    object(expression: "master:README.md") {
      ...on Blob {
      text
      byteSize
    }
  }
}

I tried some queries but getting response as below? Any clue pls?

{
  "errors": [
    {
      "message": "Unexpected end of document"
    }
  ]
}

Yes! You have a missing } at the end.

{
repository(owner:“appernetic”, name:“hugo-bootstrap-premium-dev” ) {
object(expression: “master:README.md”) {
…on Blob {
text
byteSize
               }
      }
}
}

Is there any way to get only the files changed in that specific commit?

@bialikover wrote:

Is there any way to get only the files changed in that specific commit?

Just kidding, this query gets all files in the repo…

query {
  repository(owner: "nickvanw", name: "ircx") {
    commit: object(expression: "49cff0c1f801176c4cdf774252a5803f25cdb815") {
      ... on Commit {
        tree {
          entries { name }
        }
      }
    }
  }
}