Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Copilot Lvl 2
Message 1 of 7

GraphQL getting filename,file content and commit date

Solved! Go to Solution.

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.

 

 

6 Replies
Highlighted
Community Manager
Message 2 of 7

Re: GraphQL getting filename,file content and commit date

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!

Highlighted
Solution
GitHub Staff
Message 3 of 7

Re: GraphQL getting filename,file content and commit date

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!

Highlighted
Copilot Lvl 2
Message 4 of 7

Re: GraphQL getting filename,file content and commit date

{
  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"
    }
  ]
}

 

Highlighted
Ground Controller Lvl 2
Message 5 of 7

Re: GraphQL getting filename,file content and commit date

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

Highlighted
Copilot Lvl 3
Message 6 of 7

Re: GraphQL getting filename,file content and commit date

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

Highlighted
Copilot Lvl 2
Message 7 of 7

Re: GraphQL getting filename,file content and commit date


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