Skip to main content

GraphQL Code Generation

Code generation means the automatic generation of TypeScript types based on your GraphQL schema and your GraphQL operations. This is a very powerful feature that allows you to write your code in a type-safe manner, without you needing to manually write any types for your API calls.

To do this, we will use Graphql Code Generator.

Use npx vendure add and select "Set up GraphQL code generation" to quickly set up code generation.

You can then run npx vendure schema to generate a schema.graphql file in your root directory.

Note

This guide is for adding codegen to your Vendure plugins. For a guide on adding codegen to your storefront, see the Storefront Codegen guide.

Installation

It is recommended to use the vendure add CLI command as detailed above to set up codegen. If you prefer to set it up manually, follow the steps below.

First, install the required dependencies:

Configuration

Add a codegen.ts file to your project root with the following contents:

codegen.ts

This assumes that we have an "organization" plugin which adds support for grouping customers into organizations, e.g. for B2B use-cases.

Running codegen

You can now add a script to your package.json to run codegen:

package.json

Ensure your server is running, then run the codegen script:

This will generate a file at src/plugins/organization/gql/generated.ts which contains all the GraphQL types corresponding to your schema.

Using generated types in resolvers & services

You would then use these types in your resolvers and service methods, for example:

src/plugins/organization/services/organization.service.ts

In your service methods you can directly use any input types defined in your schema:

src/plugins/organization/services/organization.service.ts

Codegen for Admin UI extensions

Deprecated

This section refers to the deprecated Angular-based Admin UI. The new React-based Dashboard has built-in graphql type safety and does not require additional setup.

When you create Admin UI extensions, very often those UI components will be making API calls to the Admin API. In this case, you can use codegen to generate the types for those API calls.

To do this, we will use the "client preset" plugin. Assuming you have already completed the setup above, you'll need to install the following additional dependency:

Then add the following to your codegen.ts file:

codegen.ts

For the client preset plugin, we need to specify a directory (.../ui/gql/) because a number of files will get generated.

Use the graphql() function

In your Admin UI components, you can now use the graphql() function exported from the generated file to define your GraphQL operations. For example:

apps/marketplace/src/plugins/marketplace/ui/components/organization-list/organization-list.component.ts

Whenever you write a new GraphQL operation, or change an existing one, you will need to re-run the codegen script to generate the types for that operation.

Codegen watch mode

You can also set up file watching as described in the Graphql Code Generator watch mode docs.

Was this chapter helpful?
Report Issue
Edited Feb 23, 2026ยทEdit this page