How to generate java client using Apollo GraphQL?

纵然是瞬间 提交于 2021-02-07 10:58:28

问题


I have tried steps mentioned in the official doc but it showing Error: Unsupported target: java

In CLI help, java is not listed as a target


回答1:


Server setup

Spring Boot with GraphQL Server: https://www.graphql-java.com/tutorials/getting-started-with-spring-boot/

Full working GraphQL server code exist on GitHub: https://github.com/graphql-java/tutorials/tree/master/book-details

Client

Generate GraphQL Query Code from schema

  • Create Java Project with the following build.gradle
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'
}
  • 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

    apollo service:download --endpoint=http://localhost:8080/graphql

  • Put schema.json in main/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

References:

Server GraphQL-Java : https://www.graphql-java.com/documentation/master/

Apollo Client: https://www.apollographql.com/docs/android/essentials/get-started.html

Spring Boot with GraphQL Query Example | Tech Primers: https://youtu.be/zX2I7-aIldE

"Building a data API with GraphQL and Spring" - API Craft Singapore: https://youtu.be/GmR2uIDZEyM




回答2:


With code-first approach, an elegant solution for Java GraphQL clients is: vertx-graphql-client



来源:https://stackoverflow.com/questions/55001020/how-to-generate-java-client-using-apollo-graphql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!