FTM*_*FTM 5 github github-api graphql github-graphql
我正在使用GraphQL使用Github的GraphQL(v4)API从存储库列表中获取一些数据。我想从存储库获取最新提交的列表,无论提交的分支/标记/引用是什么。
现在,我正在执行以下操作以从某个存储库获取提交列表:
... on Repository{
refs(refPrefix:"refs/",orderBy:$refOrder,first:1){
edges{
node{
... on Ref{
target{
... on Commit{
history(first:10){
totalCount
edges{
node{
... on Commit{
committedDate
}
}
}
}
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
$refOrder我与请求一起发送的对象在哪里,定义如下:
{
"refOrder": {
"direction": "DESC",
"field": "TAG_COMMIT_DATE"
}
}
Run Code Online (Sandbox Code Playgroud)
这段代码可以正常工作,但是无法获取我想要的结果。返回的响应带有提交列表,但不一定是存储库中的最后提交。当我进入存储库页面并单击“提交”时,通常会看到一个列表,这些列表比我从API调用得到的结果要新。
我想念什么?我应该尝试其他方法refPrefix还是orderBy争论?我已经尝试将“ master”用作refPrefix,但是遇到了同样的问题。
如果您也有兴趣获取所有分支(不仅仅是默认分支)的最新提交,您可以使用前缀请求参考refs/heads/:
{
repository(owner: "bertrandmartel", name: "callflow-workshop") {
refs(refPrefix: "refs/heads/", orderBy: {direction: DESC, field: TAG_COMMIT_DATE}, first: 100) {
edges {
node {
... on Ref {
name
target {
... on Commit {
history(first: 2) {
edges {
node {
... on Commit {
committedDate
}
}
}
}
}
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
在你的情况下 usingrefs/也给了你标签引用。
刚刚意识到,我在寻找的是一个Repository名为的对象中存在的字段defaultBranchRef。使用此字段,我可以检索所需的数据。
我的查询现在看起来像这样:
... on Repository{
defaultBranchRef{
target{
... on Commit{
history(first:10){
edges{
node{
... on Commit{
committedDate
}
}
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)