Skip to content

feat(graphql): emit graphql descriptions as tsdoc comments#4054

Open
joelluijmes wants to merge 1 commit into
nestjs:masterfrom
gynzy:feat/emit-descriptions
Open

feat(graphql): emit graphql descriptions as tsdoc comments#4054
joelluijmes wants to merge 1 commit into
nestjs:masterfrom
gynzy:feat/emit-descriptions

Conversation

@joelluijmes

Copy link
Copy Markdown

What

Adds an opt-in emitDescriptions option to DefinitionsGeneratorOptions. When enabled, GraphQL schema descriptions ("""...""") are emitted as TSDoc/JSDoc comments (/** ... */) on the generated TypeScript definitions.

Descriptions are carried through for:

  • object / interface / input types
  • fields (property signatures)
  • resolver methods (root Query/Mutation/Subscription)
  • scalars
  • enums, enum members, and enum-as-type aliases
  • unions

Why

Today the schema-first definitions generator (GraphQLDefinitionsFactoryGraphQLAstExplorer) reads the AST but silently discards every node's description. This means the documentation authors write in their SDL never reaches the generated .ts file, so it's lost to editor tooltips and IntelliSense. This option surfaces that documentation in the generated types.

Usage

const definitionsFactory = new GraphQLDefinitionsFactory();
await definitionsFactory.generate({
  typePaths: ['./src/**/*.graphql'],
  path: './src/graphql.ts',
  outputAs: 'interface',
  emitDescriptions: true,
});

Given:

"""A feline animal."""
type Cat {
  """The name of the cat."""
  name: String!
}

produces:

/** A feline animal. */
export interface Cat {
    /** The name of the cat. */
    name: string;
}

Backward compatibility

The option defaults to false, so existing generated output is unchanged unless a user opts in.

Tests

Added specs in graphql-ast.explorer.spec.ts covering both the default (descriptions dropped) and enabled (descriptions emitted on types, fields, enums, and enum members) behavior. The full @nestjs/graphql suite passes (224 tests).

Note

Per CONTRIBUTING this is a small, additive, opt-in feature so it's submitted directly as a PR. Happy to open a [discussion] issue first if maintainers prefer. Docs for the option can follow in the nestjs/docs repo if this is accepted.

Add an opt-in "emitDescriptions" option to the definitions generator
that carries GraphQL schema descriptions into the generated TypeScript
as TSDoc/JSDoc comments on types, fields, resolver methods, scalars,
enums, enum members and unions.

Defaults to false to keep existing generated output unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant