Skip to content
Latchkey

graphql-codegen: Generate Types from a GraphQL Schema

graphql-codegen reads a GraphQL schema (and optionally operations) and generates typed output such as TypeScript types, hooks, or resolvers per its config.

GraphQL Code Generator keeps client and server code in sync with the schema. In CI it regenerates output and verifies the committed files match, so schema changes cannot silently break types.

What it does

The CLI (graphql-codegen) loads a codegen.ts/codegen.yml config that names the schema source, documents, and one or more plugins, then writes the generated files defined by each output target.

Common usage

Terminal
graphql-codegen --config codegen.ts
# CI: verify committed output is up to date without writing
graphql-codegen --config codegen.ts --check
# regenerate on change during local dev
graphql-codegen --watch

Options

FlagWhat it does
-c, --config <file>Path to the codegen config
--checkFail if generated output differs from committed files
-w, --watch [glob]Regenerate when sources change
--verboseVerbose logging
--errors-onlySuppress non-error output
-r, --require <module>Require a module before running (e.g. ts-node/register)

In CI

Run graphql-codegen --check in the pipeline so a PR that changes the schema without regenerating types fails. If your codegen version lacks --check, run graphql-codegen then git diff --exit-code on the output directory to the same effect.

Common errors in CI

"Unable to find any GraphQL type definitions for the following pointers" means the schema path/glob matched nothing. "Failed to load schema from ...: connect ECONNREFUSED" means an introspection URL is unreachable in CI (prefer a checked-in SDL file). "Unable to load config file" points at a bad --config path or a TS config needing -r ts-node/register. "Syntax Error: ..." comes from an invalid .graphql document.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →