Skip to content

Instantly share code, notes, and snippets.

@johndevs
Last active March 19, 2022 20:54
Show Gist options
  • Save johndevs/8bfd1c5338be0e753e56a3794fb0cc3a to your computer and use it in GitHub Desktop.
Save johndevs/8bfd1c5338be0e753e56a3794fb0cc3a to your computer and use it in GitHub Desktop.
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

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

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

@MichaelCurrin
Copy link

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

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

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