使用GitHub的GraphQL API

参考这篇文章:
https://www.scaledrone.com/blog/graphql-tutorial-using-github-graphql-api-with-nodejs/
安装这个库:

npm install node-fetch --global

在这里插入图片描述

这个只是一个库,不是client
https://github.com/graphql-python/graphene

有一个能用的client:prisma/python-graphql-client
其实就是这样:

url = "https://api.github.com/graphql"
headers = {'Accept': 'application/json',
                   'Content-Type': 'application/json'}
headers['Authorization'] = 'token b8434f29f0ed57490c8b072cf94d2358e296f000'
#headers['Authorization']  = 'Basic SGF3a2V5ZXNoYWRvdzA3OiFIYXdrZXllX2FkbWluIXXX'
self.endpoint = url
data = '''
query {
    repository(owner:"isaacs", name:"github") {
      issues(states:CLOSED) {
        totalCount
      }
    }
  }
'''
req_data = json.dumps(data).encode('utf-8')
req = urllib.request.Request(self.endpoint, req_data, headers)

直接用nodejs的库,
参考:https://www.scaledrone.com/blog/graphql-tutorial-using-github-graphql-api-with-nodejs/

const fetch = require('node-fetch');
 
const accessToken = 'your_access_token_from_github';
const query = `
  query {
    repository(owner:"isaacs", name:"github") {
      issues(states:CLOSED) {
        totalCount
      }
    }
  }`;
 
fetch('https://api.github.com/graphql', {
  method: 'POST',
  body: JSON.stringify({query}),
  headers: {
    'Authorization': `Bearer ${accessToken}`,
  },
}).then(res => res.text())
  .then(body => console.log(body)) // {"data":{"repository":{"issues":{"totalCount":247}}}}
  .catch(error => console.error(error));

具体的查询可以看:
https://medium.com/@katopz/how-to-search-with-github-graphql-e6c142dc61ed
https://stackoverflow.com/questions/49344444/github-graphql-search-with-filtering

最终终于找到了一个可以用HTTP POST直接发的方式:
在github提供的官方工具:https://developer.github.com/v4/explorer/
在这里插入图片描述
在这里插入图片描述
代码是这样的:

#!/bin/bash
script='query{
  repository(owner: \"caiqiqi\", name: \"AutoLogin\") {
    object(expression: \"dev-v2.8\") {
      ... on Commit {
        history {
          totalCount
          nodes {
            committedDate
          }
        }
      }
    }
  }
}'

script="$(echo $script)"   # the query should be onliner, without newlines
curl --insecure -i -H 'Content-Type: application/json' \
   -H "Authorization: bearer b8434f29f0ed57490c8b072cf94d2358e2xxxbe8" \
   -X POST -d "{ \"query\": \"$script\"}" https://api.github.com/graphql

来源:https://stackoverflow.com/questions/42021113/how-to-use-curl-to-access-the-github-graphql-api

然而,github的GraphQL接口,并不支持Code搜索。
https://stackoverflow.com/questions/45382069/search-for-code-in-github-using-graphql-v4-api
https://github.community/t5/GitHub-API-Development-and/What-is-the-equivalent-graphQL-v4-api-query-for-v3-search-code/td-p/22101

No, there currently isn’t support for code searches using the GraphQL v4 API

Type: CODE is not supported yet. There is no way you can search the code using graphql right now.

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值