Generate scala/scala.js files from graphql schema and operations.

Usage no npm install needed!

<script type="module">
  import aappddeevvGraphqlCodgenScala from 'https://cdn.skypack.dev/@aappddeevv/graphql-codgen-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
      - graphql-codgen-scala
      - add: "// DO NOT EDIT"
      - add: "package awesome"
      gqlImport: "apollo_boost._#gql 
        - 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.


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"))
     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.