graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

Configure conditional directives

Open mikejpeters opened this issue 1 year ago • 5 comments

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

mikejpeters avatar Jun 16 '24 18:06 mikejpeters

Hi @mikejpeters, I've encountered the same issue and had tried the same hacky way in documentTransforms which doesn't work. My case is the same that the field's accessibility is determined in the runtime, but need to make this field nullable in codegen.

Any updates on your side?

imagicbell avatar May 05 '25 18:05 imagicbell

No, unfortunately I don't have a resolution; for now I've just been careful to treat any fields I use my directive with as nullable, even though the generated typings say otherwise

mikejpeters avatar May 05 '25 18:05 mikejpeters

Thanks for the quick reply @mikejpeters ! By you don't have a resolution, do you mean the exposing a config of custom conditional directives is not feasible? Actually before I found your opened issue here, I was thinking the same resolution which seems promising. However, if you've tried that proposal and doesn't work either, please let me know and we will look for other alternatives :)

imagicbell avatar May 05 '25 18:05 imagicbell

Sorry I just meant that I wasn't able to find a workaround without making the change to expose the config of conditional directives. I haven't tried to create a pull request / fork this project, because I had been hoping for a reply from the maintainers to know if that was going in the best direction

mikejpeters avatar May 05 '25 19:05 mikejpeters

Gotcha! Thanks for the clarification!

imagicbell avatar May 05 '25 19:05 imagicbell