How can build a tree view from a git repository tree?


#1

This api can help me do that , but how can i do that with GraphQL?
https://api.github.com/repos/s-xq/github-android/contents/docs

The command below return a root view of the git repository and it can only return master branch data.

query getOnlyRootFile {
  repository(owner: "s-xq", name: "github-android") {
    defaultBranchRef {
      target {
        ... on Commit {
          tree {
            entries {
              name
              mode
              type
            }
          }
        }
      }
    }
  }
}

#2

Hey @s-xq,

Our GraphQL API currently doesn’t expose any recursive functionality. To achieve this, you’ll have to manually recurse on the tree entry objects. This query gets more information when the object is a Tree. You can also add ... on Blob { ... } if you want more information about blob tree entries.

query getOnlyRootFile {
  repository(owner: "s-xq", name: "github-android") {
    defaultBranchRef {
      target {
        ... on Commit {
          tree {
            entries {
              name
              mode
              type
              object {
                ... on Tree {
                  entries {
                    name
                    mode
                    type
                    object {
                      ... on Tree {
                        entries {
                          name
                          mode
                          type
                          object {
                            ... on Tree {
                              oid
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Unfortunately, you’ll need to add enough levels to your query to make sure you’re fetching data all the way down the directory structure.

Will this work for you?


#3

That is also a solution to that.

But i try to do that with another idea just now, it can just help me solve the problem.

Thank you for your reply !

query getCurrentLevelTreeView($owner: String!, $reposName: String!, $expression: String!) {
  repository(owner: $owner, name: $reposName) {
    object(expression: $expression) {
      id
      ... on Blob {
        text
      }
      ...on Tree{
        entries{
          name
          mode
          type
        }
      }
    }
  }
}
{
  "owner": "s-xq",
  "reposName": "github-android",
  "expression": "module-repos:app/src/main/java/com/sxq/github/"
  
}

#4

I’m completely new to GraphQL, but can ... on Tree call a fragment that recursively calls itself whenever it encounters a Tree object?

I would do the same for Blobs and get a string of their content, and though I know that binary files don’t work can they at least pass on information that I can use the v3 API to get binary content ‘… on Null’ ?

I’m trying to build a CMS that uses .md files and can store assets in “Bundle” directories, so it would be nice to use Git as the storage backend because I can combine filetypes and gain all the other benefits of version control, I just need a way to get a single JSON tree of my Content structure and methods to pull binary files directly in Node.