Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

vite-plugin-graphql-codegen

danielwaltz95.8kMIT3.6.1TypeScript support: included

Zero-config vite plugin that uses the vite file watcher to run graphql codegen programmatically without needing to start a separate watcher

graphql, codegen, graphql-codegen, graphql-code-generator, vite, vite-plugin, vite-plugin-graphql-codegen

readme

Vite Plugin GraphQL Codegen npm

Zero-config vite plugin that uses the vite file watcher to run graphql codegen programmatically without needing to start a separate watcher.

Setup GraphQL Codegen

Installation instructions found here. Optional if already set up in project.

Install Plugin

# npm
npm i -D vite-plugin-graphql-codegen

# yarn
yarn add -D vite-plugin-graphql-codegen

# pnpm
pnpm i -D vite-plugin-graphql-codegen

Initialize Plugin

// vite.config.ts

import { defineConfig } from 'vite';
import codegen from 'vite-plugin-graphql-codegen';

export default defineConfig({
  plugins: [
    codegen(),
  ],
});

Options

Providing options is not required as sensible defaults are in place, but there may be times where it's helpful to disable codegen under certain circumstances, like when running builds in CI.

codegen({
  /**
   * Run codegen on server start.
   *
   * @default true
   */
  runOnStart: boolean,
  /**
   * Run codegen on build. Will prevent build if codegen fails.
   *
   * @default true
   */
  runOnBuild: boolean,
  /**
   * Enable codegen integration with vite file watcher.
   *
   * @default true
   */
  enableWatcher: boolean,
  /**
   * Automatically add schemas and documents referenced in the codegen config
   * to the file watcher.
   *
   * @default true
   */
  watchCodegenConfigFiles: boolean,
  /**
   * Throw an error if codegen fails on server start.
   *
   * @default false
   */
  throwOnStart: boolean,
  /**
   * Throw an error if codegen fails on build.
   *
   * @default true
   */
  throwOnBuild: boolean,
  /**
   * Run codegen when a document matches.
   *
   * @default true
   */
  matchOnDocuments: boolean,
  /**
   * Run codegen when a schema matches.
   *
   * @default false
   */
  matchOnSchemas: boolean,
  /**
   * Manually define the codegen config.
   */
  config: CodegenConfig,
  /**
   * Override parts of the codegen config just for this plugin.
   */
  configOverride: Partial<CodegenConfig>,
  /**
   * Override parts of the codegen config just for this plugin on server start.
   */
  configOverrideOnStart: Partial<CodegenConfig>,
  /**
   * Override parts of the codegen config just for this plugin on build.
   */
  configOverrideOnBuild: Partial<CodegenConfig>,
  /**
   * Override parts of the codegen config just for this plugin in the watcher.
   */
  configOverrideWatcher: Partial<CodegenConfig>,
  /**
   * Override the codegen config file path.
   */
  configFilePathOverride: string,
  /**
   * Log various steps to aid in tracking down bugs.
   *
   * @default false
   */
  debug: boolean,
});

changelog

3.6.1 (2025-05-07)

Bug Fixes

  • downgrade unbuild to fix cjs types output (c1b0bca), closes #38

Miscellaneous Chores

3.6.0 (2025-05-03)

Features

  • add option to automatically add schema and document files referenced in the codegen config to the vite file watcher (e74caad), closes #37

Miscellaneous Chores

3.5.0 (2025-02-07)

Features

3.4.5 (2025-01-25)

Bug Fixes

  • update path to codegen types (cf6c0a9)

Documentation

Miscellaneous Chores

3.4.4 (2024-12-14)

Bug Fixes

  • update jsdoc for default value (5b85be2)

Miscellaneous Chores

3.4.3 (2024-12-07)

Bug Fixes

  • remove vite and codegen cli from deps to rely on peer deps (40e8c56)

Miscellaneous Chores

  • add full git url to package file (33c53b3)
  • add vite and codegen cli as dev deps (8104f38)
  • migrate to flat eslint config (7676e68)
  • update deps and format (66dd33f)

3.4.2 (2024-12-01)

Bug Fixes

  • deps: update dep versions to be more loose (c807514)

3.4.1 (2024-12-01)

Performance Improvements

  • load and cache matches on server start (69c1d97), closes #32 #27
  • skip match cache refresh if file is generated (c0737a7)

Tests

  • scope vite instances to spec directories (90d771f)

Miscellaneous Chores

3.4.0 (2024-11-23)

Features

Continuous Integration

  • update github action versions (0c656ab)

Miscellaneous Chores

  • update node to v22.11.0 lts (d941f32)
  • use .node-version file (863a61a)

3.3.8 (2024-06-12)

Bug Fixes

  • watch documents and schemas from nested config (5f8f08d)

Miscellaneous Chores

  • normalize schema file path (39f1361)

3.3.7 (2024-06-01)

Bug Fixes

  • prepare for vite v6 release (b49c508)

Miscellaneous Chores

  • deps: refresh lockfile and dedupe deps (005f81e)
  • deps: update deps (6c75534)
  • update node to 20.12.1 (4271cfb)

3.3.6 (2024-01-02)

Miscellaneous Chores

  • deps: update deps (063408b), closes #28
  • explicitly add semantic release plugins as deps (6da889b)
  • update node to v20.10.0 and use dedicated release config (9ccbd00)

3.3.5 (2023-11-27)

Bug Fixes

  • migrate to unbuild and fix default export issues in commonjs (24914a2)

3.3.4 (2023-11-27)

Bug Fixes

3.3.3 (2023-11-17)

Bug Fixes

  • broaden allowed vite dependency versions (3b2bf38)

3.3.2 (2023-11-16)

Bug Fixes

  • add vite 5 as supported peer and update deps (f2000e5)

3.3.1 (2023-09-15)

Bug Fixes

  • match on relative document paths (ff11873), closes #23

3.3.0 (2023-08-19)

Features

  • run schema and document checks at the same time (aa8c92c), closes #21

3.2.5 (2023-08-18)

Bug Fixes

  • add @graphql-codegen/plugin-helpers to dependencies (e58be01), closes #22

3.2.4 (2023-08-14)

Bug Fixes

  • fix matching on schema changes (43ee458), closes #21

3.2.3 (2023-07-25)

Bug Fixes

  • add v5.0.0 to codegen cli peer deps definition (c67977e)

3.2.2 (2023-05-25)

Bug Fixes

  • add @graphql-codegen/cli@^4.0.0 as peer dependency (fba8d5a)

3.2.1 (2023-04-26)

Bug Fixes

  • fix watching schema while also watching documents (8cd00ea)

3.2.0 (2023-04-05)

Features

  • add options for error throwing on server start or build (4654dde), closes #15

3.1.0 (2023-02-04)

Bug Fixes

  • add missing babel peer dep (f01765d)

Features

  • support graphql codegen v3.0.0 (9b8cea4)

3.0.1 (2022-12-12)

Bug Fixes

3.0.0 (2022-12-09)

BREAKING CHANGES

  • requires using vite 4 due to plugin type incompatibility

2.3.1 (2022-10-14)

Bug Fixes

  • skip codegen config file check if filepath is not defined (4b89289), closes #13

2.3.0 (2022-08-26)

Features

  • add option for enabling matching on schemas (e4b57f1)

2.2.1 (2022-08-25)

Bug Fixes

2.2.0 (2022-08-25)

Features

  • make commonjs the default with esm definitions (df9e437)

2.1.1 (2022-08-25)

Bug Fixes

  • fix package exports path (d519d15)

2.1.0 (2022-08-25)

Bug Fixes

  • Changes package.json to module (386a875)

Features

  • Adds schema to the isGraphQLDocument check (76ae403)

2.0.1 (2022-08-24)

Bug Fixes

  • add default export for commonjs (c93263f), closes #4
  • remove gap in debug log output (a75a820)

2.0.0 (2022-08-11)

Features

  • update debug log output to match vite 3 (c33bba7)
  • use vite dev server restart function (b413156)

BREAKING CHANGES

  • Requires vite version 2.7 or greater

1.8.3 (2022-08-11)

Bug Fixes

  • switch to npm and refresh packages (be95d72)

1.8.2 (2022-08-11)

Bug Fixes

  • configure semantic release to release dist (cfc1954)

1.8.1 (2022-08-10)

Bug Fixes

  • normalize file paths before comparisons (b855f0a)

1.8.0 (2022-07-13)

Features

1.7.0 (2022-06-10)

Features

  • support overriding config in certain modes (97fce30)

1.6.0 (2022-03-18)

Features

  • Support passing manual codegen config (e86079b), closes #3

1.5.2 (2022-02-23)

Bug Fixes

  • Make graphql a dev dependency (e88edb1)

1.5.1 (2022-02-23)

Bug Fixes

1.5.0 (2022-02-10)

Features

  • Add option to override codegen config file path (ddf9c54), closes #1

1.4.0 (2022-02-09)

Features

  • Add a debug option to aid in troubleshooting (092398d)

1.3.0 (2022-02-01)

Features

  • Support disabling codegen in certain modes (9c546a7)
  • Support overwriting codegen config options (4224cec)

1.2.0 (2022-02-01)

Features

  • Restart vite when codegen config file changes (24b0d0e)

1.1.1 (2021-08-28)

Bug Fixes

  • Add fallback extension to preset config (7e6b862)
  • Make documents matcher work with multiple files (a047a37)

1.1.0 (2021-08-28)

Features

  • Add support for near operation file preset (18b1227)

1.0.1 (2021-08-28)

Bug Fixes

  • Add try catch on generate to keep vite running (94e4a9b)