GraphQL查询以从GitHub存储库获取文件信息

时间:2020-06-09 05:32:11

标签: github graphql github-api

我想在我的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,而单个文件则不能。

1 个答案:

答案 0 :(得分:4)

您可以使用以下查询来获取文件内容,并同时获取该文件的最后一次提交。这样,您还可以根据需要获取字段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);
});