Going Further

Codegen

Automatically typed queries and mutations for your Nuxt Shopify project

The module watches the GraphQL operations you write and generates TypeScript types for them automatically, straight from your store's own schema. You write a query as a plain string, and the variables going in, the data coming back, and every fragment in between are all typed for you. No interfaces to write by hand, no as casts, and no separate codegen command to run.

server/api/example.ts
export default defineEventHandler(async (event) => {
  const storefront = useStorefront(event)

  const { data } = await storefront.request(`#graphql
    query GetProduct($handle: String!) {
      product(handle: $handle) {
        id
        title
      }
    }
  `, {
    variables: { handle: 'high-top-sneakers' }, // required and typed
  })

  // data.product is { id: string; title: string } | null
  return data?.product
})

None of that was typed by hand. The compiler knows handle is a required String!, and it knows data.product has an id and a title and nothing else, because that is what the query defined.

How it works

For every client you configure, the module runs a small pipeline on nuxt prepare, nuxt dev and nuxt build that re-runs whenever a file that contains GraphQL was changed.

Introspect the schema

The module writes a minified schema to .nuxt/shopify/schema/<client>.<apiVersion>.schema.json. This is the source of truth for what the API can do. The API version is part of the filename, so bumping apiVersion uses the new version's schema instead of reusing the previous one.

  • Storefront API: with the default apiVersion, the schema comes from @shopify/hydrogen, so you don't need credentials or a network connection. For other versions, the module introspects your store (or mock.shop when mock is enabled).
  • Customer Account API: the schema always comes from @shopify/hydrogen, because Shopify doesn't offer unauthenticated introspection for it. The module shows a warning if your apiVersion differs from the version of the schema.
  • Admin API: the schema always comes from your store.

Generate the base types

From that schema it generates <client>.types.d.ts, the full TypeScript picture of the API: every object, input, enum and scalar.

Scan the operations

Then it scans the files matched by the documents globs, picks out the operations, and writes <client>.operations.d.ts. Every query, mutation and fragment it finds gets its own precise type: GetProductQuery, GetProductQueryVariables, ProductFieldsFragment, and so on.

Wire up the typed request()

The operations file registers each operation into the client's type map, keyed by the operation's own source text. So when you call storefront.request() with a #graphql string, TypeScript looks the string up in the map and pulls out both the variables it needs and the data it returns.

Because the source string is the key, the module can only type operations it can find and read statically. Write them as a #graphql-prefixed template literal or a literal with a /* GraphQL */ comment in front, and pass the literal to request(). Operations in gql or graphql tagged templates and in .graphql or .gql files also get generated types, which you can import by name. request() only infers its types from a literal. See the Editor Setup for more information.

During dev the types regenerate on every file change. Save a file that adds or changes an operation and the operations step re-runs, so the new query is typed a moment later, with no restart and nothing to run by hand.

What gets generated

Everything lands in your build directory (.nuxt), which is throwaway and git-ignored, so there is nothing to commit and nothing to keep in sync by hand. Depending on which clients you set up, you get:

.nuxt/shopify/storefront/storefront.operations.d.ts
// One precise type per query, mutation and fragment.

Each client gets the same set of files under its own folder (.nuxt/shopify/customer-account, .nuxt/shopify/admin), so a Storefront query is always checked against the Storefront schema, and an Admin query against the Admin schema.

Operation and fragment types are available globally when codegen.globalTypes is enabled (the default for the storefront client). Everything, including the schema types, can also be imported from the #shopify/<client> aliases (#shopify/storefront, #shopify/customer-account, #shopify/admin). See Generated Types and Auto-Imports.

Routing operations to the right API

Which client an operation belongs to comes down to where the file lives and what it's called, which is what each client's documents globs match on. Out of the box:

  • Storefront is the default. Any GraphQL in your app counts as a Storefront operation unless it's scoped to one of the others.
  • Admin picks up files in an admin/ directory, or named *.admin.{ts,js,gql,graphql}.
  • Customer Account picks up files in a customer-account/, customer/ or account/ directory, or named *.customer-account.*, *.customer.* or *.account.*.
This is the same convention the fragments use. Keeping shared documents under graphql/<client>/ in the project root keeps everything routed correctly.

Configuration

Codegen is configured per client, under shopify.clients.<client>. The full list lives in the configuration reference, but the options that matter most for codegen are:

documents
array
Glob patterns (relative to your project root) to scan for operations. Prefix a pattern with ! to exclude it, for example !app/legacy for one folder or !**/legacy/** for every legacy folder. A ! pattern that matches a folder excludes everything in it, so !app/legacy/* also excludes app/legacy/old/. ! patterns don't apply inside folders whose name starts with a dot, such as .storybook, unless they name the folder. Square brackets match one character, so pages/[handle].vue matches pages/h.vue, not your [handle].vue page; use pages/*.vue instead. Folders that are symlinks aren't searched. When provided, these patterns replace the client's built-in defaults. node_modules, dist, .nuxt and .output folders and .d.ts files are always skipped.
codegen
object | boolean
Code generation for this client. Set to false to disable it entirely - default: true
codegen.plugins.typescript
object
Override the underlying TypeScript plugin options, for example to customize scalar mappings.
codegen.globalTypes
boolean
Declare this client's operation and fragment types globally, so they can be used without an import - default: true for the storefront client, false for the others.

For example, to scan a custom directory and map a custom scalar:

nuxt.config.ts
export default defineNuxtConfig({
  shopify: {
    clients: {
      storefront: {
        apiVersion: '2026-10',
        publicAccessToken: '...',

        documents: [
          'queries/**/*.ts', // only scan this folder (replaces the defaults, relative to project root)
          '!queries/legacy/**', // but ignore this one
        ],

        codegen: {
          plugins: {
            typescript: {
              scalars: { Decimal: 'number' },
            },
          },
        },
      },
    },
  },
})
Shopify's custom scalars are mapped to string by default (DateTime, Decimal, HTML, URL, Color, UnsignedInt64, ISO8601DateTime, JSON), unknown scalars fall back to unknown, and enums are generated as string union types.

Good to know

A few things fall out of generating types off a live schema:

  • The first build needs credentials and a network connection for the Admin API, and for Storefront API versions other than the default. Introspection actually talks to Shopify, so the first time types are generated you need a valid token and a reachable API. If a Storefront API version can't be introspected, the module shows a warning and uses the schema from @shopify/hydrogen until the next start.
  • If the type generation fails, nuxt build fails too. nuxt dev, nuxt prepare and test runs log the error and continue without the types of that client. To build without network access or credentials, for example in CI, set codegen: false on the client.
  • Only operations written out statically get typed. If you build a query string dynamically at runtime there is nothing for the module to match against, so it falls back to a generic response. Write operations as literals and it keeps the types.
  • A brand new operation is only typed once codegen has run. In dev that happens on its own and near-instantly thanks to the file watcher. The watcher sees the app/, server/ and shared/ folders and your fragments.dirs. For operations in another folder, add the folder to watch in your Nuxt config, relative to app/, such as '../queries'. In CI, nuxt prepare generates everything before typechecking.

Advanced: customizing generation

Each step also exposes a hook (shopify:<client>:generate:introspection, shopify:<client>:generate:types and shopify:<client>:generate:operations). Each one hands you the GraphQL Code Generator config right before it runs, so if you need full control you can add plugins, change the preset, or adjust the document list. To use a plugin that the module doesn't include, install it in your app. The Hooks reference has the full list and their signatures.

Acknowledgements

We would like to thank the authors of the following libraries for making this possible:

Published under the MIT License