Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,35 @@ You can check [example](https://github.com/Code-Hex/graphql-codegen-typescript-v

The Q&A for each schema is written in the README in the respective example directory.

### TypeScript config and presets

When you use `codegen.ts` with a preset such as `client`, put this plugin's options under the plugin entry. Some presets do not pass the shared generate-level `config` object to every plugin.

```ts
import type { CodegenConfig } from '@graphql-codegen/cli';

const config: CodegenConfig = {
schema: 'schema.graphql',
generates: {
'./src/gql/': {
preset: 'client',
plugins: [
{
'typescript-validation-schema': {
schema: 'zod',
scalarSchemas: {
DateTime: 'z.string().datetime()',
},
},
},
],
},
},
};

export default config;
```

## Config API Reference

### `schema`
Expand Down Expand Up @@ -283,12 +312,75 @@ It is currently added for the purpose of using simple objects. See also [#20](ht

This option currently **does not support fragment** generation. If you are interested, send me PR would be greatly appreciated!

### `withOperationType`

type: `boolean` default: `false`

Generates Zod schemas for GraphQL operation result selection sets.
Use this when `withObjectType` is too broad because it describes the full GraphQL object type, while your query only selects a subset of fields.

```yml
config:
schema: zod
withOperationType: true
```

Currently supported for `schema: zod` and `schema: zodv4`.

### `maxDepth`

type: `number`

Limits nested object validation depth for `withObjectType` schemas generated by `schema: zod` and `schema: zodv4`.
This is useful for cyclic output graphs where validating the entire object graph would recurse forever.

```yml
config:
schema: zod
withObjectType: true
maxDepth: 1
```

### `validationSchemaExportType`

type: `ValidationSchemaExportType` default: `'function'`

Specify validation schema export type.

### `zodOptionalType`

type: `'nullish' | 'nullable' | 'optional'` default: `'nullish'`

Controls how nullable GraphQL fields are generated for `schema: zod` and `schema: zodv4`.
The default `nullish` mode matches GraphQL input coercion, where a nullable input field may be omitted or passed as `null`.
Use `nullable` or `optional` only when your generated TypeScript `Maybe`/`InputMaybe` contract intentionally differs from GraphQL's default input coercion behavior.

```yml
config:
schema: zod
zodOptionalType: nullable
```

`nullishBehavior` is also accepted as an alias.

### `strictObjectSchemas`

type: `boolean` default: `false`

Appends `.strict()` to generated Zod object schemas.

### `withDescriptions`

type: `boolean` default: `false`

Appends `.describe()` to generated Zod fields from GraphQL descriptions.

### `onlyEnums`

type: `boolean` default: `false`

Generates only enum validation schemas.

### `useEnumTypeAsDefaultValue`

type: `boolean` default: `false`
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@
"ts-jest": "29.4.9",
"typescript": "6.0.3",
"valibot": "1.3.1",
"vitest": "^4.0.0",
"vitest": "^3.2.4",
"yup": "1.7.1",
"zod": "4.3.6"
}
Expand Down
Loading
Loading