I would like to query the content of markdown files inside a docs folder in a repository. I am using gatsby-source-graphql and GitHub v4 API.
Thus far I've managed to get file contents with this code:
{
github {
search(first: 1, type: REPOSITORY, query: "repo:kalessil/phpinspectionsea") {
edges {
node {
... on GitHub_Repository {
folder: object(expression: "master:docs/") {
... on GitHub_Tree {
entries {
oid
object {
... on GitHub_Blob {
text
}
}
name
}
}
}
}
}
}
}
}
}
The problem is that any type of file might end up inside the directory, such as txt
and will require additional work to filter out.
Is there a way to query files only with md
extension inside the docs/
directory?
EDIT:
It is possible to accomplish in three steps. First by querying all files, filtering and then querying individual files:
{
github {
files: search(first: 1, type: REPOSITORY, query: "repo:kalessil/phpinspectionsea") {
edges {
node {
... on GitHub_Repository {
object(expression: "master:docs/") {
... on GitHub_Tree {
entries {
name
}
}
}
}
}
}
}
content: search(first: 1, type: REPOSITORY, query: "repo:kalessil/phpinspectionsea") {
edges {
node {
... on GitHub_Repository {
object(expression: "master:docs/DEVELOPERS.md") {
... on GitHub_Blob {
text
}
}
}
}
}
}
}
}
Maybe there is a better way?
EDIT2:
I've refactored the code according to @robinmentral (thank you):
{
github {
files: repository(owner: "kalessil", name: "phpinspectionsea") {
... on GitHub_Repository {
object(expression: "master:docs/") {
... on GitHub_Tree {
entries {
name
}
}
}
}
}
content: repository(owner: "kalessil", name: "phpinspectionsea") {
... on GitHub_Repository {
object(expression: "master:docs/DEVELOPERS.md") {
... on GitHub_Blob {
text
}
}
}
}
}
}
But it still requires filtering on JavaScript side. As I need to get all the names first, filter out by extension and query only those files directly.
This is actually the right way to do in gatsby-source-graphql; for anyone who encounters this in the future check out their example project.
Now I'm just curious if it's possible to combine all these operations in one.
Following up on my comment: if you want to query the contents of a specific file (like in your example), you could use this cleaner query: