Code Generation
The schema package provides three code generation functions for producing static artifacts from a built GraphQL schema. These are useful for generating type files, SDL for tooling, and entity definitions for the client package.
generateSDL(schema)
Section titled “generateSDL(schema)”Produces a GraphQL SDL (Schema Definition Language) string from the built schema.
import { buildSchema, generateSDL } from '@graphql-suite/schema'
const { schema } = buildSchema(db, config)const sdl = generateSDL(schema)
// Write to file for use with GraphQL toolingawait Bun.write('schema.graphql', sdl)generateTypes(schema, options?)
Section titled “generateTypes(schema, options?)”Produces TypeScript type definitions including:
- Wire format types — Drizzle types with
Datefields converted tostring(matching GraphQL serialization) - Filter types — per-entity filter types with all supported operators
- Input types — insert and update input types for mutations
- OrderBy types — per-entity ordering types
import { buildSchema, generateTypes } from '@graphql-suite/schema'
const { schema } = buildSchema(db, config)const types = generateTypes(schema, { drizzle: { importPath: '@/db/schema', },})
await Bun.write('generated/types.ts', types)Options
Section titled “Options”type CodegenOptions = { drizzle?: { /** Import path for Drizzle schema types (e.g. '@/db/schema') */ importPath: string /** Override mapping: Drizzle table name to export type name */ typeNames?: Record<string, string> }}When drizzle.importPath is provided, the generated wire types import from your Drizzle schema and convert Date fields to string. Without it, wire types fall back to Record<string, unknown>.
The drizzle.typeNames option lets you override the mapping between table names and Drizzle export type names, which is useful when the Drizzle type name does not match the capitalized table name:
const types = generateTypes(schema, { drizzle: { importPath: '@/db/schema', typeNames: { overrideToAsset: 'OverrideAsset', }, },})generateEntityDefs(schema, options?)
Section titled “generateEntityDefs(schema, options?)”Produces entity definitions and a runtime schema object for the client package. The output includes:
- A
schemaconst with query/mutation names, fields, and relation metadata - An
EntityDefstype for client-side type inference - A
TableNameMaptype mapping Drizzle type names to entity keys
import { buildSchema, generateEntityDefs } from '@graphql-suite/schema'
const { schema } = buildSchema(db, config)const entityDefs = generateEntityDefs(schema, { drizzle: { importPath: '@/db/schema', },})
await Bun.write('generated/entity-defs.ts', entityDefs)Schema from Drizzle exports (no database needed)
Section titled “Schema from Drizzle exports (no database needed)”For code generation, you typically do not need a live database connection. Use buildSchemaFromDrizzle to build a schema directly from your Drizzle schema exports:
import { buildSchemaFromDrizzle, generateEntityDefs, generateSDL, generateTypes,} from '@graphql-suite/schema'
import * as drizzleSchema from './db/schema'
const { schema } = buildSchemaFromDrizzle(drizzleSchema, config)
await Bun.write('schema.graphql', generateSDL(schema))await Bun.write('generated/types.ts', generateTypes(schema, options))await Bun.write('generated/entity-defs.ts', generateEntityDefs(schema, options))