graphql-codegen-client-preset: inline calls, the leftover tag import, and name-only matching keep the document map in bundles

オープン
#687 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@kdy1 がすでに取り組んでいます。

2026年11月2日 から。

評価

この issue はまだ評価されていません。

説明

Describe the bug

@swc-contrib/plugin-graphql-codegen-client-preset rewrites const X = graphql(...) declarations. Three cases still leave the client-preset document map in the bundle, or produce imports that don't exist:

  1. Calls outside a variable initializer are not rewritten. useMutation(graphql(...)), { query: graphql(...) }, and apollo.query({ query: graphql(...) }) keep calling the gql.ts function, so gql.ts and its map of every document stay in the bundle. Within a declaration, the first declarator that isn't a tag call returns early, so const a = 1, b = graphql(...) is not rewritten either.
  2. The tag import stays after every call is rewritten. import { graphql } from './gql' keeps gql.ts in the module graph. Its import * as types from './graphql' then keeps every generated document, because a namespace import blocks export-level tree shaking.
  3. The tag is matched by name only. A project that also compiles a package with its own client-preset output (for example an SDK with import { graphql } from '../lib/gql-generated/gql') has those calls rewritten to imports from the application's artifactDirectory, where the documents don't exist. The build fails, or the import resolves to undefined.

URL to minimal reproduction

A fix with tests is ready on a fork. It's the closed PR #686, commit f6d6cfb, branch graphql-codegen-import-sources. It was closed automatically by the contributor policy. Inline reproduction with artifactDirectory: "./src/gql" and gqlTagName: "graphql":

import { graphql } from './src/gql';

useMutation(graphql(`mutation SaveUser { id }`)); // not rewritten (case 1)
const a = 1, b = graphql(`query GetUser { id }`); // not rewritten (case 1)
const c = graphql(`query GetOrg { id }`); // rewritten, but the graphql import stays (case 2)
// packages/sdk/queries.ts, a separate client-preset output
import { graphql } from '../lib/gql-generated/gql';
const q = graphql(`query GetVendor { id }`); // rewritten to ./src/gql/graphql's GetVendorDocument, which doesn't exist (case 3)

Expected behavior

  • Every tag call whose first argument is a template literal is rewritten, in any expression position. A document used twice is imported once.
  • There is an opt-in way to limit rewriting to tags imported from the application's artifact directory. With that option set, the plugin removes the tag import once nothing references it, so bundlers can drop gql.ts.

The fork adds a gqlImportSources option for this. It lists the module specifiers the tag is imported from, including aliased imports. When the option is unset, matching and imports behave as they do today. It includes 8 new tests; the 23 existing tests are unchanged and pass. cargo clippy is clean and the option is documented in README.tmpl.md.

Additional context

In a Next.js 16.4.2 (Turbopack) application with about 2,300 documents, these changes let Turbopack drop the document map and any document a route doesn't import. The application also splits its generated documents into one module per document. Client entry JS for one route went from 12.1 MB to 6.4 MB (2,266 KB to 1,922 KB gzip), without hoisting inline calls and without a workaround for an SDK package that has its own graphql().

cc @kdy1 (plugin maintainer) and @mischnic (Turbopack tree-shaking context). I'm happy to adjust the option's name or shape, or to split this into separate changes if you prefer.

主要言語
Rust
スター
387
フォーク
111
平均マージ
3時間 40分
マージ済み PR(30日)
18

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

swc-project/plugins のほかの issue

swc-project/plugins の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。