Skip to Content
Gqlts

Programmatic generation

Use the generate function when schema generation belongs inside a custom build script. This is useful for monorepos, generated SDK packages, or backend demos that create a client from a freshly built schema.

Generate from a schema string

import { readFile } from 'node:fs/promises'; import { join } from 'node:path'; import { generate } from '@gqlts/cli'; await generate({ schema: await readFile(join(import.meta.dirname, 'schema.graphql'), 'utf8'), output: join(import.meta.dirname, 'generated'), scalarTypes: { DateTime: 'string', Upload: 'File', }, });

Generate from an endpoint

import { generate } from '@gqlts/cli'; await generate({ endpoint: 'https://api.example.com/graphql', output: './generated', headers: { authorization: `Bearer ${process.env.GRAPHQL_TOKEN}`, }, sortProperties: true, });

API shape

generate(config: { endpoint?: string; useGet?: boolean; schema?: string; output?: string; headers?: Record<string, string>; scalarTypes?: Record<string, string>; onlyEsModules?: boolean; onlyCJSModules?: boolean; sortProperties?: boolean; verbose?: boolean; }): Promise<void>;

When to prefer the CLI

Prefer the CLI when a package script is enough. Prefer generate(config) when another script already knows where the schema is, which headers to use, or which output package should be rebuilt.