参考这篇文章:
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.