我试图使用GraphQL库为编码graphql-codegen类型构建一个bazel规则。
gqlgen规则的实现非常简单:
load("@bazel_skylib//lib:paths.bzl", "paths")
load("@build_bazel_rules_nodejs//:defs.bzl", "nodejs_binary")
_SCHEMA = """
{types}:
schema: {schema}
plugins:
- "typescript"
{meta}:
schema: {schema}
plugins:
- "introspection"
"""
_CONFIG = """
overwrite: true
generates:
{instructions}
"""
def _gqlgen_impl(ctx):
config_file = ctx.actions.declare_file(ctx.label.name + ".yml")
out_files = []
instructions = []
for s in ctx.files.srcs:
name = paths.basename(s.path)[:-len(".graphql")]
types = ctx.actions.declare_file("types/{}.ts".format(name))
meta = ctx.actions.declare_file("meta/{}.json".format(name))
out_files.append(types)
out_files.append(meta)
instructions.append(_SCHEMA.format(schema = s.path, types = types.path, meta = meta.path))
instructions = "\n".join(instructions)
ctx.actions.write(
content = _CONFIG.format(instructions = instructions),
output = config_file,
)
ctx.actions.run_shell(
tools = [ctx.executable._codegen],
inputs = depset(ctx.files.srcs + [config_file]),
outputs = out_files,
command = "{gqlgen} --config {cfg}".format(
gqlgen = ctx.executable._codegen.path,
cfg = config_file.path,
),
)输入是一组*.graphql文件,输出是一组对应的*.json和*.ts文件。
我使用的是nodejs规则,因为graphql-codegen是一个节点模块,所以我在gqlgen规则中为它声明了一个规则:
def gqlgen(name, **kwargs):
nodejs_binary(
name = "gqlgen",
data = ["@npm//:node_modules"],
entry_point = "@npm//:node_modules/@graphql-codegen/cli/bin.js",
install_source_map_support = False,
visibility = ["//visibility:public"],
)
_gqlgen(
name = name,
**kwargs
)我的问题在于把这两件事联系起来。我有以下几点:
_gqlgen = rule(
implementation = _gqlgen_impl,
attrs = {
"srcs": attr.label_list(
allow_files = [".graphql"],
),
"_codegen": attr.label(
cfg = "host",
default = "//schemas:gqlgen",
executable = True,
),
},
)但是,请注意,对于_codegen属性,我将可执行文件指定为//schemas:gqlgen,这是错误的,因为我应该能够从任何包中使用gqlgen规则。
有没有方法从调用nodejs_binary中引用rule()?
发布于 2019-12-10 01:21:54
为了回答我的问题,我可以简单地做以下几点:
_gqlgen = rule(
implementation = _gqlgen_impl,
attrs = {
"srcs": attr.label_list(
allow_files = [".graphql"],
),
"_codegen": attr.label(
cfg = "host",
executable = True,
),
},
)
def gqlgen(name, **kwargs):
nodejs_binary(
name = "gqlgen",
data = ["@npm//:node_modules"],
entry_point = "@npm//:node_modules/@graphql-codegen/cli/bin.js",
install_source_map_support = False,
visibility = ["//visibility:public"],
)
_gqlgen(
name = name,
_codegen = "gqlgen",
**kwargs
)https://stackoverflow.com/questions/59241815
复制相似问题