@aappddeevv/graphql-codgen-scala 中文文档教程

发布于 5年前 浏览 22 项目主页 更新于 3年前

graphql-codegen-scala

@graphql-codegen 工具链的 scala 代码生成器。

代码生成器的开发似乎已经从 这个插件的基础 apollo 工具。 @graphql-codgen 使用 插件的概念允许您在不同的代码生成器上分层 任务以达到预期的输出。

这个代码生成器目前专注于为 scala.js 前端。

npm i -D @aappddeevv/graphql-codegen-scala

您应该使用其他@graphql-codgen 插件来实现 所需的输出包:

# yml configuration
schema: "schema.json" //generated separately from server
documents: "awesome_sub_project/src/main/graphql/*.graphql
generates:
  awesome_sub_project/target/scala-2.13/src_managed/main/cli_codegen/graphql.scala
    plugins:
      - graphql-codgen-scala
      - add: "// DO NOT EDIT"
      - add: "package awesome"
    config:
      gqlImport: "apollo_boost._#gql 
    hooks:
      afterOneFileWrite:
        - scalafmt

`

如果你想阅读干净的源代码,你只需要 scalafmt 钩子。 跳过 gqlImport 意味着文档没有内联到 操作对象。 apollo_boost 是 scala.js 发布的包 它有一个 gql 函数,它接受一个字符串并产生一个 graphql DocumentNode 通过 graphql 包编写 javascript 并由 apollo_boost scala.js 门面作为门面。

您不必对输出路径进行硬编码,您可以通过 将 sbt 的信息输出到“运行”脚本中。

sbt

您将希望直接将源代码生成到 项目的 src_generated 目录,然后 确保观看内容。 你可以拥有这个代码生成器 使用 sbt-cli-codgen 自动触发 插件并简单地将其配置为在源时运行 改变。

sbt-cli-codgen 插件安装到 project/plugins.sbt 中,然后:

// build.sbt
mycommand = (files: Seq[String]) => (Seq("npx", "graphql-codegen"), Seq("graphql.scala"))
lazy val proja = project.in(file("proja"))
  .enablePlugins(CLICodgenPlugin)
  .settings(
     codegenCommand := mycommand,
     codegenInputSources := Seq(sourceDirectory.value.toGlob / "proja/src/main/graphql/*.graphql")
  )

上面的配置假定了一些 sbt-cli-codegen 默认值。 mycommand 制定了 要运行的命令和输出文件。 如果你需要将 sbt 中的内容插入到 yaml 配置文件,您可以在函数内部执行副作用。

graphql-codegen-scala

A scala code generator for the @graphql-codegen toolchain.

Development of code generators seems to have shifted from the base apollo tools to this plugin. @graphql-codgen uses the concept of plugins to allow you to layer on different codegen tasks to achieve the desired output.

This codegen is currently focused on generating code for scala.js frontends.

npm i -D @aappddeevv/graphql-codegen-scala

You should use the other @graphql-codgen plugins to achieve the desired output package:

# yml configuration
schema: "schema.json" //generated separately from server
documents: "awesome_sub_project/src/main/graphql/*.graphql
generates:
  awesome_sub_project/target/scala-2.13/src_managed/main/cli_codegen/graphql.scala
    plugins:
      - graphql-codgen-scala
      - add: "// DO NOT EDIT"
      - add: "package awesome"
    config:
      gqlImport: "apollo_boost._#gql 
    hooks:
      afterOneFileWrite:
        - scalafmt

`

You only need the scalafmt hook if you want to read clean sources. Skipping gqlImport means that the documents are not inlined into the operation objects. apollo_boost is scala.js published package that has a gql function that takes a string and produces a graphql DocumentNode via the graphql packages written in javascript and facaded by the apollo_boost scala.js facade.

You do not have to hardcode the output path, you could pass output information from sbt into the "run" script.

sbt

You will want to generate your sources directly into the src_generated directory of your project and then ensure that the contents are watched. You can have this codegen automatically triggered by using the sbt-cli-codgen plugin and simply configuring it to run when the sources change.

Install the sbt-cli-codgen plugin into project/plugins.sbt, then:

// build.sbt
mycommand = (files: Seq[String]) => (Seq("npx", "graphql-codegen"), Seq("graphql.scala"))
lazy val proja = project.in(file("proja"))
  .enablePlugins(CLICodgenPlugin)
  .settings(
     codegenCommand := mycommand,
     codegenInputSources := Seq(sourceDirectory.value.toGlob / "proja/src/main/graphql/*.graphql")
  )

The above config assumes a few sbt-cli-codegen defaults. mycommand formulates the command to be run and the output file. If you need to insert content from sbt into the yaml config file, you could perform a side-effect inside the function.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文