Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Github GraphQL - Get all file contents in repository
# Provide $query as a variable.
# The query is the same as you would enter into the search field e.g. "org:johndevs in:name feedreader"
query GetFilesQuery($branch: GitObjectID, $query: String!) {
search(first: 1, type: REPOSITORY, query: $query) {
edges {
node {
... on Repository {
object(expression: "master:", oid: $branch) {
... on Tree {
entries {
name
object {
... on Blob {
text
}
}
}
}
}
}
}
}
}
}
@MichaelCurrin
Copy link

MichaelCurrin commented May 11, 2020

Thanks! I needed something similar to get all the files in a repo and this was most useful.

I linked back here :) https://gist.github.com/MichaelCurrin/6777b91e6374cdb5662b64b8249070ea

@truumahn
Copy link

truumahn commented Jul 2, 2020

Thank you for this gist.

The title is a little misleading though. This query only gets the root level files, you don't get files that are deeper inside directories.

@sorokinvj
Copy link

sorokinvj commented Dec 15, 2020

To query files in a specific directory change "master:" with "master: folder/with/some/subfolders"

@MichaelCurrin
Copy link

MichaelCurrin commented Dec 17, 2020

I can't find anything suitable like master: *.

But I did find a way to get an extra level down using nesting in my query.

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

@fregante
Copy link

fregante commented Feb 12, 2021

Don't use master: but HEAD:

Not every repo has master: as the default branch

@elprl
Copy link

elprl commented Mar 15, 2022

How would one grab the updated date or committedDate for each of these TreeEntries?

@MichaelCurrin
Copy link

MichaelCurrin commented Mar 17, 2022

I tried using Commit instead of Blob and that didn't work. Got all empty objects. {}

              object {
                  ... on Commit {
                    committedDate
                    authoredDate
                  }
                }

@elprl
Copy link

elprl commented Mar 17, 2022

I couldn't figure it out either. Put a stackoverflow question on it:
https://stackoverflow.com/questions/71499519/getting-commit-data-on-file-list-github-graphql-api

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment