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

Đang mở
#687 0 bình luận 0 reaction 1 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@kdy1 đang làm issue này rồi.

Từ ngày 2/11/2026.

Đánh giá

Issue này chưa được đánh giá.

Mô tả

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.

Ngôn ngữ chính
Rust
Star
387
Fork
111
Merge trung bình
3 giờ 40 phút
Pull request đã merge (30 ngày)
18

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của swc-project/plugins

Tất cả issue của swc-project/plugins

Issue tương tự

Thêm issue về Rust

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.