如何使用Apollo GraphQL生成Java客户端? [英] How to generate java client using Apollo GraphQL?
问题描述
我尝试了官方文档中提到的步骤,但显示错误:不受支持的目标:java
I have tried steps mentioned in the official doc but it showing Error: Unsupported target: java
在CLI帮助中,未将Java列为目标
In CLI help, java is not listed as a target
推荐答案
服务器设置
使用GraphQL Server进行Spring Boot: https://www.graphql-java.com/tutorials/getting-started-with-spring-boot/
Spring Boot with GraphQL Server: https://www.graphql-java.com/tutorials/getting-started-with-spring-boot/
完整的GraphQL服务器代码可在GitHub上找到: https://github.com/graphql-java/tutorials/tree/master/book-details
Full working GraphQL server code exist on GitHub: https://github.com/graphql-java/tutorials/tree/master/book-details
客户
从架构生成GraphQL查询代码
Generate GraphQL Query Code from schema
- 使用以下build.gradle创建Java项目
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.apollographql.apollo:apollo-gradle-plugin:0.5.0'
}
}
plugins {
id 'java'
}
apply plugin: 'com.apollographql.android'
group = 'com.graphql-java.tutorial'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
repositories {
jcenter()
}
dependencies {
compile group: 'com.apollographql.apollo', name: 'apollo-runtime', version: '0.5.0'
}
- 创建GraphQL查询文件(BookById.graphQL),将生成相同的命名Java文件(参考:
- Create GraphQL Query file (BookById.graphQL), same namedjava file will be generated (Reference: https://www.apollographql.com/docs/android/essentials/get-started.html#creating-graphql-file)
- Put BookById.graphQL file in main.graphql package with expected package hierarchy Example: main/graphql/com/apollographql/apollo/sample/BookById.graphql;
Download schema.jsonfromthe GraphQL server endpoint Go to CLI, run below command, it will generate schema.json
阿波罗服务:下载--endpoint = http://localhost:8080/graphql
-
将 schema.json 放入 main/graphql
运行Gradle构建任务,它将在 Build/generation/source/apollo 中生成客户端查询代码,其包结构与 BookById.graphql
Run Gradle build task it will generate Client Query Code in Build/generated/source/apollo with same package structure as of BookById.graphql
BookByIdQuery.java
BookByIdQuery.java
参考:
服务器GraphQL-Java: https://www.graphql-java.com/documentation/master/
Server GraphQL-Java : https://www.graphql-java.com/documentation/master/
Apollo客户端: https://www.apollographql.com/docs/android/essentials/get-started.html
Apollo Client: https://www.apollographql.com/docs/android/essentials/get-started.html
带有GraphQL查询示例的Spring Boot |技术入门手册: https://youtu.be/zX2I7-aIldE
Spring Boot with GraphQL Query Example | Tech Primers: https://youtu.be/zX2I7-aIldE
使用GraphQL和Spring构建数据API"-新加坡API Craft: https://youtu.be/GmR2uIDZEyM
"Building a data API with GraphQL and Spring" - API Craft Singapore: https://youtu.be/GmR2uIDZEyM
这篇关于如何使用Apollo GraphQL生成Java客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!