Created
February 23, 2023 01:42
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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