Using GraphQL in Bazel - bazel

There's a Gradle Plugin for GraphQL that one can easily integrate in Gradle Projects. As we are moving away from Gradle to Bazel, I am not sure how to migrate a specific module that currently makes use of the Apollo Gradle Plugin.
So imagine that in Gradle the current setup is like this:
plugins {
// ...
id("com.apollographql.apollo3").version("3.6.2")
}
apollo {
packageName.set("com.example.rocketreserver")
srcDir("/src/main/java/com/example/rocketreserver/graphql")
}
How would one go about converting this to Bazel?
Is it possible, somehow, to generate a wrapper (maybe custom rule?) on top of the original plugin and use it in Bazel, or a new plugin from scratch should be written in this case?
Any input is highly appreciated.

There's no Bazel equivalent for the Apollo Gradle plugin, however, building
one for Bazel is pretty straightforward.
How the Gradle Apollo plugin works
If we check the sources of the Apollo Gradle plugin,
we notice that the gradle plugin is nothing but a facade
in front of the apollo-compiler library.
apollo-compiler is a separate library responsible for the actual code generation, and this library is decoupled from the gradle plugin implementation.
To trigger code generation directly via apollo-compiler
one can use:
import java.io.File
val outputDir = File("apollo")
val queryFile = File("queries.graphql")
val schemaFile = File("schema.graphqls")
val testDir = File("apolloTest")
val packageName = "com.example"
val options = Options(
setOf(queryFile),
schemaFile,
outputDir,
testDir,
packageName
)
ApolloCompiler.write(options)
The generated sources will be placed in a directory defined by outputDir in a package defined by packageName.
Building a Bazel rule that triggers the code generation
With the above in place we can now create a Bazel rule that takes as input the schema and .graphql files
and produces as output the generated source code. For convenience, the generated
source code is zipped into a source jar, and this source jar becomes the target's output
which other rules can depend on.

Related

Jenkins Pipeline - Workflow CPS plugin JAR dependencies

I'm implementing unit tests for code used in a Jenkins Pipeline Shared Groovy Library. Specically, I need to mock the steps object available in the Jenkinsfile, which is an instance of org.jenkinsci.plugins.workflow.cps.DSL. In my Gradle build I've specified a dependency like so:
testCompile group: 'org.jenkins-ci.plugins.workflow', name: 'workflow-cps', version: '2.30', ext: 'jar'
which is the project hosting the class above. Without specifying the ext as a JAR, Gradle retrieves the .hpi file since this is the packaging defined in the project's pom.xml; obviously I need to override this and fetch the JAR for my project. However, in doing this Gradle does not download the transitive dependencies of the workflow-cps JAR and I find myself having to populate my build.gradle with all the dependencies determined via trial and error. Is there a way to retrieve the transitive dependencies, or is this a limitation of the workflow-cps project and how it defines its pom.xml?
The CPS class you're wanting to mock won't contain methods/variables introduced by plugins or your workflow lib, so this approach probably won't be fully satisfying.
Facing the same challenge I took the pragmatic approach of making my own TestScript interface in my test sources and Mock that, and not typing the script reference in classes.
The drawbacks are not having IDE code inspection for stuff referencing the script, and having to manually add signatures to TestScript as I add tests (which is also error-prone, since I have manually ensure that those signatures match).
But it works, and avoids getting dragged into plugin dependency hell.

sonar multi-module scan with java libraries

I'm running sonar scan with following versions:
ant v3.0.5
sonar v4.5.4
sonar-ant-task v2.3
My ant project contains 100+ submodules; about half of them have external libs, and half don't
In my ant build file, following sonar properties are set:
sonar.projectKey = com.foo:bar
sonar.projectName = foobar
sonar.projectVersion = ${build.version.major}.${build.version.minor}.${build.version.subminor}
sonar.sourceEncoding=UTF-8
sonar.language = java
sonar.sources = src
sonar.java.binaries = build/classes
sonar.java.libraries = build/dependency/*.jar
Initial problem with above is that for the modules that don't have external libs, it fails since there's no jar inside /build/dependency after compilation.
According to this archive link: http://sonarqube-archive.15.x6.nabble.com/Analysis-aborts-because-of-quot-No-files-nor-directories-matching-lib-jar-quot-td5035215.html
I should be able to change the libraries property to
sonar.java.libraries = build/dependency/*
But this did not work for the combination of app versions i listed above. Using only "*" results in class not found error so i don't even think it correctly grabs the dependency jar files.
Could anyone advise if i'm using incorrect combination of the versions, or if this have regressed?
My current workaround:
Keep the "*.jar" as default project property, and add individual module's libraries property to the empty folder for those that do not have dependency jar. e.g.
module1.sonar.projectName=module1
module1.sonar.projectBaseDir=modules/module1
module1.sonar.java.libraries=build/dependency
module2.sonar.projectName=module2
module2.sonar.projectBaseDir=modules/module2
module2.sonar.java.libraries=build/dependency
... x 50 more of these
Is there more elegant solution?
thanks,
Scott
sonar.java.libraries property is handled by the SonarQube Java plugin. Please provide its version and if it is not the latest try to update.
You're on the right track. The analysis of Java multi-module projects is only easy for Maven users. :-(

Grails inline plugin notation for Java project dependencies?

I've successfully used Grails's inline / inplace plugin notation to develop my Grails app and several plugins concurrently. I only have to compile my Grails app and all the inline plugins get compiled too (great!):
grails.plugin.location.myFooPlugin = '../plugins/foo-plugin'
Can I do the same thing for a Java dependency rather than a Grails plugin?
Let's say I have some Java project that ultimately produces a JAR, but rather than compile and store the JAR in my Maven local repo I'd like to simply compile my Grails app and have the Java project's code also compiled as a result. Possible? If so, what are the rules, such as dir structure adherence? I might want to use Gradle or Maven, not sure.
I'm using a java dependency in my Grails project with help of /scripts/_Events.groovy:
eventCompileStart = {
projectCompiler.srcDirectories << "${basedir}/../your_java_proj/src".toString()
}
The java project will be compiled automatically along with e.g. grails war commando.

How to install gradle-grails-plugin?

Complete gradle nooby here.
I want to be able to execute grails build commands like "grails compile", "grails test-app" and so forth from gradle.
I found the grails-gradle-plugin from Peter Ledbrook at: https://github.com/grails/grails-gradle-plugin.
Cloning the repository I get some files and folders. In the readme file it says " include the required JARs via buildscript {} and 'apply' the plugin". The apply part sure I get but how do I add the JAR? And which jar? Do I need to use gradle on the build file in the folder of the downloaded plug-in and compile a jar? And ones I get the jar where do I place it and how do I include it in my projects build.gradle file?
I have a feeling this is going to be ridiculously easy but I just can't get it to work.
In Gradle, the jars are added to build script or to your application class path through dependencies closure e.g.
dependencies {
compile "org.grails:grails-crud:1.3.4"
compile "org.grails:grails-gorm:1.3.4"
compile "ch.qos.logback:logback-core:1.0.7"
compile "org.slf4j:slf4j-api:1.7.2"
}
compile is a name of one of the many configurations (there are also test, runtime etc.) and e.g. "org.grails:grails-crud:1.3.4" is a reference to a jar in one of the public repositories, which are also specified in your scripts in repositories closure.
You can read more about Gradle dependency management in http://gradle.org/docs/current/userguide/dependency_management.html.
For your Grails project you need to define a build.gradle file which looks similar to what is described in the README.
Though I tried today to just create a simple Grails project using that plugin and gradle init command and it didn't work. I have created an issue for that: https://github.com/grails/grails-gradle-plugin/issues/16.

Manage source and javadoc for Grails automatic dependencies in IntelliJ IDEA?

How do I attach source and javadoc to libraries in IntelliJ IDEA that are linked automatically by Grails dependency resolution and are not explicitly listed in the IDEA project settings?
For example in
BuildConfig.groovy:
grails.project.dependency.resolution = {
repositories {
mavenRepo "http://oss.sonatype.org/content/repositories/releases/"
}
dependencies {
runtime 'org.elasticsearch:elasticsearch:0.17.7'
}
}
Is there a method to have IDEA automatically pick up the source and javadoc from the Maven repository?
IDEA attached the source automatically to the project plugin module. The JavaDoc can be manually attached to the plugin module in the project structure interface at:
File -> Project Structure -> Project Settings -> Modules -> project name-grailsPlugins -> Paths -> JavaDoc
IDEA 11 will be ably to search and download sources for any jar.
The search is performed using https://oss.sonatype.org/ by artifactId and version got from name of jar.
not an answer, but an idea:
you might hook into the 'grails integrate-with' gant script and add this functionality yourself. $GRAILS_HOME/scripts/IntegrateWith.groovy uses some templates for .ipr and .iml file that could be modified.
At some point in future it is excepted that gradle will be used for building Grails projects but there's still a long way to go.

Resources