vgrec / bazel-graphql-poc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example usage of Bazel + Apollo GraphQL

To build the POC project, run:

bazel build //src/main/java/com/example

How the Gradle Apollo plugin works

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.

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.

About


Languages

Language:Starlark 53.8%Language:Kotlin 46.2%