@nxgt/graphql-codegen-zod 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,220 @@
1
+ # Troubleshooting
2
+
3
+ One entry per error you can hit, headed by the message you will search for.
4
+ Configuration, Scalars, Operations and The schema happen while
5
+ `graphql-codegen` runs; The generated file is what `tsc` and the generated
6
+ schemas report.
7
+
8
+ - [Configuration](#configuration)
9
+ - [Scalars](#scalars)
10
+ - [Operations](#operations)
11
+ - [The generated file](#the-generated-file)
12
+ - [The schema](#the-schema)
13
+
14
+ ## Configuration
15
+
16
+ ### `@nxgt/graphql-codegen-zod: namingConvention "my-module#myCase" is not one this plugin reads. Use 'keep', a function, or 'change-case-all#<case>' with one of camelCase, capitalCase, constantCase, dotCase, headerCase, noCase, paramCase, pascalCase, pathCase, sentenceCase, snakeCase, lowerCase, upperCase, lowerCaseFirst, upperCaseFirst.`
17
+
18
+ **When:** running codegen with a `namingConvention` string that names another module, or a case not in the list. The string in the message is what you wrote.
19
+ **Why:** the plugin reads the forms the typescript plugins take for type names, but only `change-case-all` and `change-case` cases; it does not load a module.
20
+ **Fix:** use one of the forms, or pass a function.
21
+
22
+ ```ts
23
+ config: { namingConvention: 'change-case-all#pascalCase' }
24
+ // or
25
+ config: { namingConvention: (name) => name.toUpperCase() }
26
+ ```
27
+
28
+ ## Scalars
29
+
30
+ ### `@nxgt/graphql-codegen-zod: the scalar Money is not in zodScalars, and no scalarSchemas is set. Map it: zodScalars: { Money: './module#export' }, or set scalarSchemas: '@nxgt/graphql-scalars'.`
31
+
32
+ **When:** running codegen on a schema with a custom scalar (here `Money`).
33
+ **Why:** a scalar written as `z.unknown()` would check nothing, in silence, so
34
+ the plugin refuses instead.
35
+ **Fix:** map the scalar, or point `scalarSchemas` at a module that has it.
36
+
37
+ ```ts
38
+ config: { zodScalars: { Money: './money#moneySchema' } }
39
+ ```
40
+
41
+ ### `@nxgt/graphql-codegen-zod: the scalar Money is neither in zodScalars nor in @nxgt/graphql-scalars's scalarSchemas. Map it: zodScalars: { Money: './module#export' }, or set scalarSchemas: '@nxgt/graphql-scalars'.`
42
+
43
+ **When:** running codegen with `scalarSchemas` set, on a scalar the record has
44
+ no key for. The module path in the message is the one you configured.
45
+ **Why:** the plugin loaded the record and `Money` is not one of its keys.
46
+ **Fix:** map that one scalar next to the record; `zodScalars` wins over it.
47
+
48
+ ```ts
49
+ config: {
50
+ scalarSchemas: '@nxgt/graphql-scalars',
51
+ zodScalars: { Money: './money#moneySchema' },
52
+ }
53
+ ```
54
+
55
+ ### `@nxgt/graphql-codegen-zod: zodScalars.Money is "moneySchema"; write it '<module>#<export>', e.g. './money#moneySchema'.`
56
+
57
+ **When:** running codegen with a `zodScalars` entry that is not `module#export`.
58
+ **Why:** the entry needs a module and an export name on either side of the
59
+ last `#`; the one in the message has none, or an empty side.
60
+ **Fix:**
61
+
62
+ ```ts
63
+ config: { zodScalars: { Money: './money#moneySchema' } }
64
+ ```
65
+
66
+ ### `@nxgt/graphql-codegen-zod: @nxgt/graphql-scalars exports no scalarSchemas record.`
67
+
68
+ **When:** running codegen with `scalarSchemas` set.
69
+ **Why:** the module loaded, but exports no `scalarSchemas` object. The module
70
+ path in the message is the one you configured. For `@nxgt/graphql-scalars`,
71
+ the record is exported from 0.4.0; an older version has none.
72
+ **Fix:** upgrade the package, or export the record yourself.
73
+
74
+ ```ts
75
+ // scalars.ts
76
+ import { z } from 'zod';
77
+
78
+ export const scalarSchemas = {
79
+ DateTime: z.iso.datetime({ offset: true }),
80
+ };
81
+ ```
82
+
83
+ ## Operations
84
+
85
+ ### `@nxgt/graphql-codegen-zod: SignUp's $input has a type the schema does not define.`
86
+
87
+ **When:** running codegen on a document.
88
+ **Why:** the operation (`SignUp`) declares a variable (`$input`) of a type the
89
+ schema has no input type for, usually a typo, or a document written against
90
+ another schema.
91
+ **Fix:** declare the type in the schema, or correct the variable's type in the
92
+ document.
93
+
94
+ ```graphql
95
+ mutation SignUp($input: SignUpInput!) { signUp(input: $input) }
96
+ ```
97
+
98
+ ### `@nxgt/graphql-codegen-zod: Both($v:) is passed to Query.e(v:) (format: "email") and to Query.u(v:) (format: "uuid"), whose schemas cannot both apply. Use one variable for each.`
99
+
100
+ **When:** running codegen on an operation that passes one variable to two
101
+ places whose `@constraint(format:)` differ. The operation, variable and places
102
+ in the message are yours.
103
+ **Why:** each `format` replaces the schema, so the variable cannot be both.
104
+ **Fix:** use one variable for each.
105
+
106
+ ```graphql
107
+ query Both($e: String, $u: String) { e(v: $e) u(v: $u) }
108
+ ```
109
+
110
+ ## The generated file
111
+
112
+ ### `Property 'Money' does not exist on type '{ DateTime: ... }'` (or `TS2339`)
113
+
114
+ **When:** type-checking the generated file, with `scalarSchemas` set.
115
+ **Why:** the plugin loads the record's module to check its keys, but cannot
116
+ when it is a `.ts` file under Node. It then trusts the record, and the file
117
+ reads `scalarSchemas.Money` which the record lacks.
118
+ **Fix:** add the key to the record, or map the scalar with `zodScalars`, or run
119
+ codegen under Bun so the module loads and the plugin refuses with the message
120
+ above.
121
+
122
+ ```ts
123
+ config: { zodScalars: { Money: './money#moneySchema' } }
124
+ ```
125
+
126
+ ### `TS2300: Duplicate identifier 'SignUpInput'`
127
+
128
+ **When:** type-checking, with the plugin and `@graphql-codegen/typescript`
129
+ writing to the same file.
130
+ **Why:** both write a type of the same name.
131
+ **Fix:** generate into a file of its own.
132
+
133
+ ```ts
134
+ generates: {
135
+ 'src/generated/zod.ts': { plugins: ['@nxgt/graphql-codegen-zod'] },
136
+ }
137
+ ```
138
+
139
+ ### A client request is refused for `{ id: 5 }`: `Invalid input: expected string, received number`
140
+
141
+ **When:** `safeParse` of a variables schema with a number where the schema has
142
+ an `ID`.
143
+ **Why:** `graphql` accepts an `Int` for an `ID`; the generated schema is
144
+ `z.string()`: an id is a string in what the client sends, as in what the
145
+ resolver receives.
146
+ **Fix:** send the id as a string.
147
+
148
+ ```ts
149
+ zUserQueryVariables.safeParse({ id: String(id) });
150
+ ```
151
+
152
+ ### A client request is refused: `Unrecognized key: "nickname"`
153
+
154
+ **When:** `safeParse` of a variables schema whose input object carries a field
155
+ the input type does not declare.
156
+ **Why:** input types are `z.strictObject`, as `graphql` refuses an unknown
157
+ field. The variables object itself ignores an extra variable, as `graphql` does.
158
+ **Fix:** remove the field, or add it to the input type in the schema.
159
+
160
+ ## The schema
161
+
162
+ ### `@nxgt/graphql-codegen-zod: the file would declare FilterInput twice: two GraphQL names, or an input type in a cycle and its Input type, give the same name. Rename one of the GraphQL types, or set typesSuffix.`
163
+
164
+ **When:** running codegen on a schema where two declarations of the same kind
165
+ get one name: an input type in a cycle (`Filter`) declares `FilterInput`, and
166
+ the schema also has an input type `FilterInput`.
167
+ **Why:** the file cannot declare one name twice.
168
+ **Fix:** rename one of the GraphQL types, or set `typesSuffix`: `Filter`
169
+ then declares `FilterTInput`, apart from `FilterInputT`. A `typesPrefix`
170
+ does not part them.
171
+
172
+ ```graphql
173
+ input Filter { and: [Filter] }
174
+ input FilterFields { a: Int } # was FilterInput
175
+ ```
176
+
177
+ ### `@nxgt/graphql-codegen-zod: the default of Query.a(n:) holds 9007199254740993, which a JavaScript number cannot keep exact. Write it as a string, if its scalar takes one.`
178
+
179
+ **When:** running codegen on an argument, input field or variable whose
180
+ default is an integer past `Number.MAX_SAFE_INTEGER`, given to an `ID` or a
181
+ custom scalar (`Long`, `BigInt`). An `Int` or a `Float` rounds on the server
182
+ too, and is written as it is.
183
+ **Why:** the generated `.prefault(...)` would hold a rounded number, while the
184
+ server hands the scalar every digit.
185
+ **Fix:** write the default as a string, when the scalar parses one.
186
+
187
+ ```graphql
188
+ type Query { a(n: Long = "9007199254740993"): Int }
189
+ ```
190
+
191
+ ### `@nxgt/graphql-codegen-zod: the schema declares @constraint, but Query has no SDL to read it from (an introspected schema?). Point codegen's schema at the SDL files.`
192
+
193
+ **When:** running codegen on a schema that declares `@constraint` but was
194
+ introspected or loaded from a URL. The type in the message is the first found.
195
+ **Why:** an introspected schema carries no directives on its arguments, so
196
+ every constraint would be dropped in silence.
197
+ **Fix:** point codegen's `schema` at the SDL files. A schema without
198
+ `@constraint` can still be introspected; its defaults are read back.
199
+
200
+ ```ts
201
+ schema: 'src/schema/**/*.graphql',
202
+ ```
203
+
204
+ ### Errors of `@constraint`
205
+
206
+ The plugin refuses a schema `withValidation` would refuse, with the same
207
+ message, for example `@constraint(minLength) on Query.a(n:) needs a String or
208
+ an ID, not Int.`. Each is explained, with its fix, in
209
+ [`@nxgt/graphql-validation`'s troubleshooting](https://github.com/softistx/nxgt-graphql/blob/develop/packages/graphql-validation/docs/troubleshooting.md).
210
+
211
+ ### No error, and nothing is constrained
212
+
213
+ **When:** the generated schemas have no `.min()`, `.email()` and the like.
214
+ **Why:** the schema codegen reads does not declare `@constraint`, so the
215
+ plugin reads none; plain types are still generated, without a warning.
216
+ **Fix:** add the directive to the schema codegen loads.
217
+
218
+ ```ts
219
+ import { constraintTypeDefs } from '@nxgt/graphql-validation';
220
+ ```
package/package.json CHANGED
@@ -1,6 +1,62 @@
1
1
  {
2
- "name": "@nxgt/graphql-codegen-zod",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
2
+ "name": "@nxgt/graphql-codegen-zod",
3
+ "version": "0.1.0",
4
+ "description": "A graphql-codegen plugin that writes Zod schemas from your SDL — inputs, enums, field arguments and operation variables — with the @constraint rules of @nxgt/graphql-validation",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "main": "./dist/index.js",
8
+ "types": "./dist/index.d.ts",
9
+ "files": [
10
+ "dist",
11
+ "docs",
12
+ "README.md",
13
+ "package.json",
14
+ "LICENSE"
15
+ ],
16
+ "exports": {
17
+ ".": {
18
+ "types": "./dist/index.d.ts",
19
+ "import": "./dist/index.js",
20
+ "default": "./dist/index.js"
21
+ },
22
+ "./package.json": "./package.json"
23
+ },
24
+ "keywords": [
25
+ "graphql",
26
+ "codegen",
27
+ "graphql-codegen",
28
+ "zod",
29
+ "validation",
30
+ "constraint",
31
+ "typescript"
32
+ ],
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/softistx/nxgt-graphql.git",
36
+ "directory": "packages/graphql-codegen-zod"
37
+ },
38
+ "publishConfig": {
39
+ "registry": "https://registry.npmjs.org",
40
+ "access": "public"
41
+ },
42
+ "scripts": {
43
+ "build": "bun run ../../build.ts",
44
+ "test": "bun test src",
45
+ "typecheck": "tsc --noEmit",
46
+ "generated:write": "bun run test/write-generated.ts"
47
+ },
48
+ "dependencies": {
49
+ "@nxgt/graphql-validation": "^0.2.0",
50
+ "change-case": "^5.4.4"
51
+ },
52
+ "devDependencies": {
53
+ "@types/bun": "^1.4.0",
54
+ "graphql": "16.11.0",
55
+ "zod": "4.6.5"
56
+ },
57
+ "peerDependencies": {
58
+ "graphql": "^16.11.0 || ^17.0.0",
59
+ "typescript": "^6.0.3",
60
+ "zod": ">=4.6.5 <5"
61
+ }
62
+ }