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.
Setup
To install the @gel/generate
package, run the following command.
$
npm install --save-dev @gel/generate
$
yarn add --dev @gel/generate
$
pnpm add --save-dev @gel/generate
$
bun add --dev @gel/generate
$
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:
[instance]
server-version = "6.4"
[hooks]
schema.update.after = "npx @gel/generate queries --file"
Basic usage
Run a generator with the following command.
$
npx @gel/generate <generator> [options]
$
yarn run -B generate <generator> [options]
$
pnpm exec generate <generator> [options]
$
deno run \
--allow-all \
npm:@gel/generate <generator> [options]
$
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. | |
queries |
Scans your project for *.edgeql files and generates functions that allow you to execute these queries in a typesafe way. | |
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. |
Targets
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.
|
Generate TypeScript files ( |
|
Generate TypeScript files ( |
|
Generate |
|
Generate JavaScript with CommonJS syntax and and |
|
Generate TypeScript files with Deno-style ESM imports |