Genql
Website

Generate the client#

From http endpoint with --endpoint#

bash
1
genql --endpoint https://countries.trevorblades.com --output ./generated

You can fetch using get passing the --get flag

bash
1
genql --get --endpoint https://countries.trevorblades.com --output ./generated

From local schema with --schema#

bash
1
genql --schema ./schema.graphql --output ./generated

Passing headers with -H#

bash
1
genql -H 'Authorization: Bearer myToken' -e https://countries.trevorblades.com -o ./generated

Only generate ES modules with --esm#

If you are using nodejs you probably don't care about this flag

By default genql generates commonjs code that use require and module.exports to handle imports and exports

If you are compiling your code trough a bundler like webpack you can add the --esm flag to generate ES modules exports and imports, this way when you do import { createClient } from './generated' you are actually importing from an es module

Using ES modules has the advantage of smaller bundle size via tree shaking.

bash
1
genql --esm --schema ./schema.graphql --output ./generated

Generate both commonjs and ESM with --esm-and-cjs#

If you publish a package with genql code you probably want to include both ESM and CJS code for users using browser and nodes respectively

To do that pass the --esm-and-cjs flag

sh
1
genql --esm-and-cjs --schema ./schema.graphql --output ./generated

Changing scalar types with -S#

This is useful if you want to map from a custom scalar to a type like string, number, etc.

By default custom scalars have type any

bash
1
genql -S MyScalar:string -S AnotherScalar:number -e https://countries.trevorblades.com -o ./generated

Sort generated code with --sort#

If you see a lot of diffs between one generation and another you can use the --sort flag to sort object properties

The order of generated object properties is taken from the schema introspection by default, so you shouldn't need this flag if using popular frameworks like Apollo server

bash
1
genql --sort -e https://countries.trevorblades.com -o ./generated
Generate the client