Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions packages/wabe-documentation/docs/documentation/codegen.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
# Codegen

With Wabe, you have the option to configure code generation (`codegen`). This feature allows you to generate a schema.graphql file that represents the entirety of the schema you've defined using Wabe. This can be particularly useful for generating types for the front-end or for other uses.
With Wabe, you can configure when code generation is triggered with `codegen`, and provide your own generation pipeline with `onGenerateCodegen`. This inversion of control lets you run any tool you want (for example `oxfmt`, `prettier`, or a custom generator), without Wabe enforcing a formatter strategy.

Additionally, the `codegen` creates a `wabe.ts` file in the specified folder. This file contains all the TypeScript types corresponding to your schema. You can, for instance, use it to define [WabeTypes](/wabe/concepts.md#wabetypes) or to have fully typed elements in your backend.
Your callback is fully custom. In this example, we only format generated files with `oxfmt`.

```ts
import { Wabe } from "wabe";
import { resolve } from "node:path";

const run = async () => {
const wabe = new Wabe({
Expand All @@ -14,6 +15,11 @@ const run = async () => {
enabled: true,
path: `${import.meta.dirname}/../generated/`,
},
onGenerateCodegen: async ({ path }) => {
const process = Bun.spawn(["bunx", "oxfmt", "--write", resolve(path)]);
const exitCode = await process.exited;
if (exitCode !== 0) throw new Error(`oxfmt failed with code ${exitCode}`);
},
});

await wabe.start();
Expand Down
13 changes: 12 additions & 1 deletion packages/wabe/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import { getDatabaseAdapter } from '../src/utils/testHelper'
import { Wabe } from '../src/server'
import { FileDevAdapter } from '../src'
import { runDatabase } from 'wabe-mongodb-launcher'
import { resolve } from 'node:path'

const run = async () => {
await runDatabase()
const codegenPath = `${import.meta.dirname}/../generated/`

const wabe = new Wabe<{
types: WabeSchemaTypes
Expand All @@ -22,7 +24,16 @@ const run = async () => {
isProduction: false,
codegen: {
enabled: true,
path: `${import.meta.dirname}/../generated/`,
path: codegenPath,
},
onGenerateCodegen: async ({ path }) => {
const process = Bun.spawn(['bunx', 'oxfmt', '--write', resolve(path)], {
stdout: 'inherit',
stderr: 'inherit',
})

const exitCode = await process.exited
if (exitCode !== 0) throw new Error(`oxfmt failed with exit code ${exitCode}`)
},
rootKey: 'dev',
authentication: {
Expand Down
Loading
Loading