Skip to content

graphql-codegen-client-preset: document monorepo artifactDirectory paths with Turbopack #693

Done

Description

Problem

In a monorepo (Turborepo / pnpm or Yarn workspaces) where the Next.js app lives below the workspace root, the README's Next.js example resolves artifactDirectory against the workspace root rather than the Next app directory. The build then fails with:

Module not found: Can't resolve './../../../../src/gql/graphql'

and the error message doesn't point to the artifact path as the cause.

Root cause: Turbopack passes its root directory (by default, the directory containing the lockfile) to SWC as the cwd. The plugin joins relative artifactDirectory values onto that cwd, so "./src/gql" lands at <workspace-root>/src/gql instead of <workspace-root>/apps/web/src/gql.

Reproduced with Next 16.2.6 + Turbopack + Yarn 4 workspaces, plugin 0.24.0. With a workspace-root-relative artifactDirectory (apps/web/src/gql), the build succeeds and the resulting chunks no longer contain the GraphQL operation strings, so tree-shaking works as advertised.

Proposed docs change

Add the following to contrib/graphql-codegen-client-preset/README.tmpl.md, right after the Next.js example (before #### .swcrc):

> **Monorepos with Turbopack:** Turbopack passes its [root directory](https://nextjs.org/docs/app/api-reference/config/next-config-js/turbopack#root-directory) to SWC as the `cwd`. By default this is the workspace root (the directory containing your lockfile, e.g. `yarn.lock` or `pnpm-lock.yaml`), not the Next.js app directory. If your app lives below that root, write `artifactDirectory` as a path from the workspace root. This does not apply to webpack builds, or if you set `turbopack.root` to the app directory. In those cases, keep the path relative to the app.

```ts
// apps/web/next.config.mjs, in a monorepo where the lockfile sits at the repo root
const nextConfig = {
  experimental: {
    swcPlugins: [
      [
        "@swc-contrib/plugin-graphql-codegen-client-preset",
        { artifactDirectory: "apps/web/src/gql", gqlTagName: "graphql" },
      ],
    ],
  },
};

export default nextConfig;
```

The same change was proposed in #692 (auto-closed by the contributor policy), which replaced #627.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions