---
title: "GraphQL Codegen"
description: "Auto-generate typed Admin API queries with the codegen module option."
canonical_url: "https://shopify.yan.ad/guides/graphql-codegen"
---
# GraphQL Codegen

> Auto-generate typed Admin API queries with the codegen module option.

## Overview

`@yan-ad/nuxt-shopify` can auto-generate TypeScript types from your Shopify Admin API GraphQL queries. When enabled, the module fetches the Admin API schema via introspection during `nuxt prepare` and generates typed definitions using [`@shopify/graphql-codegen`](https://www.npmjs.com/package/@shopify/graphql-codegen).

Generated types are importable via `#shopify/types/<version>/admin`.

## Setup

### 1. Install peer dependencies

```bash
bun add -d @graphql-codegen/cli @shopify/graphql-codegen graphql
```

### 2. Enable codegen in your config

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  modules: ['@yan-ad/nuxt-shopify'],

  shopify: {
    apiKey: '...',
    apiSecretKey: '...',
    appUrl: '...',

    // Generate types for the default apiVersion
    codegen: true

    // Or specify multiple API versions
    // codegen: ['2025-10', '2026-01'],
  }
})
```

### 3. Add store credentials

The module needs to introspect your store's Admin API to fetch the schema. Add these environment variables to your `.env`:

```dotenv
SHOPIFY_CODEGEN_STORE_DOMAIN=your-store.myshopify.com
SHOPIFY_CODEGEN_ADMIN_ACCESS_TOKEN=shpca_xxxxx
```

You can obtain an Admin API access token from a [custom app](https://help.shopify.com/en/manual/apps/app-types/custom-apps) in your Shopify store settings, or from the [Shopify Partners Dashboard](https://partners.shopify.com).

<tip>

The token only needs `read_` scopes for the resources you query. For full schema introspection, a token with broad read access works best.

</tip>

### 4. Generate types

```bash
bun run dev:prepare
```

The module will:

1. Fetch the Admin API schema via GraphQL introspection
2. Scan your `server/**/*` and `app/**/*` files for `#graphql` tagged queries
3. Generate typed `.d.ts` files in `.nuxt/types/shopify/`
4. Register `#shopify/types/<version>/admin` aliases

## Usage

Once types are generated, import them in your server routes or composables:

```ts [server/api/products.ts]
import type { GetProductsQuery } from '#shopify/types/2026-01/admin'

export default defineEventHandler(async (event) => {
  const { admin } = await useShopifyAdmin(event)

  const response = await admin.graphql(`#graphql
    query GetProducts {
      products(first: 10) {
        nodes {
          id
          title
          description
        }
      }
    }
  `)

  const data: GetProductsQuery = await response.json()
  return data
})
```

## Multiple API versions

If your app needs to support multiple API versions (e.g., during migration), pass an array:

```ts [nuxt.config.ts]
export default defineNuxtConfig({
  shopify: {
    codegen: ['2025-10', '2026-01']
  }
})
```

Each version gets its own alias:

```ts
import type { GetProductsQuery as V2025 } from '#shopify/types/2025-10/admin'
import type { GetProductsQuery as V2026 } from '#shopify/types/2026-01/admin'
```

## Caching

Schemas are cached in `.nuxt/shopify-schema/` after the first fetch. Subsequent `nuxt prepare` runs will reuse the cached schema and skip the network request.

To force a re-fetch, delete the cache directory:

```bash
rm -rf .nuxt/shopify-schema
bun run dev:prepare
```

## How it works

<table>
<thead>
  <tr>
    <th>
      Step
    </th>
    
    <th>
      What happens
    </th>
  </tr>
</thead>

<tbody>
  <tr>
    <td>
      <code>
        nuxt prepare
      </code>
    </td>
    
    <td>
      Module hooks into <code>
        prepare:types
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Schema fetch
    </td>
    
    <td>
      Introspection query against <code>
        https://<store>/admin/api/<version>/graphql.json
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Cache
    </td>
    
    <td>
      Schema JSON saved to <code>
        .nuxt/shopify-schema/<version>.json
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Codegen
    </td>
    
    <td>
      <code>
        @graphql-codegen/cli
      </code>
      
       runs with <code>
        @shopify/graphql-codegen
      </code>
      
       preset
    </td>
  </tr>
  
  <tr>
    <td>
      Output
    </td>
    
    <td>
      <code>
        .nuxt/types/shopify/admin-<version>.d.ts
      </code>
    </td>
  </tr>
  
  <tr>
    <td>
      Alias
    </td>
    
    <td>
      <code>
        #shopify/types/<version>/admin
      </code>
      
       registered in Nuxt
    </td>
  </tr>
</tbody>
</table>

## Graceful degradation

The codegen step is **non-blocking**. If any of the following are missing, the module warns and continues without generating types:

- Peer dependencies not installed
- Store credentials env vars not set
- Shopify API returns an error (invalid token, network issue)

Your app will still build and run — you just won't have the generated types until the issue is resolved.


## Sitemap

See the full [sitemap](https://shopify.yan.ad/sitemap.md) for all pages.
