GraphQL查询以从GitHub存储库获取文件信息 [英] GraphQL query to get file info from GitHub repository
问题描述
我想在我的Gatsby网站中使用GitHub存储库发布帖子.现在,我正在使用两个查询,首先要获取文件名:
I would like to use GitHub repository for posts in my Gatsby site. Right now I'm using two queries, first to get the names of the files:
{
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
而不是单个文件.
Is there any way to get information about when each file was created and last updated with GraphQL? Right now I can get only pushedAt
for the whole repository and not individual files.
推荐答案
您可以使用以下查询获取文件内容,并同时获取该文件的最后一次提交.这样,您还可以根据需要获取pushedAt
,committedDate
和authorDate
字段:
You can use the following query to get the file content and at the same time getting the last commit for this file. This way you also get the fields pushedAt
, committedDate
and authorDate
depending on what you need :
{
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
字段,以便对文件进行首次提交(以获取文件创建日期)
Note that we need to also get the endCursor
field in order to get the first commit on the file (to get the file creation date)
例如,对于Makefile
文件,在 Linux存储库上:
For instance on the Linux repo, for the Makefile
file it gives:
"pageInfo": {
"endCursor": "b29482fde649c72441d5478a4ea2c52c56d97a5e 0"
}
"totalCount": 1806
因此该文件有1806次提交
So there are 1806 commit for this file
为了获得第一个提交,请引用最后一个游标的查询,该查询应为b29482fde649c72441d5478a4ea2c52c56d97a5e 1804
:
In order to get the first commit, a query referencing the last cursor which would be 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
}
}
}
}
}
}
}
返回此文件的第一次提交.
which returns the first commit of this file.
我没有有关游标字符串格式"b29482fde649c72441d5478a4ea2c52c56d97a5e 1804"
的任何资料,我已经对其他一些存储库进行了测试,这些存储库的文件提交次数超过1000,看来它总是像这样格式化:
I don't have any source about the cursor string format "b29482fde649c72441d5478a4ea2c52c56d97a5e 1804"
, I've tested with some other repositories with files with more than 1000 commits and it seems that it's always formatted like :
<static hash> <incremented_number>
避免在引用文件的提交超过100次的情况下迭代所有提交
which avoid to iterate over all the commits in case that there is more than 100 commits referencing your file
这是 javascript 的问题的实现,使用 graphql.js :
Here is an implementation in javascript using 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);
});
这篇关于GraphQL查询以从GitHub存储库获取文件信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!