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

293 views
Skip to first unread message

Veaceslav Grec

unread,
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()
    args.add(out.path)
    args.add_all(ctx.files.srcs)

    ctx.actions.run(
        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")

java_binary(
    name = "code_generator",
    srcs = [
        "Compiler.java",
    ],
    main_class = "com.example.compiler.Compiler",
    deps = [
         "@maven//:com_apollographql_apollo3_apollo_compiler",
    ],
)

The rest of the code can be found here: https://github.com/vgrec/bazel-graphql-poc 

Would appreciate any input.

Derek Perez

unread,
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 bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/18d1c113-152e-4b32-8878-7e6b30aedf1cn%40googlegroups.com.

vgrec

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

Konstantin

unread,
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.

Konstantin
Reply all
Reply to author
Forward
0 new messages