dotansimha / dotansimha/graphql-code-generator

Typescript: generate *both* enums and types?

Open
#2,471 1 comment 8 reactions 0 assignees View on GitHub
core kind/enhancement plugins
Dominant language
TypeScript
Stars
11.3k
Forks
1.4k
Avg merge
1d 1h
Merged PRs (30d)
23

Description

Feature request: some way to expose GraphQL enums as both types _and_ enums.

**Context**

I'm updating an old codebase that was using a combination of apollo-codegen and an early version of graphql-code-generator.

In the old system, one generator produced the equivalent of today's `enumsAsTypes: true`, and the other `enumsAsTypes: false`. The codebase has come to rely on having both available.

**Describe the solution you'd like**

I'd like some way to generate both.

**Describe alternatives you've considered**
1) Generate enums, and try to derive a union type using TypeScript.

so, e.g. generate `enum Foo { Bar = "bar", Baz = "baz" }`, and then try to implement `type EnumValuesOf` to produce a type of `"bar" | "baz"`.

I didn't get very far with this.

2) Generate separate files with `enumsAsTypes: true` and `enumsAsTypes: false`.

This could work, but duplicates everything that's not an enum.

Perhaps I could tell the generator to emit a file that has _only_ enums in it? Then I'd emit one file with everything (and `enumsAsTypes:false`), and then emit a second enums-only file with `enumsAsTypes:true`.

**Other ideas?**

I'd be interested to hear other approaches to this problem, especially if they're achievable with the current versions of Typescript and graphql-code-generator ;)

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.