Bazel and GraphQL: how to include the generated sources in the project

Skip to first unread message

Veaceslav Grec

Oct 19, 2022, 4:38:24 PM10/19/22
to bazel-discuss
I am working on a small POC where I am trying to use GraphQL from Bazel. To do that, I created a wrapper around apollo-compiler which given a graphql file and a schema would generate the corresponding source files. 

I am the following issue: 
the generated files are saved in bazel-bin/src/main/java/com/example/apollo, however, when I try to reference any of those classes in my program, they are not visible, i.e: not only are they not visible in the IDE, but even if I reference them by the fully qualified package name and try to build from command line, the compilation would still fail.

So basically my question is, how do I make the generated sources to be included in the project?

Here the compiler rule definition:
def _compiler(ctx):
     out = ctx.actions.declare_directory("apollo")
     inputs = ctx.files.srcs

    args = ctx.actions.args()
        executable = ctx.executable.code_generator,
        arguments = [args],
        inputs = inputs,
        outputs = [out],

    return [DefaultInfo(files = depset([out]))]

compiler = rule(
   attrs = {
     "code_generator": attr.label(
      cfg = "exec",
      default = ":code_generator",
      executable = True,
   "srcs": attr.label_list(
        allow_files = True,
        mandatory = True,
        allow_empty = False,
    implementation = _compiler,
    output_to_genfiles = True,

BUILD file
load("@rules_java//java:defs.bzl", "java_binary")

    name = "code_generator",
    srcs = [
    main_class = "com.example.compiler.Compiler",
    deps = [

The rest of the code can be found here: 

Would appreciate any input.

Derek Perez

Oct 19, 2022, 4:50:10 PM10/19/22
to Veaceslav Grec, bazel-discuss
Any difference if you don’t output to genfiles? 

You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit


Oct 19, 2022, 4:57:27 PM10/19/22
to bazel-discuss
No difference. 


Nov 12, 2022, 6:12:54 PM11/12/22
to bazel-discuss
Your question is not clear. You supposed to have two distinct rules - one to generate sources and one to consume (compile) them. I expect both of those rules instantiated in your BUILD file and then if the first target is called "codegen" you would add it to `srcs` attribute of the second (compiling) rule.
If this is not enough to get you going please provide more detailed example.

Reply all
Reply to author
0 new messages