To build the POC project, run:
bazel build //src/main/java/com/example
If we check the sources of the Apollo Gradle plugin,
we can see that the plugin functions as a facade
for the apollo-compiler library.
The apollo-compiler
library is responsible for the actual code generation,
and is separate from the implementation of the gradle plugin.
To generate code directly using the 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
.
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.