dotansimha / graphql-code-generator

A tool for generating code based on a GraphQL schema and GraphQL operations (query/mutation/subscription), with flexible support for custom plugins.
https://the-guild.dev/graphql/codegen/
MIT License
10.72k stars 1.31k forks source link

Configure conditional directives #10009

Open mikejpeters opened 3 weeks ago

mikejpeters commented 3 weeks ago

Is your feature request related to a problem? Please describe.

I am using a custom conditional directive @since to make it easier to support server schemas that are older than the client schema (i.e. self-hosted instances of server software). My custom directive is similar to the built-in @include directive, and only includes the decorated field if the server is at least the specified version.

fragment orderInventory on OrderInventory {
  someField
  fieldAddedInNewVersion @since(version: "1.2.0")
}

I would like to generated Typescript types for fields with this conditional directive applied to be nullable, so my code can be more type safe.

Describe the solution you'd like

Expose an option to specify additional conditional directives in the visitor plugin:

https://github.com/dotansimha/graphql-code-generator/blob/cac19e53e3acd30eb72571e39e4e0c314260c69c/packages/plugins/other/visitor-plugin-common/src/utils.ts#L507

Describe alternatives you've considered

I tried using the documentTransforms option to transform my @since directives to @include, and this does result in the generated types being correct, but the problem is that in my case I need to do the version comparison at runtime, which requires running a document transform in my app's code and not during the codegen.

Is your feature request related to a problem? Please describe.

No response