jup*_*ror 5 github github-api graphql
我想将 GitHub 存储库用于 Gatsby 站点中的帖子。现在我正在使用两个查询,首先是获取文件的名称:
{
viewer {
repository(name: "repository-name") {
object(expression: "master:") {
id
... on Tree {
entries {
name
}
}
}
pushedAt
}
}
}
Run Code Online (Sandbox Code Playgroud)
第二个获取文件的内容:
{
viewer {
repository(name: "repository-name") {
object(expression: "master:file.md") {
... on Blob {
text
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以获取有关每个文件的创建时间和上次使用 GraphQL 更新的信息?现在我只能获取pushedAt整个存储库而不是单个文件。
您可以使用以下查询来获取文件内容,同时获取此文件的最后一次提交。这样,你也得到了场pushedAt,committedDate并authorDate根据您的需要:
{
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
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我们还需要获取该endCursor字段以获取文件的第一次提交(获取文件创建日期)
例如在Linux repo 上,对于Makefile它给出的文件:
"pageInfo": {
"endCursor": "b29482fde649c72441d5478a4ea2c52c56d97a5e 0"
}
"totalCount": 1806
Run Code Online (Sandbox Code Playgroud)
所以这个文件有 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
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
它返回此文件的第一次提交。
我没有关于游标字符串格式的任何来源"b29482fde649c72441d5478a4ea2c52c56d97a5e 1804",我已经使用其他一些存储库进行了测试,其中包含超过 1000 次提交的文件,似乎它的格式始终如下:
<static hash> <incremented_number>
Run Code Online (Sandbox Code Playgroud)
避免迭代所有提交,以防有超过 100 个提交引用您的文件
这是使用graphql.js 的javascript实现:
<static hash> <incremented_number>
Run Code Online (Sandbox Code Playgroud)