从 GitHub 存储库获取文件信息的 GraphQL 查询
GraphQL query to get file info from GitHub repository
我想使用 GitHub 存储库来存储我的 Gatsby 站点中的帖子。现在我正在使用两个查询,首先是获取文件的名称:
{
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
获取整个存储库而不是单个文件。
您可以使用以下查询获取文件内容,同时获取该文件的最后一次提交。通过这种方式,您还可以根据需要获得字段 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
}
}
}
}
}
}
请注意,我们还需要获取 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
}
}
}
}
}
}
}
哪个 returns 此文件的第一次提交。
我没有关于游标字符串格式的任何来源 "b29482fde649c72441d5478a4ea2c52c56d97a5e 1804"
,我已经用超过 1000 次提交的文件对其他一些存储库进行了测试,它似乎总是被格式化为:
<static hash> <incremented_number>
避免迭代所有提交,以防超过 100 个提交引用您的文件
这是 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);
});
我想使用 GitHub 存储库来存储我的 Gatsby 站点中的帖子。现在我正在使用两个查询,首先是获取文件的名称:
{
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
获取整个存储库而不是单个文件。
您可以使用以下查询获取文件内容,同时获取该文件的最后一次提交。通过这种方式,您还可以根据需要获得字段 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
}
}
}
}
}
}
请注意,我们还需要获取 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
}
}
}
}
}
}
}
哪个 returns 此文件的第一次提交。
我没有关于游标字符串格式的任何来源 "b29482fde649c72441d5478a4ea2c52c56d97a5e 1804"
,我已经用超过 1000 次提交的文件对其他一些存储库进行了测试,它似乎总是被格式化为:
<static hash> <incremented_number>
避免迭代所有提交,以防超过 100 个提交引用您的文件
这是 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);
});