【问题标题】:GraphQL query to get file info from GitHub repository从 GitHub 存储库获取文件信息的 GraphQL 查询
【发布时间】:2020-09-28 05:18:09
【问题描述】:

我想在我的 Gatsby 网站上使用 GitHub 存储库来发布帖子。现在我正在使用两个查询,首先是获取文件的名称:

{
  viewer {
    repository(name: "repository-name") {
      object(expression: "master:") {
        id
        ... on Tree {
          entries {
            name
          }
        }
      }
      pushedAt
    }
  }
}

第二个获取文件内容:

{
  viewer {
    repository(name: "repository-name") {
      object(expression: "master:file.md") {
        ... on Blob {
          text
        }
      }
    }
  }
}

有没有什么方法可以获取每个文件的创建时间和最后一次使用 GraphQL 更新的时间?现在我只能为整个存储库而不是单个文件获取pushedAt

【问题讨论】:

    标签: github graphql github-api


    【解决方案1】:

    您可以使用以下查询来获取文件内容,同时获取该文件的最后一次提交。这样,您还可以根据需要获得 pushedAtcommittedDateauthorDate 字段:

    {
      repository(owner: "torvalds", name: "linux") {
        content: object(expression: "master:Makefile") {
          ... on Blob {
            text
          }
        }
        info: ref(qualifiedName: "master") {
          target {
            ... on Commit {
              history(first: 1, path: "Makefile") {
                nodes {
                  author {
                    email
                  }
                  message
                  pushedDate
                  committedDate
                  authoredDate
                }
                pageInfo {
                  endCursor
                }
                totalCount
              }
            }
          }
        }
      }
    }
    

    请注意,我们还需要获取 endCursor 字段才能获取文件的第一次提交(获取文件创建日期)

    例如在Linux repo 上,它给出的Makefile 文件:

    "pageInfo": {
      "endCursor": "b29482fde649c72441d5478a4ea2c52c56d97a5e 0"
    }
    "totalCount": 1806
    

    所以这个文件有 1806 次提交

    为了获得第一次提交,引用最后一个游标的查询将是b29482fde649c72441d5478a4ea2c52c56d97a5e 1804

    {
      repository(owner: "torvalds", name: "linux") {
        info: ref(qualifiedName: "master") {
          target {
            ... on Commit {
              history(first: 1, after:"b29482fde649c72441d5478a4ea2c52c56d97a5e 1804", path: "Makefile") {
                nodes {
                  author {
                    email
                  }
                  message
                  pushedDate
                  committedDate
                  authoredDate
                }
              }
            }
          }
        }
      }
    }
    

    返回此文件的第一次提交。

    我没有关于光标字符串格式"b29482fde649c72441d5478a4ea2c52c56d97a5e 1804" 的任何来源,我已经使用其他一些包含超过 1000 次提交的文件的存储库进行了测试,似乎它的格式总是如下:

    <static hash> <incremented_number>
    

    避免迭代所有提交,以防有超过 100 个提交引用您的文件

    这是在 中使用graphql.js 的实现:

    const graphql = require('graphql.js');
    
    const token = "YOUR_TOKEN";
    const queryVars = { name: "linux", owner: "torvalds" };
    const file = "Makefile";
    const branch = "master";
    
    var graph = graphql("https://api.github.com/graphql", {
      headers: {
        "Authorization": `Bearer ${token}`,
        'User-Agent': 'My Application'
      },
      asJSON: true
    });
    
    graph(`
        query ($name: String!, $owner: String!){
          repository(owner: $owner, name: $name) {
            content: object(expression: "${branch}:${file}") {
              ... on Blob {
                text
              }
            }
            info: ref(qualifiedName: "${branch}") {
              target {
                ... on Commit {
                  history(first: 1, path: "${file}") {
                    nodes {
                      author {
                        email
                      }
                      message
                      pushedDate
                      committedDate
                      authoredDate
                    }
                    pageInfo {
                      endCursor
                    }
                    totalCount
                  }
                }
              }
            }
          }
        }
    `)(queryVars).then(function(response) {
      console.log(JSON.stringify(response, null, 2));
      var totalCount = response.repository.info.target.history.totalCount;
      if (totalCount > 1) {
        var cursorPrefix = response.repository.info.target.history.pageInfo.endCursor.split(" ")[0];
        var nextCursor = `${cursorPrefix} ${totalCount-2}`;
        console.log(`total count : ${totalCount}`);
        console.log(`cursorPrefix : ${cursorPrefix}`);
        console.log(`get element after cursor : ${nextCursor}`);
    
        graph(`
          query ($name: String!, $owner: String!){
            repository(owner: $owner, name: $name) {
              info: ref(qualifiedName: "${branch}") {
                target {
                  ... on Commit {
                    history(first: 1, after:"${nextCursor}", path: "${file}") {
                      nodes {
                        author {
                          email
                        }
                        message
                        pushedDate
                        committedDate
                        authoredDate
                      }
                    }
                  }
                }
              }
            }
          }`)(queryVars).then(function(response) {
            console.log("first commit info");
            console.log(JSON.stringify(response, null, 2));
          }).catch(function(error) {
            console.log(error);
          });
      }
    }).catch(function(error) {
      console.log(error);
    });
    

    【讨论】:

    • 非常感谢您的回复?我还没有尝试过javascript代码,但是GraphQL查询很好用!
    猜你喜欢
    • 2019-12-11
    • 2014-09-29
    • 2018-09-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-25
    • 2018-09-09
    相关资源
    最近更新 更多