dotansimha / dotansimha/graphql-code-generator

Configure conditional directives

Open
#10,009 5 comments 1 reaction 0 assignees View on GitHub
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

### 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.

```gql
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_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.