Search
ctrl+/
Ask AI
ctrl+.
Light
Dark
System
Sign in

Code Generation

The @gel/generate package provides a set of code generation tools that are useful when developing a Gel-backed applications with TypeScript/JavaScript.

To install the @gel/generate package, run the following command.

npm
yarn
pnpm
bun
deno
Copy
$ 
npm install --save-dev @gel/generate
Copy
$ 
yarn add --dev @gel/generate
Copy
$ 
pnpm add --save-dev @gel/generate
Copy
$ 
bun add --dev @gel/generate
Copy
$ 
deno add --dev npm:@gel/generate

Since the generators work by connecting to the database to introspect your schema and analyze queries, you'll need to have a database connection available when running the generators and to rerun generators any time the schema changes.

Like the CLI, the generators use the connection details from your initialized project, environment, or connection flags to connect to the database. See the connection parameters reference for more details.

You can ensure that the generators are always up-to-date locally by adding them to your gel.toml (or deprecated edgedb.toml)'s schema.update.after hook (added in 6) as in this example:

gel.toml
Copy
[instance]
server-version = "6.4"

[hooks]
schema.update.after = "npx @gel/generate queries --file"

Run a generator with the following command.

npm
yarn
pnpm
Deno
bun
Copy
$ 
npx @gel/generate <generator> [options]
Copy
$ 
yarn run -B generate <generator> [options]
Copy
$ 
pnpm exec generate <generator> [options]
Copy
$ 
  
  
deno run \
--allow-all \
npm:@gel/generate <generator> [options]
Copy
$ 
bunx @gel/generate <generator> [options]

The value of <generator> should be one of the following:

edgeql-js

Generates the query builder which provides a code-first way to write fully-typed EdgeQL queries with TypeScript. We recommend it for TypeScript users, or anyone who prefers writing queries with code.

docs

queries

Scans your project for *.edgeql files and generates functions that allow you to execute these queries in a typesafe way.

docs

interfaces

Introspects your schema and generates file containing TypeScript interfaces that correspond to each object type. This is useful for writing typesafe code to interact with Gel.

docs

All generators look at your environment and guess what kind of files to generate (.ts vs .js + .d.ts) and what module system to use (CommonJS vs ES modules). You can override this with the --target flag.

--target ts

Generate TypeScript files (.ts)

--target mts

Generate TypeScript files (.mts) with extensioned ESM imports

--target esm

Generate .js with ESM syntax and .d.ts declaration files

--target cjs

Generate JavaScript with CommonJS syntax and and .d.ts declaration files

--target deno

Generate TypeScript files with Deno-style ESM imports

To see helptext for the @gel/generate command, run the following.

Copy
$ 
npx @gel/generate --help