【问题标题】:How to generate java client using Apollo GraphQL?如何使用 Apollo GraphQL 生成 java 客户端?
【发布时间】:2019-07-26 19:33:30
【问题描述】:

我尝试了官方文档中提到的步骤,但显示错误:不支持的目标:java

在 CLI 帮助中,java 未列为目标

【问题讨论】:

    标签: apollo-client


    【解决方案1】:

    服务器设置

    带有 GraphQL 服务器的 Spring Boot:https://www.graphql-java.com/tutorials/getting-started-with-spring-boot/

    GitHub 上有完整的 GraphQL 服务器代码:https://github.com/graphql-java/tutorials/tree/master/book-details

    客户

    从架构生成 GraphQL 查询代码

    • 使用以下 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文件(参考:https://www.apollographql.com/docs/android/essentials/get-started.html#creating-graphql-file
    • 将 BookById.graphQL 文件放入具有预期包层次结构的 main.graphql 包中 示例:main/graphql/com/apollographql/apollo/sample/BookById.graphql;
    • 从 GraphQL 服务器端点下载 schema.json 转到 CLI,运行以下命令,它将生成 schema.json

      apollo 服务:下载 --endpoint=http://localhost:8080/graphql

    • schema.json放到main/graphql

    • 运行 Gradle 构建任务,它将在 Build/generated/source/apollo 中生成与 BookById.graphql

      相同的包结构的客户端查询代码

      BookByIdQuery.java

    参考资料:

    服务器 GraphQL-Java:https://www.graphql-java.com/documentation/master/

    Apollo 客户端:https://www.apollographql.com/docs/android/essentials/get-started.html

    带有 GraphQL 查询示例的 Spring Boot |技术入门:https://youtu.be/zX2I7-aIldE

    “使用 GraphQL 和 Spring 构建数据 API”- API Craft Singapore:https://youtu.be/GmR2uIDZEyM

    【讨论】:

    • BookById.graphQL 应该包含什么?我尝试使用query bookById($id: ID!) { book(id: $id) { id name pageCount author { lastName } } } 我得到:` bookquery.graphql (2:4) Can't query book on type Query -------------------- -------------------------------- [1]: 查询 bookById($id: ID!) { [2]: book(id: $id) { [3]: id ------------------------- --------------- `
    【解决方案2】:

    使用代码优先的方法,Java GraphQL 客户端的优雅解决方案是:vertx-graphql-client

    【讨论】:

      猜你喜欢
      • 2020-09-19
      • 2018-08-12
      • 2020-05-03
      • 2020-10-25
      • 2019-09-27
      • 2021-05-30
      • 2019-06-22
      • 2019-01-14
      • 2020-08-27
      相关资源
      最近更新 更多