如何获得每个阵列用户的存储库数量? [英] How would I get the number of repositories for each user of an array?

查看:48
本文介绍了如何获得每个阵列用户的存储库数量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用GitHub的v4 API获取一些用户的存储库数量.在他们的资源管理器中,我发现我可以查询 user(login:String!),但是,我只能看到一个用户存储库.是否可以通过GitHub的v4 API遍历用户列表并获取他们的每个存储库计数?


I am trying to get the number of respositories of a few users using GitHub’s v4 API. In their explorer I found that I can query user(login: String!) however, that is only one users repositories count that I can see. Is it possible to loop though a list of users and get their each one of their repository counts using GitHub’s v4 API?

推荐答案

您可以使用别名将更多的 user 查询添加到您的请求中,并以编程方式遍历用户列表来构建graphql查询.在graphql资源管理器中,将是这样的:

You can use aliases to add more user query to your request and build the graphql query programmatically looping through the user list. In the graphql explorer it would be something like this :

{
  torvalds: user(login: "torvalds") {
    repositories() {
      totalCount
    }
  }
  nelenkov: user(login: "nelenkov") {
    repositories() {
      totalCount
    }
  }
  evilsocket: user(login: "evilsocket") {
    repositories() {
      totalCount
    }
  }
}

使用graphql.js的示例:

An example using graphql.js :

var users = [ "torvalds", "nelenkov", "evilsocket"];

var graph = graphql("https://api.github.com/graphql", {
  headers: {
    "Authorization": "Bearer YOUR_ACCESS_TOKEN"
  },
  asJSON: true
});

graph(`
  query {
    ${buildRepoCountFromUsers(users)}
  }
`, {}).then(function(response) {
  console.log(response);
}).catch(function(error) {
  console.log(error);
});

function buildRepoCountFromUsers(users){
  var query = "";
  for (var i = 0; i < users.length; i++){
    query+=` ${users[i]} : user(login: ${users[i]}) {
      repositories() {
        totalCount
      }
    }`;
  }
  return query;
} 

请注意,我在此处选择了用户登录名作为别名键,该键适用于此处的示例,但是如果登录名以数字字符开头或登录名包含连字符,则将无法使用.在这种情况下,您可以使用 user0 user1 等...,并获取每个用户的字段登录名

Note that I've chosen the user login as alias key here which works for the examples here but would not work if the login begins with a numeric character or if login contains hyphen. In that case you may use something like user0, user1 etc... and get the field login for each user

这篇关于如何获得每个阵列用户的存储库数量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆