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

Abierto
#687 0 comentarios 0 reacciones 1 asignado Ver en GitHub

Los mantenedores suelen responder en 1 día

@kdy1 ya está trabajando en esto.

Desde el 2/11/2026.

Evaluación

Este issue todavía no se ha evaluado.

Descripción

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.

Lenguaje dominante
Rust
Estrellas
387
Forks
111
Merge medio
3 h 40 min
PR fusionados (30 d)
18

Preparar el entorno

Este proyecto no incluye contenedor de desarrollo, Dockerfile ni guía de contribución, así que la configuración corre por tu cuenta: empieza por su README y consulta nuestra guía para la primera contribución para los pasos generales.

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de swc-project/plugins

Todos los issues de swc-project/plugins

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.