Skip to content

Commit 9a451bc

Browse files
committed
Resolve validation schema open issues
1 parent ba10348 commit 9a451bc

22 files changed

Lines changed: 3014 additions & 356 deletions

README.md

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,35 @@ You can check [example](https://github.com/Code-Hex/graphql-codegen-typescript-v
4242

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

45+
### TypeScript config and presets
46+
47+
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.
48+
49+
```ts
50+
import type { CodegenConfig } from '@graphql-codegen/cli';
51+
52+
const config: CodegenConfig = {
53+
schema: 'schema.graphql',
54+
generates: {
55+
'./src/gql/': {
56+
preset: 'client',
57+
plugins: [
58+
{
59+
'typescript-validation-schema': {
60+
schema: 'zod',
61+
scalarSchemas: {
62+
DateTime: 'z.string().datetime()',
63+
},
64+
},
65+
},
66+
],
67+
},
68+
},
69+
};
70+
71+
export default config;
72+
```
73+
4574
## Config API Reference
4675

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

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

315+
### `withOperationType`
316+
317+
type: `boolean` default: `false`
318+
319+
Generates Zod schemas for GraphQL operation result selection sets.
320+
Use this when `withObjectType` is too broad because it describes the full GraphQL object type, while your query only selects a subset of fields.
321+
322+
```yml
323+
config:
324+
schema: zod
325+
withOperationType: true
326+
```
327+
328+
Currently supported for `schema: zod` and `schema: zodv4`.
329+
330+
### `maxDepth`
331+
332+
type: `number`
333+
334+
Limits nested object validation depth for `withObjectType` schemas generated by `schema: zod` and `schema: zodv4`.
335+
This is useful for cyclic output graphs where validating the entire object graph would recurse forever.
336+
337+
```yml
338+
config:
339+
schema: zod
340+
withObjectType: true
341+
maxDepth: 1
342+
```
343+
286344
### `validationSchemaExportType`
287345

288346
type: `ValidationSchemaExportType` default: `'function'`
289347

290348
Specify validation schema export type.
291349

350+
### `zodOptionalType`
351+
352+
type: `'nullish' | 'nullable' | 'optional'` default: `'nullish'`
353+
354+
Controls how nullable GraphQL fields are generated for `schema: zod` and `schema: zodv4`.
355+
The default `nullish` mode matches GraphQL input coercion, where a nullable input field may be omitted or passed as `null`.
356+
Use `nullable` or `optional` only when your generated TypeScript `Maybe`/`InputMaybe` contract intentionally differs from GraphQL's default input coercion behavior.
357+
358+
```yml
359+
config:
360+
schema: zod
361+
zodOptionalType: nullable
362+
```
363+
364+
`nullishBehavior` is also accepted as an alias.
365+
366+
### `strictObjectSchemas`
367+
368+
type: `boolean` default: `false`
369+
370+
Appends `.strict()` to generated Zod object schemas.
371+
372+
### `withDescriptions`
373+
374+
type: `boolean` default: `false`
375+
376+
Appends `.describe()` to generated Zod fields from GraphQL descriptions.
377+
378+
### `onlyEnums`
379+
380+
type: `boolean` default: `false`
381+
382+
Generates only enum validation schemas.
383+
292384
### `useEnumTypeAsDefaultValue`
293385

294386
type: `boolean` default: `false`

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,9 @@
100100
"ts-dedent": "^2.2.0",
101101
"ts-jest": "29.4.4",
102102
"typescript": "5.9.2",
103-
"valibot": "1.1.0",
103+
"valibot": "1.3.1",
104104
"vitest": "^3.0.0",
105105
"yup": "1.7.1",
106-
"zod": "4.1.11"
106+
"zod": "4.3.6"
107107
}
108108
}

pnpm-lock.yaml

Lines changed: 51 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)