我想从Java语言中使用GitHub,但经过广泛的搜索,我没有找到一个库或其他解决方案,可以让我以一种简单易用的方式做到这一点。
官方文档也没有详细说明如何做到这一点。
如何使用GitHub的v4接口?我想基于API本身生成代码,这样我就可以以编程的方式使用它。
发布于 2019-03-08 16:02:17
我建议你尝试一下开源的失眠症。它使您能够从您的GraphQL查询自动生成Java代码,以供OkHttp库使用。
例如:
以下graphQL查询...
query GetIssues($client: String!
) {
organization(login: $client) {
name
repositories(last: 10) {
edges {
cursor
node {
name
url
description
}
}
}
}
}
...will生成以下Java代码片段...
OkHttpClient client = new OkHttpClient();
MediaType mediaType = MediaType.parse("application/graphql");
RequestBody body = RequestBody.create(mediaType, "{\"query\":\"query GetIssues($client: String!) {\\n organization(login: $client) {\\n name\\n repositories(last: 10) {\\n edges {\\n cursor\\n node {\\n name\\n url\\n description\\n }\\n }\\n }\\n }\\n}\\n\",\"variables\":{\"client\":\"AniTrend\"},\"operationName\":\"GetIssues\"}");
Request request = new Request.Builder()
.url("https://api.github.com/graphql")
.post(body)
.addHeader("authorization", "Bearer YOU_BEARER_AUTH_HERE")
.build();
Response response = client.newCall(request).execute();
然后,如果您在查询中使用您所知道的关于GraphQL变量的知识,并将其与您所知道的Java变量相匹配,那么您应该能够修改RequestBody
字符串。这是一个简单而肮脏的解决方案,有很多改进的空间,这也是我目前正在探索的。当我开发一个更健壮的解决方案时,我将重新考虑这一点。
https://stackoverflow.com/questions/49327249
复制相似问题