[英]How can GitHub's GraphQL API can be consumed from Java?
I'd like to consume GitHub's GraphQL API from Java and after extensive searching I was not able to find a library or other solution which lets me do this in an easy to use way. 我想从Java使用GitHub的GraphQL API,经过大量搜索后,我找不到能够使我以一种易于使用的方式进行操作的库或其他解决方案。
The official docs do not detail how this can be done either. 官方文档也没有详细说明如何做到这一点。
How can I consume GitHub's v4 API? 如何使用GitHub的v4 API? I'd like to generate code based on the API itself so I can use it in a programmatic way. 我想基于API本身生成代码,因此可以以编程方式使用它。
I would suggest you try out open source Insomnia. 我建议您尝试开源的失眠症。 It gives you the ability to automatically generate Java code from your GraphQL queries for the OkHttp library to use. 它使您能够从GraphQL查询自动生成Java代码,以供OkHttp库使用。
For example: 例如:
The following graphQL query... 下面的graphQL查询...
query GetIssues($client: String!
) {
organization(login: $client) {
name
repositories(last: 10) {
edges {
cursor
node {
name
url
description
}
}
}
}
}
...will generate the following Java snippet... ...将生成以下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();
Then if you use what you know about GraphQL variables in the queries and mate that to what you know about Java variables, you should then be able to modify the RequestBody
strings. 然后,如果您在查询中使用对GraphQL变量的了解并将其与对Java变量的了解相匹配,则应该能够修改RequestBody
字符串。 It's a simple and dirty solution that has loads of room for improvement, something I am exploring currently as well. 这是一个简单又肮脏的解决方案,有很多改进的余地,我目前也在探索。 I'll revisit this as I develop a more robust solution. 在开发更强大的解决方案时,我将重新讨论这一点。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.