Skip to content

Commit d3eea5b

Browse files
add support for avoidOptionals codegen config (#325)
Co-authored-by: Conrawl Rogers <me@diizzayy.com>
1 parent 7735746 commit d3eea5b

File tree

4 files changed

+21
-2
lines changed

4 files changed

+21
-2
lines changed

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ export default defineNuxtConfig({
7070
useTypeImports: true,
7171
dedupeFragments: true,
7272
onlyOperationTypes: true,
73+
avoidOptionals: false,
7374
disableOnBuild: false
7475
}
7576
}
@@ -108,6 +109,12 @@ Only generate the types for the operations in your GraphQL documents.
108109
When set to true, only the types needed for your operations will be generated.
109110
When set to false, all types from the GraphQL API will be generated.
110111

112+
### `avoidOptionals`
113+
114+
- default: `false`
115+
116+
Avoid using TypeScript optionals on types. See [GraphQL Code Generator documentation](https://the-guild.dev/graphql/codegen/plugins/typescript/typescript#usage-examples) for more options
117+
111118
### `disableOnBuild`
112119

113120
- default: `false`

src/generate.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ function prepareConfig (options: GenerateOptions & GqlCodegen): CodegenConfig {
4848
onlyOperationTypes: options.onlyOperationTypes,
4949
namingConvention: {
5050
enumValues: 'change-case-all#upperCaseFirst'
51-
}
51+
},
52+
avoidOptionals: options?.avoidOptionals
5253
}
5354

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

src/module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ export default defineNuxtModule<GqlConfig>({
4848
useTypeImports: true,
4949
dedupeFragments: true,
5050
disableOnBuild: false,
51-
onlyOperationTypes: true
51+
onlyOperationTypes: true,
52+
avoidOptionals: false
5253
}
5354

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

src/types.d.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,16 @@ export interface GqlCodegen {
172172
* @default true
173173
* */
174174
onlyOperationTypes?: boolean
175+
176+
/**
177+
* Avoid using TypeScript optionals on generated types.
178+
*/
179+
avoidOptionals?: boolean | {
180+
field?: boolean
181+
inputValue?: boolean
182+
object?: boolean
183+
defaultValue?: boolean
184+
}
175185
}
176186

177187
export interface GqlConfig<T = GqlClient> {

0 commit comments

Comments
 (0)