2024-07-17

GraphQL Codegenでenumをオブジェクトリテラルで生成する

GraphQL Codegen

graphql-codegen のenumsAsConst設定なるものを知った。 非推奨の enum ではなく、オブジェクトリテラルで生成してくれる。

// オブジェクトリテラル
const Position = {
	Top: 0,
	Right: 1,
	Bottom: 2,
	Left: 3,
} as const;

client preset は現状未対応だった。

https://github.com/dotansimha/graphql-code-generator/pull/9981

enumsAsType

enumsAsConstの代わりに enum を Union 型で生成してくれるenumsAsTypeが使えそう

参考

https://typescriptbook.jp/reference/values-types-variables/enum/enum-problems-and-alternatives-to-enums