Skip to content

Commit 451d128

Browse files
authored
feat: support enumsAsTypes and enumsAsConst options (#627)
1 parent 60a876a commit 451d128

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

docs/content/1.getting-started/4.configuration.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ export default defineNuxtConfig({
7373
avoidOptionals: false,
7474
disableOnBuild: false,
7575
maybeValue: 'T | null',
76-
scalars: {}
76+
scalars: {},
77+
enumsAsTypes: false,
78+
enumsAsConst: false,
7779
}
7880
}
7981
})
@@ -135,7 +137,18 @@ Allow to override the type value of `Maybe`. See [GraphQL Code Generator documen
135137

136138
Extends or overrides the built-in scalars and custom GraphQL scalars to a custom type. See [GraphQL Code Generator documentation](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#scalars) for more options
137139

138-
140+
### `enumsAsTypes`
141+
142+
- default: `false`
143+
144+
Generates enum as TypeScript string union type instead of an enum. Useful if you wish to generate .d.ts declaration file instead of .ts, or if you want to avoid using TypeScript enums due to bundle size concerns. See [GraphQL Code Generator documentation](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#enumsastypes) for more options
145+
146+
### `enumsAsConst`
147+
148+
- default: `false`
149+
150+
Generates enum as TypeScript const assertions instead of enum. This can even be used to enable enum-like patterns in plain JavaScript code if you choose not to use TypeScript’s enum construct. See [GraphQL Code Generator documentation](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#enumsasconst) for more options
151+
139152
## Client configuration
140153

141154
::alert{type="warning"}

src/generate.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ function prepareConfig(options: GenerateOptions & GqlCodegen): CodegenConfig {
5151
},
5252
avoidOptionals: options?.avoidOptionals,
5353
maybeValue: options?.maybeValue,
54-
scalars: options?.scalars
54+
scalars: options?.scalars,
55+
enumsAsTypes: options?.enumsAsTypes,
56+
enumsAsConst: options?.enumsAsConst
5557
}
5658

5759
const generates: CodegenConfig['generates'] = Object.entries(options.clients || {}).reduce((acc, [k, v]) => {

src/module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,9 @@ export default defineNuxtModule<GqlConfig>({
5353
onlyOperationTypes: true,
5454
avoidOptionals: false,
5555
maybeValue: 'T | null',
56-
scalars: {}
56+
scalars: {},
57+
enumsAsTypes: false,
58+
enumsAsConst: false
5759
}
5860

5961
config.codegen = !!config.codegen && defu<GqlCodegen, [GqlCodegen]>(config.codegen, codegenDefaults)

src/types.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,20 @@ export interface GqlCodegen {
207207
(https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#scalars
208208
*/
209209
scalars?: string | { [name: string]: string | { input: string, output: string } }
210+
211+
/**
212+
* Generates enum as TypeScript string union type instead of an enum.
213+
* Useful if you wish to generate .d.ts declaration file instead of .ts, or if you want to avoid using TypeScript enums due to bundle size concerns.
214+
(https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#enumsastypes
215+
*/
216+
enumsAsTypes?: boolean
217+
218+
/**
219+
* Generates enum as TypeScript const assertions instead of enum.
220+
* This can even be used to enable enum-like patterns in plain JavaScript code if you choose not to use TypeScript’s enum construct.
221+
(https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#enumsasconst
222+
*/
223+
enumsAsConst?: boolean
210224
}
211225

212226
export interface GqlConfig<T = GqlClient> {

0 commit comments

Comments
 (0)