Skip to content

Instantly share code, notes, and snippets.

@lukasbach
Created February 23, 2023 01:42
Show Gist options
  • Save lukasbach/ae5ba67460cc7fbe3ed65536b08b2257 to your computer and use it in GitHub Desktop.
Save lukasbach/ae5ba67460cc7fbe3ed65536b08b2257 to your computer and use it in GitHub Desktop.
Generate Github API Graphql Query for fetching all file contents in a repo
const owner = "lukasbach";
const repo = "react-complex-tree";
const branch = "main";
(async () => {
const trees = (await (await fetch(`https://api.github.com/repos/${owner}/${repo}/git/trees/${branch}?recursive=1`)).json()).tree;
const folders = trees.filter(i => i.type === "tree").map(i => i.path);
const queries = folders.map(folder => `
${folder.replace(/[^a-zA-Z0-9]/g, "_")}: object(expression: "${branch}:${folder}") {
... on Tree {
entries {
name
type
mode
object {
... on Blob {
byteSize
isBinary
text
}
}
}
}
}
`);
const query = `
query RepoFiles {
repository(owner: "${owner}", name: "${repo}") {
${queries.join("\n")}
}
rateLimit {
limit
cost
remaining
resetAt
nodeCount
used
}
}
`;
console.log(query)
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment