dotansimha / dotansimha/graphql-code-generator

discriminatedQueryTypes possibilty for typescript-operations

Open
#9,575 0 comments 0 reactions 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 currently implementing a plugin to generate types and helper functions for graphql interfaces and unions. The gist of the idea is generating types for each query e.G. `export type InterfaceNameOfQueryName` that only holds the fields you queried. Using the `export type InterfaceName` in for example a function signature and passing a result of a query, can result to runtime errors, without even throwing any compile errors.

I am as far in the plugin that the concept works and prevents runtime errors from happening, but it still can happen that you can pass in a result of a different query into one of those functions or cast to the types of the other queries IF there are no differences between mandatory fields. Which means you will get an error if there a difference in mandatory fields (preventing runtime errors), but not for optionals.

What can happen here is that you could accidently use the wrong type, receive no compile error, get access to optional fields, which you should not have in the first place, and debug for a long time to find out why a certain field has no value, when the developer expects it to be there.

I propose a discrimant field `__queryName` for each query, as they have that value anyway. Id make a fork or a PR, but without some pointers i am having troubles finding a place to add this feature.

Any help is welcome!

### Describe the solution you'd like

A configurable option `discriminatedQueries` that generate `__queryName` along side the `__typename` of a generated QueryType for `typescript-operations`

### Describe alternatives you've considered

_No response_

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