cometkim / gatsby-plugin-typegen

Let's give developers using GatsbyJS better DX with extreme type-safety
https://www.gatsbyjs.org/packages/gatsby-plugin-typegen/
MIT License
204 stars 25 forks source link

Global types with enum values #81

Closed chrbala closed 3 years ago

chrbala commented 4 years ago

Because of the update to export only global types, enum values break. That is, because enum values pass through the types to the underlying Gatbsy compilation process, the Gatsby compilation process doesn't know how to handle the global types. Example below.

GraphQL definition

enum MyEnum {
  SOME_VALUE
  OTHER_VALUE
}

This gets transformed to the following in gatsby-types.ts

enum MyEnum {
  SOME_VALUE = 'SOME_VALUE',
  OTHER_VALUE = 'OTHER_VALUE',
}

Typescript then allows the following usage:

const acceptMyEnum = (value: GatsbyTypes.MyEnum) => value == GatsbyTypes.MyEnum.SOME_VALUE ? "something" : "something else";

But the actual Gatsby build fails where GatbsyTypes is needed in the runtime because it can't find GatsbyTypes with an error of "GatsbyTypes" is not defined. Is it possible to emit both the global types and the older importable types? Otherwise, is there better solution to this?

cometkim commented 4 years ago

Duplicates #78

These enum values will be changed into an internal type union after #79 is merged.

orblazer commented 3 years ago

Hello @cometkim, this bug is still present. In the release 2.2.0 that PR #80 is missing, probably fail.

cometkim commented 3 years ago

Oh no :cry: @orblazer you right. Thank you for notifying it.

image

cometkim commented 3 years ago

Published at v2.2.2

https://github.com/cometkim/gatsby-plugin-typegen/pull/135