Codegen

CLI Reference

Command-line interface for UDL code generation

Overview

The udl-codegen CLI generates TypeScript types from various sources. Use it standalone or let it run automatically when starting the UDL server.

Usage

Terminal
udl-codegen [options]

Source Options

OptionShortDescription
--endpoint <url>-eGraphQL endpoint to introspect
--from-response <file>-rJSON file to infer types from
--type <name>-tType name for response inference (required with -r)
--from-store-sInfer from UDL node store (requires running server)

Output Options

OptionShortDefaultDescription
--output <path>-o./generatedOutput directory
--guards-gfalseGenerate type guard functions

Behavior Options

OptionShortDescription
--watch-wWatch for changes and regenerate
--clean-cRemove generated files
--dry-run-dPreview without writing files

Config Options

OptionShortDefaultDescription
--config <file>-Cudl.config.tsPath to config file

Generator Options

OptionDefaultDescription
--no-internalfalseExclude internal metadata fields
--no-jsdocfalseExclude JSDoc comments
--export-typefalseUse type instead of interface

Other Options

OptionShortDescription
--help-hShow help message
--version-vShow version number

Examples

Generate from GraphQL Endpoint

The most common usage - introspect a running UDL server:

Terminal
# Start your UDL server first
npx universal-data-layer &

# Then generate types
udl-codegen --endpoint http://localhost:4000/graphql

Generate with Type Guards

Include runtime type checking functions:

Terminal
udl-codegen -e http://localhost:4000/graphql --guards

Generated guards look like:

export function isContentfulProduct(value: unknown): value is ContentfulProduct {
  // Runtime type checking
}

Generate from JSON Response

Useful for mocking or quick prototyping:

Terminal
udl-codegen --from-response ./samples/product.json --type Product

This infers a TypeScript interface from the JSON structure.

Watch Mode

Automatically regenerate when schemas change:

Terminal
udl-codegen -e http://localhost:4000/graphql --watch

With --endpoint, watch mode polls every 5 seconds for schema changes.

Preview Changes

See what would be generated without writing files:

Terminal
udl-codegen -e http://localhost:4000/graphql --dry-run

Clean Generated Files

Remove all generated files:

Terminal
udl-codegen --clean

Custom Output Directory

Terminal
udl-codegen -e http://localhost:4000/graphql --output ./src/types

Using with Config File

If you have a udl.config.ts with codegen settings, CLI options override config file values:

Terminal
# Uses settings from udl.config.ts but overrides output
udl-codegen --output ./custom-types

Integration with Package Scripts

Add to your package.json:

package.json
{
  "scripts": {
    "codegen": "udl-codegen -e http://localhost:4000/graphql",
    "codegen:watch": "udl-codegen -e http://localhost:4000/graphql --watch",
    "codegen:clean": "udl-codegen --clean"
  }
}

Then run:

Terminal
npm run codegen

Exit Codes

CodeMeaning
0Success
1Error (missing source, invalid options, generation failure)

Next Steps