Skip to content

Commit

Permalink
add support for avoidOptionals codegen config (#325)
Browse files Browse the repository at this point in the history
Co-authored-by: Conrawl Rogers <[email protected]>
  • Loading branch information
AveryBibeau and Diizzayy committed Jul 17, 2023
1 parent 7735746 commit d3eea5b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/content/1.getting-started/4.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default defineNuxtConfig({
useTypeImports: true,
dedupeFragments: true,
onlyOperationTypes: true,
avoidOptionals: false,
disableOnBuild: false
}
}
Expand Down Expand Up @@ -108,6 +109,12 @@ Only generate the types for the operations in your GraphQL documents.
When set to true, only the types needed for your operations will be generated.
When set to false, all types from the GraphQL API will be generated.

### `avoidOptionals`

- default: `false`

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

### `disableOnBuild`

- default: `false`
Expand Down
3 changes: 2 additions & 1 deletion src/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ function prepareConfig (options: GenerateOptions & GqlCodegen): CodegenConfig {
onlyOperationTypes: options.onlyOperationTypes,
namingConvention: {
enumValues: 'change-case-all#upperCaseFirst'
}
},
avoidOptionals: options?.avoidOptionals
}

const generates: CodegenConfig['generates'] = Object.entries(options.clients || {}).reduce((acc, [k, v]) => {
Expand Down
3 changes: 2 additions & 1 deletion src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ export default defineNuxtModule<GqlConfig>({
useTypeImports: true,
dedupeFragments: true,
disableOnBuild: false,
onlyOperationTypes: true
onlyOperationTypes: true,
avoidOptionals: false
}

config.codegen = !!config.codegen && defu<GqlCodegen, [GqlCodegen]>(config.codegen, codegenDefaults)
Expand Down
10 changes: 10 additions & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,16 @@ export interface GqlCodegen {
* @default true
* */
onlyOperationTypes?: boolean

/**
* Avoid using TypeScript optionals on generated types.
*/
avoidOptionals?: boolean | {
field?: boolean
inputValue?: boolean
object?: boolean
defaultValue?: boolean
}
}

export interface GqlConfig<T = GqlClient> {
Expand Down

0 comments on commit d3eea5b

Please sign in to comment.