简体   繁体   English

GitHub的GraphQL API如何从Java中使用?

[英]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.

相关问题 如何使用Java访问github graphql API - how to access github graphql API using java 如何从Java访问github graphql API而不在Java内部运行curl命令 - how to access the github graphql API from java without running curl commands inside java 如何从Java类检查应用程序是否已使用消息? - How can I check from a Java class that an application has consumed a message? 如何使用HttpUrlConnect用Java查询Github graphql API - How to query Github graphql API with java using HttpUrlConnect 如何在Java中执行graphql? - How can I execute graphql in java? 如何从github构建其他人的项目? - How can I build other people's projects from github? 如何使用自定义 Java 库(来自 github) - How can I use custom java library (from github) 公开WCF服务以便从Java / CXF轻松使用它的最佳方法是什么? - What is the best way to expose a WCF service so that it can be easily consumed from Java/CXF? 我们如何手动重置通过 Spring Boot Java 应用程序消耗的 kafka 主题的偏移量? - How can we manually reset the offset of a kafka topic which is consumed through a spring boot java application? 在Spark的Java API中,如何从数据集中选择列 <Row> 使用正则表达式? - In Spark's Java API, how can I select columns from a Dataset<Row> using regular expressions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM