nitro-graphql 2.0.0-beta.4 → 2.0.0-beta.40

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.
Files changed (162) hide show
  1. package/README.md +441 -27
  2. package/dist/cli/commands/generate.d.mts +26 -0
  3. package/dist/cli/commands/generate.mjs +196 -0
  4. package/dist/cli/commands/index.d.mts +4 -0
  5. package/dist/cli/commands/index.mjs +5 -0
  6. package/dist/cli/commands/init.d.mts +46 -0
  7. package/dist/cli/commands/init.mjs +195 -0
  8. package/dist/cli/commands/validate.d.mts +10 -0
  9. package/dist/cli/commands/validate.mjs +69 -0
  10. package/dist/cli/completions.d.mts +7 -0
  11. package/dist/cli/completions.mjs +34 -0
  12. package/dist/cli/config.d.mts +75 -0
  13. package/dist/cli/config.mjs +20 -0
  14. package/dist/cli/index.d.mts +24 -0
  15. package/dist/cli/index.mjs +253 -0
  16. package/dist/core/codegen/client.d.mts +23 -0
  17. package/dist/core/codegen/client.mjs +150 -0
  18. package/dist/core/codegen/document-loader.d.mts +10 -0
  19. package/dist/core/codegen/document-loader.mjs +18 -0
  20. package/dist/core/codegen/index.d.mts +8 -0
  21. package/dist/core/codegen/index.mjs +9 -0
  22. package/dist/core/codegen/plugin.d.mts +20 -0
  23. package/dist/core/codegen/plugin.mjs +30 -0
  24. package/dist/core/codegen/runtime.d.mts +20 -0
  25. package/dist/core/codegen/runtime.mjs +60 -0
  26. package/dist/core/codegen/schema-loader.d.mts +28 -0
  27. package/dist/core/codegen/schema-loader.mjs +128 -0
  28. package/dist/core/codegen/server.d.mts +28 -0
  29. package/dist/core/codegen/server.mjs +143 -0
  30. package/dist/core/codegen/validation.d.mts +13 -0
  31. package/dist/core/codegen/validation.mjs +96 -0
  32. package/dist/core/config.d.mts +50 -0
  33. package/dist/core/config.mjs +82 -0
  34. package/dist/core/constants.d.mts +188 -0
  35. package/dist/core/constants.mjs +210 -0
  36. package/dist/core/index.d.mts +32 -0
  37. package/dist/core/index.mjs +26 -0
  38. package/dist/core/scanning/ast-scanner.d.mts +23 -0
  39. package/dist/core/scanning/ast-scanner.mjs +103 -0
  40. package/dist/core/scanning/common.d.mts +37 -0
  41. package/dist/core/scanning/common.mjs +60 -0
  42. package/dist/core/scanning/directives.d.mts +10 -0
  43. package/dist/core/scanning/directives.mjs +29 -0
  44. package/dist/core/scanning/documents.d.mts +21 -0
  45. package/dist/core/scanning/documents.mjs +43 -0
  46. package/dist/core/scanning/index.d.mts +7 -0
  47. package/dist/core/scanning/index.mjs +8 -0
  48. package/dist/core/scanning/resolvers.d.mts +10 -0
  49. package/dist/core/scanning/resolvers.mjs +58 -0
  50. package/dist/core/scanning/schemas.d.mts +14 -0
  51. package/dist/core/scanning/schemas.mjs +64 -0
  52. package/dist/core/schema/builder.d.mts +53 -0
  53. package/dist/core/schema/builder.mjs +70 -0
  54. package/dist/core/schema/federation.d.mts +34 -0
  55. package/dist/core/schema/federation.mjs +40 -0
  56. package/dist/core/schema/index.d.mts +3 -0
  57. package/dist/core/schema/index.mjs +4 -0
  58. package/dist/core/types/adapter.d.mts +58 -0
  59. package/dist/core/types/codegen.d.mts +133 -0
  60. package/dist/core/types/config.d.mts +212 -0
  61. package/dist/core/types/config.mjs +1 -0
  62. package/dist/{utils/define.d.ts → core/types/define.d.mts} +3 -30
  63. package/dist/core/types/define.mjs +1 -0
  64. package/dist/core/types/index.d.mts +5 -0
  65. package/dist/core/types/index.mjs +1 -0
  66. package/dist/core/types/scanning.d.mts +69 -0
  67. package/dist/core/types/scanning.mjs +1 -0
  68. package/dist/{utils/directive-parser.d.ts → core/utils/directive-parser.d.mts} +21 -4
  69. package/dist/{utils/directive-parser.js → core/utils/directive-parser.mjs} +25 -34
  70. package/dist/core/utils/errors.d.mts +77 -0
  71. package/dist/core/utils/errors.mjs +93 -0
  72. package/dist/core/utils/file-io.d.mts +24 -0
  73. package/dist/core/utils/file-io.mjs +47 -0
  74. package/dist/core/utils/imports.d.mts +15 -0
  75. package/dist/core/utils/imports.mjs +25 -0
  76. package/dist/core/utils/index.d.mts +7 -0
  77. package/dist/core/utils/index.mjs +8 -0
  78. package/dist/core/utils/logger.d.mts +19 -0
  79. package/dist/core/utils/logger.mjs +38 -0
  80. package/dist/core/utils/ofetch-templates.d.mts +30 -0
  81. package/dist/core/utils/ofetch-templates.mjs +135 -0
  82. package/dist/core/validation/external-services.d.mts +11 -0
  83. package/dist/core/validation/external-services.mjs +34 -0
  84. package/dist/core/validation/index.d.mts +2 -0
  85. package/dist/core/validation/index.mjs +3 -0
  86. package/dist/define.d.mts +294 -0
  87. package/dist/define.mjs +323 -0
  88. package/dist/index.d.mts +6 -0
  89. package/dist/index.mjs +6 -0
  90. package/dist/nitro/adapter.d.mts +30 -0
  91. package/dist/nitro/adapter.mjs +97 -0
  92. package/dist/{utils/apollo.d.ts → nitro/apollo.d.mts} +3 -3
  93. package/dist/nitro/apollo.mjs +59 -0
  94. package/dist/nitro/codegen.d.mts +19 -0
  95. package/dist/nitro/codegen.mjs +141 -0
  96. package/dist/nitro/config.d.mts +52 -0
  97. package/dist/nitro/config.mjs +58 -0
  98. package/dist/nitro/index.d.mts +46 -0
  99. package/dist/nitro/index.mjs +65 -0
  100. package/dist/nitro/paths.d.mts +54 -0
  101. package/dist/nitro/paths.mjs +93 -0
  102. package/dist/nitro/rollup.d.mts +6 -0
  103. package/dist/nitro/rollup.mjs +95 -0
  104. package/dist/nitro/routes/apollo-server.d.mts +6 -0
  105. package/dist/nitro/routes/apollo-server.mjs +71 -0
  106. package/dist/nitro/routes/debug-template.d.mts +15 -0
  107. package/dist/nitro/routes/debug-template.mjs +385 -0
  108. package/dist/nitro/routes/debug.d.mts +55 -0
  109. package/dist/nitro/routes/debug.mjs +102 -0
  110. package/dist/nitro/routes/graphql-yoga.d.mts +6 -0
  111. package/dist/nitro/routes/graphql-yoga.mjs +62 -0
  112. package/dist/nitro/routes/health.d.mts +10 -0
  113. package/dist/{routes/health.js → nitro/routes/health.mjs} +4 -3
  114. package/dist/nitro/setup/file-watcher.d.mts +16 -0
  115. package/dist/nitro/setup/file-watcher.mjs +80 -0
  116. package/dist/nitro/setup/logging.d.mts +17 -0
  117. package/dist/nitro/setup/logging.mjs +66 -0
  118. package/dist/nitro/setup/rollup-integration.d.mts +16 -0
  119. package/dist/nitro/setup/rollup-integration.mjs +90 -0
  120. package/dist/nitro/setup/routes.d.mts +10 -0
  121. package/dist/nitro/setup/routes.mjs +35 -0
  122. package/dist/nitro/setup/ts-config.d.mts +11 -0
  123. package/dist/nitro/setup/ts-config.mjs +69 -0
  124. package/dist/nitro/setup.d.mts +12 -0
  125. package/dist/nitro/setup.mjs +231 -0
  126. package/dist/nitro/types.d.mts +378 -0
  127. package/dist/nitro/types.mjs +1 -0
  128. package/dist/nitro/virtual/generators.d.mts +31 -0
  129. package/dist/nitro/virtual/generators.mjs +193 -0
  130. package/dist/nitro/virtual/stubs.d.mts +20 -0
  131. package/dist/nitro/virtual/stubs.mjs +31 -0
  132. package/dist/{ecosystem/nuxt.d.ts → nuxt.d.mts} +1 -1
  133. package/dist/nuxt.mjs +109 -0
  134. package/dist/{graphql/server.d.ts → stubs/index.d.mts} +5 -1
  135. package/dist/stubs/index.mjs +1 -0
  136. package/package.json +97 -78
  137. package/dist/ecosystem/nuxt.js +0 -67
  138. package/dist/graphql/index.d.ts +0 -5
  139. package/dist/index.d.ts +0 -8
  140. package/dist/index.js +0 -264
  141. package/dist/rollup.js +0 -119
  142. package/dist/routes/apollo-server.d.ts +0 -6
  143. package/dist/routes/apollo-server.js +0 -89
  144. package/dist/routes/graphql-yoga.d.ts +0 -6
  145. package/dist/routes/graphql-yoga.js +0 -91
  146. package/dist/routes/health.d.ts +0 -6
  147. package/dist/types/index.d.ts +0 -128
  148. package/dist/types/standard-schema.d.ts +0 -59
  149. package/dist/utils/apollo.js +0 -61
  150. package/dist/utils/client-codegen.d.ts +0 -38
  151. package/dist/utils/client-codegen.js +0 -290
  152. package/dist/utils/define.js +0 -57
  153. package/dist/utils/index.d.ts +0 -39
  154. package/dist/utils/index.js +0 -250
  155. package/dist/utils/server-codegen.d.ts +0 -7
  156. package/dist/utils/server-codegen.js +0 -136
  157. package/dist/utils/type-generation.d.ts +0 -7
  158. package/dist/utils/type-generation.js +0 -287
  159. package/dist/vite.d.ts +0 -25
  160. package/dist/vite.js +0 -40
  161. /package/dist/{graphql/index.js → core/types/adapter.mjs} +0 -0
  162. /package/dist/{graphql/server.js → core/types/codegen.mjs} +0 -0
@@ -1,250 +0,0 @@
1
- import { directiveParser, generateDirectiveSchema, generateDirectiveSchemas } from "./directive-parser.js";
2
- import { join, relative } from "pathe";
3
- import { readFile } from "node:fs/promises";
4
- import { hash } from "ohash";
5
- import { parseAsync } from "oxc-parser";
6
- import { glob } from "tinyglobby";
7
-
8
- //#region src/utils/index.ts
9
- const GLOB_SCAN_PATTERN = "**/*.{graphql,gql,js,mjs,cjs,ts,mts,cts,tsx,jsx}";
10
- /**
11
- * Get all Nuxt layer directories from Nitro config
12
- */
13
- function getLayerDirectories(nitro) {
14
- return nitro.options.graphql?.layerDirectories || [];
15
- }
16
- /**
17
- * Get all Nuxt layer server directories from Nitro config
18
- */
19
- function getLayerServerDirectories(nitro) {
20
- return nitro.options.graphql?.layerServerDirs || [];
21
- }
22
- /**
23
- * Get all Nuxt layer app directories from Nitro config
24
- */
25
- function getLayerAppDirectories(nitro) {
26
- return nitro.options.graphql?.layerAppDirs || [];
27
- }
28
- /**
29
- * Generate layer-aware ignore patterns for auto-generated files
30
- */
31
- function generateLayerIgnorePatterns() {
32
- return [];
33
- }
34
- function getImportId(p, lazy) {
35
- return (lazy ? "_lazy_" : "_") + hash(p).replace(/-/g, "").slice(0, 6);
36
- }
37
- const RELATIVE_RE = /^\.{1,2}\//;
38
- function relativeWithDot(from, to) {
39
- const rel = relative(from, to);
40
- return RELATIVE_RE.test(rel) ? rel : `./${rel}`;
41
- }
42
- async function scanGraphql(nitro) {
43
- const serverDirRelative = relative(nitro.options.rootDir, nitro.graphql.serverDir);
44
- const files = await scanDir(nitro, nitro.options.rootDir, serverDirRelative, "**/*.{graphql,gql}");
45
- const layerServerDirs = getLayerServerDirectories(nitro);
46
- const layerFiles = await Promise.all(layerServerDirs.map((layerServerDir) => scanDir(nitro, layerServerDir, "graphql", "**/*.{graphql,gql}"))).then((r) => r.flat());
47
- const allFiles = [...files, ...layerFiles];
48
- const seenPaths = /* @__PURE__ */ new Set();
49
- return allFiles.filter((file) => {
50
- if (seenPaths.has(file.fullPath)) return false;
51
- seenPaths.add(file.fullPath);
52
- return true;
53
- }).map((f) => f.fullPath);
54
- }
55
- async function scanResolvers(nitro) {
56
- const serverDirRelative = relative(nitro.options.rootDir, nitro.graphql.serverDir);
57
- const regularFiles = await scanDir(nitro, nitro.options.rootDir, serverDirRelative, "**/*.resolver.{ts,js}");
58
- const layerServerDirs = getLayerServerDirectories(nitro);
59
- const layerFiles = await Promise.all(layerServerDirs.map((layerServerDir) => scanDir(nitro, layerServerDir, "graphql", "**/*.resolver.{ts,js}"))).then((r) => r.flat());
60
- const allFiles = [...regularFiles, ...layerFiles];
61
- const seenPaths = /* @__PURE__ */ new Set();
62
- const files = allFiles.filter((file) => {
63
- if (seenPaths.has(file.fullPath)) return false;
64
- seenPaths.add(file.fullPath);
65
- return true;
66
- });
67
- const exportName = [];
68
- for (const file of files) {
69
- const fileContent = await readFile(file.fullPath, "utf-8");
70
- const parsed = await parseAsync(file.fullPath, fileContent);
71
- const exports = {
72
- imports: [],
73
- specifier: file.fullPath
74
- };
75
- for (const node of parsed.program.body) if (node.type === "ExportNamedDeclaration" && node.declaration && node.declaration.type === "VariableDeclaration") {
76
- for (const decl of node.declaration.declarations) if (decl.type === "VariableDeclarator" && decl.init && decl.id.type === "Identifier") {
77
- if (decl.init && decl.init.type === "CallExpression") {
78
- if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineResolver") exports.imports.push({
79
- name: decl.id.name,
80
- type: "resolver",
81
- as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
82
- });
83
- if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineQuery") exports.imports.push({
84
- name: decl.id.name,
85
- type: "query",
86
- as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
87
- });
88
- if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineMutation") exports.imports.push({
89
- name: decl.id.name,
90
- type: "mutation",
91
- as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
92
- });
93
- if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineType") exports.imports.push({
94
- name: decl.id.name,
95
- type: "type",
96
- as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
97
- });
98
- if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineSubscription") exports.imports.push({
99
- name: decl.id.name,
100
- type: "subscription",
101
- as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
102
- });
103
- if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineDirective") exports.imports.push({
104
- name: decl.id.name,
105
- type: "directive",
106
- as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
107
- });
108
- }
109
- }
110
- }
111
- if (exports.imports.length > 0) exportName.push(exports);
112
- }
113
- return exportName;
114
- }
115
- async function scanDirectives(nitro) {
116
- const serverDirRelative = relative(nitro.options.rootDir, nitro.graphql.serverDir);
117
- const regularFiles = await scanDir(nitro, nitro.options.rootDir, serverDirRelative, "**/*.directive.{ts,js}");
118
- const layerServerDirs = getLayerServerDirectories(nitro);
119
- const layerFiles = await Promise.all(layerServerDirs.map((layerServerDir) => scanDir(nitro, layerServerDir, "graphql", "**/*.directive.{ts,js}"))).then((r) => r.flat());
120
- const allFiles = [...regularFiles, ...layerFiles];
121
- const seenPaths = /* @__PURE__ */ new Set();
122
- const files = allFiles.filter((file) => {
123
- if (seenPaths.has(file.fullPath)) return false;
124
- seenPaths.add(file.fullPath);
125
- return true;
126
- });
127
- const exportName = [];
128
- for (const file of files) {
129
- const fileContent = await readFile(file.fullPath, "utf-8");
130
- const parsed = await parseAsync(file.fullPath, fileContent);
131
- const exports = {
132
- imports: [],
133
- specifier: file.fullPath
134
- };
135
- for (const node of parsed.program.body) if (node.type === "ExportNamedDeclaration" && node.declaration && node.declaration.type === "VariableDeclaration") {
136
- for (const decl of node.declaration.declarations) if (decl.type === "VariableDeclarator" && decl.init && decl.id.type === "Identifier") {
137
- if (decl.init && decl.init.type === "CallExpression") {
138
- if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineDirective") exports.imports.push({
139
- name: decl.id.name,
140
- type: "directive",
141
- as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
142
- });
143
- }
144
- }
145
- }
146
- if (exports.imports.length > 0) exportName.push(exports);
147
- }
148
- return exportName;
149
- }
150
- async function scanSchemas(nitro) {
151
- const serverDirRelative = relative(nitro.options.rootDir, nitro.graphql.serverDir);
152
- const files = await scanDir(nitro, nitro.options.rootDir, serverDirRelative, "**/*.graphql");
153
- const layerServerDirs = getLayerServerDirectories(nitro);
154
- const layerFiles = await Promise.all(layerServerDirs.map((layerServerDir) => scanDir(nitro, layerServerDir, "graphql", "**/*.graphql"))).then((r) => r.flat());
155
- const allFiles = [...files, ...layerFiles];
156
- const seenPaths = /* @__PURE__ */ new Set();
157
- return allFiles.filter((file) => {
158
- if (seenPaths.has(file.fullPath)) return false;
159
- seenPaths.add(file.fullPath);
160
- return true;
161
- }).map((f) => f.fullPath);
162
- }
163
- async function scanDocs(nitro) {
164
- const files = await scanDir(nitro, nitro.options.rootDir, nitro.graphql.dir.client, "**/*.graphql");
165
- const layerAppDirs = getLayerAppDirectories(nitro);
166
- const layerFiles = await Promise.all(layerAppDirs.map((layerAppDir) => scanDir(nitro, layerAppDir, "graphql", "**/*.graphql"))).then((r) => r.flat());
167
- const combinedFiles = [...files, ...layerFiles];
168
- const seenPaths = /* @__PURE__ */ new Set();
169
- const allFiles = combinedFiles.filter((file) => {
170
- if (seenPaths.has(file.fullPath)) return false;
171
- seenPaths.add(file.fullPath);
172
- return true;
173
- });
174
- const externalPatterns = (nitro.options.graphql?.externalServices || []).flatMap((service) => service.documents || []);
175
- return allFiles.filter((f) => !f.path.startsWith("external/")).filter((f) => {
176
- const relativePath = f.path;
177
- for (const pattern of externalPatterns) {
178
- const clientDirPattern = `${nitro.graphql.dir.client}/`;
179
- if (pattern.replace(/* @__PURE__ */ new RegExp(`^${clientDirPattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`), "").split("/")[0] === relativePath.split("/")[0]) return false;
180
- }
181
- return true;
182
- }).map((f) => f.fullPath);
183
- }
184
- /**
185
- * Scan documents for a specific external service
186
- */
187
- async function scanExternalServiceDocs(nitro, serviceName, patterns) {
188
- if (!patterns.length) return [];
189
- const files = [];
190
- for (const pattern of patterns) try {
191
- const serviceFiles = await glob(pattern, {
192
- cwd: nitro.options.rootDir,
193
- dot: true,
194
- ignore: nitro.options.ignore,
195
- absolute: true
196
- });
197
- files.push(...serviceFiles);
198
- } catch (error) {
199
- nitro.logger.warn(`[graphql:${serviceName}] Error scanning documents with pattern "${pattern}":`, error);
200
- }
201
- return files.filter((file, index, self) => self.indexOf(file) === index);
202
- }
203
- /**
204
- * Validate external GraphQL service configuration
205
- */
206
- function validateExternalServices(services) {
207
- const errors = [];
208
- const serviceNames = /* @__PURE__ */ new Set();
209
- for (const [index, service] of services.entries()) {
210
- const prefix = `externalServices[${index}]`;
211
- if (!service || typeof service !== "object") {
212
- errors.push(`${prefix} must be an object`);
213
- continue;
214
- }
215
- if (!("name" in service) || typeof service.name !== "string") errors.push(`${prefix}.name is required and must be a string`);
216
- else if (serviceNames.has(service.name)) errors.push(`${prefix}.name "${service.name}" must be unique`);
217
- else serviceNames.add(service.name);
218
- if (!("schema" in service) || !service.schema) errors.push(`${prefix}.schema is required`);
219
- if (!("endpoint" in service) || typeof service.endpoint !== "string") errors.push(`${prefix}.endpoint is required and must be a string`);
220
- else try {
221
- new URL(service.endpoint);
222
- } catch {
223
- errors.push(`${prefix}.endpoint "${service.endpoint}" must be a valid URL`);
224
- }
225
- if ("name" in service && service.name && typeof service.name === "string" && !/^[a-z]\w*$/i.test(service.name)) errors.push(`${prefix}.name "${service.name}" must be a valid identifier (letters, numbers, underscore, starting with letter)`);
226
- }
227
- return errors;
228
- }
229
- async function scanDir(nitro, dir, name, globPattern = GLOB_SCAN_PATTERN) {
230
- return (await glob(join(name, globPattern), {
231
- cwd: dir,
232
- dot: true,
233
- ignore: nitro.options.ignore,
234
- absolute: true
235
- }).catch((error) => {
236
- if (error?.code === "ENOTDIR") {
237
- nitro.logger.warn(`Ignoring \`${join(dir, name)}\`. It must be a directory.`);
238
- return [];
239
- }
240
- throw error;
241
- })).map((fullPath) => {
242
- return {
243
- fullPath,
244
- path: relative(join(dir, name), fullPath)
245
- };
246
- }).sort((a, b) => a.path.localeCompare(b.path));
247
- }
248
-
249
- //#endregion
250
- export { GLOB_SCAN_PATTERN, directiveParser, generateDirectiveSchema, generateDirectiveSchemas, generateLayerIgnorePatterns, getImportId, getLayerAppDirectories, getLayerDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives, scanDocs, scanExternalServiceDocs, scanGraphql, scanResolvers, scanSchemas, validateExternalServices };
@@ -1,7 +0,0 @@
1
- import { NitroGraphQLOptions } from "../types/index.js";
2
- import { GraphQLSchema } from "graphql";
3
-
4
- //#region src/utils/server-codegen.d.ts
5
- declare function generateTypes(selectFremework: string, schema: GraphQLSchema, config?: Partial<NitroGraphQLOptions>, outputPath?: string): Promise<string>;
6
- //#endregion
7
- export { generateTypes };
@@ -1,136 +0,0 @@
1
- import consola from "consola";
2
- import { defu as defu$1 } from "defu";
3
- import { parse } from "graphql";
4
- import { printSchemaWithDirectives } from "@graphql-tools/utils";
5
- import { codegen } from "@graphql-codegen/core";
6
- import * as typescriptPlugin from "@graphql-codegen/typescript";
7
- import { CurrencyResolver, DateTimeISOResolver, DateTimeResolver, JSONObjectResolver, JSONResolver, NonEmptyStringResolver, UUIDResolver } from "graphql-scalars";
8
- import * as typescriptResolversPlugin from "@graphql-codegen/typescript-resolvers";
9
-
10
- //#region src/utils/server-codegen.ts
11
- function pluginContent(_schema, _documents, _config, _info) {
12
- return {
13
- prepend: [
14
- "// THIS FILE IS GENERATED, DO NOT EDIT!",
15
- "/* eslint-disable eslint-comments/no-unlimited-disable */",
16
- "/* tslint:disable */",
17
- "/* eslint-disable */",
18
- "/* prettier-ignore */"
19
- ],
20
- content: ""
21
- };
22
- }
23
- async function generateTypes(selectFremework, schema, config = {}, outputPath) {
24
- const mergedConfig = defu$1({
25
- scalars: {
26
- DateTime: DateTimeResolver.extensions.codegenScalarType,
27
- DateTimeISO: DateTimeISOResolver.extensions.codegenScalarType,
28
- UUID: UUIDResolver.extensions.codegenScalarType,
29
- JSON: JSONResolver.extensions.codegenScalarType,
30
- JSONObject: JSONObjectResolver.extensions.codegenScalarType,
31
- NonEmptyString: NonEmptyStringResolver.extensions.codegenScalarType,
32
- Currency: CurrencyResolver.extensions.codegenScalarType,
33
- File: {
34
- input: "File",
35
- output: "File"
36
- }
37
- },
38
- defaultScalarType: "unknown",
39
- defaultMapper: `ResolverReturnType<{T}>`,
40
- contextType: "h3#H3Event",
41
- maybeValue: "T | null | undefined",
42
- inputMaybeValue: "T | undefined",
43
- declarationKind: "interface",
44
- enumsAsTypes: true,
45
- ...config.federation?.enabled && { federation: true }
46
- }, config.codegen?.server);
47
- return await codegen({
48
- filename: outputPath || "types.generated.ts",
49
- schema: parse(printSchemaWithDirectives(schema)),
50
- documents: [],
51
- config: mergedConfig,
52
- plugins: [
53
- { imports: {} },
54
- { pluginContent: {} },
55
- { typescript: {} },
56
- { typescriptResolvers: {} }
57
- ],
58
- pluginMap: {
59
- pluginContent: { plugin: pluginContent },
60
- imports: { plugin: () => {
61
- return {
62
- prepend: [
63
- `import schemas from '#graphql/schema'`,
64
- `import type { StandardSchemaV1 } from 'nitro-graphql'`,
65
- `
66
- export interface NPMConfig {
67
- framework: '${selectFremework || "graphql-yoga"}';
68
- }
69
-
70
- export type SchemaType = Partial<Record<Partial<keyof ResolversTypes>, StandardSchemaV1>>
71
-
72
- // Check if schemas is empty object, return never if so
73
- type SafeSchemaKeys<T> = T extends Record<PropertyKey, never>
74
- ? never
75
- : keyof T extends string | number | symbol
76
- ? keyof T extends never
77
- ? never
78
- : keyof T
79
- : never;
80
-
81
-
82
- type SchemaKeys = SafeSchemaKeys<typeof schemas>;
83
-
84
- type InferInput<T> = T extends StandardSchemaV1 ? StandardSchemaV1.InferInput<T> : unknown;
85
- type InferOutput<T> = T extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<T> : unknown;
86
-
87
- type InferInputFromSchema<T extends SchemaKeys> = InferInput<(typeof schemas)[T]>;
88
- type InferOutputFromSchema<T extends SchemaKeys> = InferOutput<(typeof schemas)[T]>;
89
-
90
- type Primitive =
91
- | null
92
- | undefined
93
- | string
94
- | number
95
- | boolean
96
- | symbol
97
- | bigint;
98
-
99
- type BuiltIns = Primitive | void | Date | RegExp;
100
-
101
-
102
- type ResolverReturnType<T> = T extends BuiltIns
103
- ? T
104
- : T extends (...args: any[]) => unknown
105
- ? T | undefined
106
- : T extends object
107
- ? T extends Array<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
108
- ? ItemType[] extends T // Test for arrays (non-tuples) specifically
109
- ? Array<ResolverReturnType<ItemType>>
110
- : ResolverReturnTypeObject<T> // Tuples behave properly
111
- : ResolverReturnTypeObject<T>
112
- : unknown;
113
-
114
- type ResolverReturnTypeObject<T extends object> =
115
- T extends { __typename?: infer TTypename }
116
- ? TTypename extends SchemaKeys
117
- ? InferOutputFromSchema<TTypename>
118
- : { [K in keyof T]: ResolverReturnType<T[K]> }
119
- : { [K in keyof T]: ResolverReturnType<T[K]> };
120
- `,
121
- ""
122
- ],
123
- content: ""
124
- };
125
- } },
126
- typescript: typescriptPlugin,
127
- typescriptResolvers: typescriptResolversPlugin
128
- }
129
- }).catch((e) => {
130
- consola.withTag("graphql").error("Error generating types:", e);
131
- return "";
132
- });
133
- }
134
-
135
- //#endregion
136
- export { generateTypes };
@@ -1,7 +0,0 @@
1
- import { Nitro } from "nitro/types";
2
-
3
- //#region src/utils/type-generation.d.ts
4
- declare function serverTypeGeneration(app: Nitro): Promise<void>;
5
- declare function clientTypeGeneration(nitro: Nitro): Promise<void>;
6
- //#endregion
7
- export { clientTypeGeneration, serverTypeGeneration };