How to generate java client using Apollo GraphQL? - apollo-client

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

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

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

Related

Maven Project in IntelliJ, include Gradle Plugin

I'm new to IntelliJ and Gradle
I've got a Maven Project with a lot of dependencies which works on it's own.
Now I should use the libraries from that Project and create a Plugin for IntelliJ in Gradle.
I tried various ways to add the dependencies in the IntelliJ Module Settings, which allowed me to use the needed classes to write my code and build it. However, when I tried to start the plugin, it couldn't find the classes anymore.
I think I need to specify these in the build.gradle but I don't understand exactly how, as all the ways I tried didn't worked.
build.gradle:
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.6.5'
}
group 'com.ast.devmate.intellij'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
**compile 'com.ast.devmate.intellij:root:1.0.0-SNAPSHOT'**
}
// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2019.1'
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
}
gets me this:
Could not find com.ast.devmate.intellij:root:1.0.0-SNAPSHOT.
without the line marked with ** I got a lot of
error: package foo does not exist
import foo;
It looks like you're trying to access a custom library using Gradle. You will probably need to use a file dependency: How to add local .jar file dependency to build.gradle file?

Convert .md file into .pdf using Gradle

I´ve been searching for a way to automatically convert my readme.md file into a .pdf using a gradle task. I know that I can do this by using my prompt console, and it works fine, but I want to know if there is a way of doing the same by creating a gradle task. I´ve found some gitHub projects, and I´ve try some of them, but I always get errors in applying the needed plugins. There is a simple way of doing this, I know that gradle has a huge amount of task types, but I can´t find one for this. Can someone help me?
I´ve trying to use the plugin from https://github.com/fntsoftware/gradle-plugin-markdown2pdf, but when I run the implemented gradle task, I get the error:
Could not get unknown property 'MarkdownToPdfTask' for root project 'cms' of type org.gradle.api.Project
My build gradle from root path:
plugins {
id 'java'
id 'war'
id 'jacoco'
id 'eclipse'
id "fr.putnami.gwt" version "0.4.0"
id "info.solidsoft.pitest" version "1.3.0"
id "de.fntsoftware.gradle.markdown-to-pdf" version "1.1.0"
}
repositories {
mavenCentral()
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
testCompile 'junit:junit:4.12'
testCompile 'org.easymock:easymock:2.5.2'
testCompile 'com.google.gwt:gwt-dev:2.8.1'
compile 'net.sourceforge.plantuml:plantuml:8001'
}
task exampleTask1(type: MarkdownToPdfTask){
inputFile = '/PATH/TO/README.md'
outputFile = '/PATH/TO/README.pdf'
}
My build gradle from buildSrc path:
plugins {
id "de.fntsoftware.gradle.markdown-to-pdf" version "1.1.0"
}
repositories {
mavenCentral()
maven {
url 'https://plugins.gradle.org/m2/'
}
}
dependencies {
// compile gradleApi()
compile 'org.codehaus.groovy:groovy:2.4.2'
compile 'commons-io:commons-io:2.4'
compile 'net.sourceforge.plantuml:plantuml:8051'
}
Thank you.
MarkdownToPdfTask is a class, and because it is not in the Gradle namespace (it's from a 3rd party plugin) it needs to be qualified. I can see that the documentation doesn't mention this, but try putting the following at the top of the script:
import de.fntsoftware.gradle.MarkdownToPdfTask
Otherwise, Gradle thinks it is a property.

Passing correct path to a local WSDL file in wsdl2Java task in Gradle Spring Boot project

For this web project I need to generate Java classes from some WSDL files (which I have locally in my resources folder) in order to use them in my service. The final aim is to run jar file on web server but the program can't find path to WSDL file while running as a jar.
I am using wsdl2java to generate these classes and the code, where I do that in my build gradle file, looks like the following:
buildscript{
repositories{
jcenter()
mavenCentral()
}
dependencies {
classpath 'no.nils:wsdl2java:0.12'
}
}
plugins {
id 'org.springframework.boot' version '2.3.5.RELEASE'
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
id 'java'
}
apply plugin: 'no.nils.wsdl2java'
group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
wsdl2java {
wsdlsToGenerate = [
['-p', 'wsdlOne', file("${projectDir}/src/main/resources/wsdl/wsdlOne.wsdl")],
['-p', 'wsdlTwo', file("${projectDir}/src/main/resources/wsdl/wsdlTwo.wsdl")],
['-p', 'wsdlThree', file("${projectDir}/src/main/resources/wsdl/wsdlThree.wsdl")],
['-p', 'wsdlFour', file("${projectDir}/src/main/resources/wsdl/wsdlFour.wsdl")],
['-p', 'wsdlFive', file("${projectDir}/src/main/resources/wsdl/wsdlFive.wsdl")],
]
}
// More lines here...
Project works fine locally, but when I run jar file on the server and test the service which uses WSDL, it crashes with the following exception:
Exception: Failed to access the WSDL at: file:/D:/projects/my_project/src/main/resources/wsdl/wsdlOne.wsdl. It failed with:
/D:/projects/my_project/src/main/resources/wsdl/wsdlOne.wsdl (No such file or directory).
I could replace local addresses of WSDL files with their real http addresses on the web, but I am required to leave them as they are now.
I researched a lot and tried all the possible solutions but I could not find any that would work for my case. I have looked thoroughly (I hope so) through these questions and answers but without results.
If useful in anything, here is the link to see the list of all the optional arguments that can be used to customize code generated from WSDL files by wsdl2Java and you can view the description of wsdl2java gradle plugin here.
I would be very grateful if anyone could give even a slightest hint about what I am missing here.

can't use 'springBoot' scope in kotlin build script

I have a question.
There is a question about spring-boot-gradle-plugin. I written about spring boot example that allows the specified version (ex. 2.2.1.RELEASE).
By the way, springBoot scope is not available when applying spring-boot-gradle-plugin written in legacy plugin application style. But the plugin DSL style is not a problem.
I know that there is no dynamic version assignment in the plugins scope in Kotlin build scripts, so I have to write in the legacy plugin application style.
The code is as follows:
Using legacy plugin application:
buildscript {
repositories {
gradlePluginPortal()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.1.RELEASE")
}
}
apply(plugin = "org.springframework.boot")
springBoot {
mainClassName = "blabla~~"
}
e: .../build.gradle.kts:25:1: Unresolved reference: springBoot
Using the plugins DSL:
plugins {
id("org.springframework.boot") version "2.2.1.RELEASE"
}
springBoot {
mainClassName = "blabla~~"
}
no problem
Thanks! 😄

Gradle subproject ordering and generate jar (spring boot + angular)

I am a Gradle newby. I have the following project setup:
Root
core: contains spring boot 2 application
ui: angular 5 front-end application
Goal: I want to run 'gradle build' from my root folder and it should contain one jar file which includes the Angular app.
I got the 'ui' covered:
apply plugin: "com.moowork.node"
buildscript {
repositories globalRepositories
dependencies {
classpath "com.moowork.gradle:gradle-node-plugin:1.2.0"
}
}
node {
// based on current version of Angular 5
version = "8.9.1"
npmVersion = "5.6.0"
download = true
}
task buildAngular(type: NpmTask) {
args = ['run', 'build']
}
buildAngular.dependsOn(npm_install)
build.dependsOn(buildAngular)
The above gradle definition will build and generate the Angular files in the static backend core application.
The 'core' gradle build file looks like this (I excluded the dependencies), nothing special:
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
group = 'xxx.xxxxxx'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = 1.8
targetCompatibility = 1.8
buildscript {
repositories globalRepositories
ext {
springBootVersion = '2.0.0.M7'
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
jar {
baseName = 'spring-boot-angular'
version = '1.0.0'
}
How can I make this possible? I want the following actions to be triggered when I run 'gradle build' from my root project:
run first 'gradle build' in ui
then second run 'gradle build' in core
use the generated jar file from 'core' as the end result
I can't stand the groovy like syntax, can't wait for Gradle Kotlin DSL to mature :P
Hope somebody can help. I will open source this setup (together with Spring 5, Hibernate 5 and flyway) when I get this up and ready. Thanks in advance.
You need to include the result of the frontend (ui) buildAngular task inside the jar generated in the backend (core) build:
bootJar {
dependsOn ':ui:buildAngular'
into('BOOT-INF/classes/static') {
from "${project(':ui').projectDir}/dist"
}
}
The fact that the bootJar task now depends on the buildAngular task of the frontend will make gradle order them as needed.
You can browse this project of mine to have an example using basically the same setup (except it uses yarn instead of npm to resolve dependencies)
Answer of JB Nizet should work. You can also add the following in the root gradle file:
build.dependsOn("core:build").mustRunAfter("ui:build")
Above answer is in my opinion cleaner.

Resources