print() can change a document's meaning: shorthand query after a definition without a body

Aperta
#4,901 0 commenti 2 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@nimbly1 ci sta già lavorando.

Dal 23/10/2026.

  • #4913 di @nimbly1 — aperta
  • #4966 di @martinbonnin — aperta

Valutazione

Difficoltà
3/5
Tempo stimato
Mezza giornata
Idoneità per principianti
82/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
graphql, javascript, typescript
Ambito
tooling

Direzione di ricerca

Start with the Document printer and the parse/print round-trip behavior described in the reproduction. Check how anonymous query shorthand is emitted after bodyless type, interface, input, enum, and schema definitions or extensions. Add regression coverage for the affected definition forms and verify that reparsing preserves the operation or reports the expected syntax error.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

print writes an anonymous query without variables or directives in shorthand form, { ... }. When the previous definition is a type, interface, input or enum definition or extension without a body, or a schema extension without operation types, the parser reads that { as the previous definition's body. Printing and reparsing then gives a different document, and the query is gone.

Reproduction (graphql 17.0.2; the printer code is the same on 16.x.x and next):

import { parse, print } from 'graphql';

const document = parse('type A\n\nquery { a: b }');
const printed = print(document);
// 'type A\n\n{\n  a: b\n}'
parse(printed).definitions;
// one ObjectTypeDefinition `A` with a field `a: b`; the OperationDefinition is gone

The same happens after interface A, input A, enum A (with { a }), extend type A @d, and extend schema @d (with { query: b }). Where the query's selections aren't valid as the previous definition's body, the printed document is a syntax error instead.

This was found by fuzzing a Rust port of graphql-js with a print/parse round-trip property.

A possible fix: in the Document printer, keep the query keyword when the previous definition can take a body, i.e. it is an object, interface, input object or enum type definition or extension with no fields/values, or a schema extension with no operation types. Single-definition output and all other documents stay unchanged. I'm happy to open a PR with that change and tests.

Lingua principale
TypeScript
Stelle
20.3k
Fork
2.1k
Merge medio
6h 51m
PR unite (30g)
117

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di graphql/graphql-js

Tutte le issue di graphql/graphql-js

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.