nitro-graphql 0.0.4 → 0.0.6

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 (167) hide show
  1. package/README.md +1 -30
  2. package/dist/_virtual/rolldown_runtime.js +37 -0
  3. package/dist/ecosystem/nuxt.d.ts +7 -0
  4. package/dist/ecosystem/nuxt.js +30 -0
  5. package/dist/graphql/index.d.ts +5 -0
  6. package/dist/graphql/server.d.ts +5 -0
  7. package/dist/index.d.ts +9 -3
  8. package/dist/index.js +125 -135
  9. package/dist/node_modules/.pnpm/@graphql-codegen_add@3.2.3_graphql@16.11.0/node_modules/@graphql-codegen/add/esm/config.js +9 -0
  10. package/dist/node_modules/.pnpm/@graphql-codegen_add@3.2.3_graphql@16.11.0/node_modules/@graphql-codegen/add/esm/index.js +17 -0
  11. package/dist/node_modules/.pnpm/@graphql-codegen_import-types-preset@3.0.1_graphql@16.11.0/node_modules/@graphql-codegen/import-types-preset/esm/index.js +38 -0
  12. package/dist/node_modules/.pnpm/@graphql-codegen_plugin-helpers@3.1.2_graphql@16.11.0/node_modules/@graphql-codegen/plugin-helpers/esm/federation.js +22 -0
  13. package/dist/node_modules/.pnpm/@graphql-codegen_plugin-helpers@3.1.2_graphql@16.11.0/node_modules/@graphql-codegen/plugin-helpers/esm/getCachedDocumentNodeFromSchema.js +6 -0
  14. package/dist/node_modules/.pnpm/@graphql-codegen_plugin-helpers@3.1.2_graphql@16.11.0/node_modules/@graphql-codegen/plugin-helpers/esm/helpers.js +79 -0
  15. package/dist/node_modules/.pnpm/@graphql-codegen_plugin-helpers@3.1.2_graphql@16.11.0/node_modules/@graphql-codegen/plugin-helpers/esm/index.js +6 -0
  16. package/dist/node_modules/.pnpm/@graphql-codegen_plugin-helpers@3.1.2_graphql@16.11.0/node_modules/@graphql-codegen/plugin-helpers/esm/oldVisit.js +1 -0
  17. package/dist/node_modules/.pnpm/@graphql-codegen_plugin-helpers@3.1.2_graphql@16.11.0/node_modules/@graphql-codegen/plugin-helpers/esm/resolve-external-module-and-fn.js +7 -0
  18. package/dist/node_modules/.pnpm/@graphql-codegen_plugin-helpers@3.1.2_graphql@16.11.0/node_modules/@graphql-codegen/plugin-helpers/esm/utils.js +13 -0
  19. package/dist/node_modules/.pnpm/camel-case@4.1.2/node_modules/camel-case/dist/index.js +33 -0
  20. package/dist/node_modules/.pnpm/capital-case@1.0.4/node_modules/capital-case/dist/index.js +32 -0
  21. package/dist/node_modules/.pnpm/change-case-all@1.0.15/node_modules/change-case-all/dist/index.js +56 -0
  22. package/dist/node_modules/.pnpm/change-case@4.1.2/node_modules/change-case/dist/index.js +37 -0
  23. package/dist/node_modules/.pnpm/constant-case@3.0.4/node_modules/constant-case/dist/index.js +28 -0
  24. package/dist/node_modules/.pnpm/dot-case@3.0.4/node_modules/dot-case/dist/index.js +23 -0
  25. package/dist/node_modules/.pnpm/header-case@2.0.4/node_modules/header-case/dist/index.js +23 -0
  26. package/dist/node_modules/.pnpm/is-lower-case@2.0.2/node_modules/is-lower-case/dist/index.js +21 -0
  27. package/dist/node_modules/.pnpm/is-upper-case@2.0.2/node_modules/is-upper-case/dist/index.js +21 -0
  28. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_Hash.js +37 -0
  29. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_ListCache.js +37 -0
  30. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_Map.js +15 -0
  31. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_MapCache.js +37 -0
  32. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_Stack.js +34 -0
  33. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_Symbol.js +15 -0
  34. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_Uint8Array.js +15 -0
  35. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_apply.js +30 -0
  36. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_arrayLikeKeys.js +35 -0
  37. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_assignMergeValue.js +26 -0
  38. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_assignValue.js +32 -0
  39. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_assocIndexOf.js +26 -0
  40. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseAssignValue.js +31 -0
  41. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseCreate.js +34 -0
  42. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseFor.js +25 -0
  43. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseGetTag.js +30 -0
  44. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseIsArguments.js +26 -0
  45. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseIsNative.js +44 -0
  46. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseIsTypedArray.js +32 -0
  47. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseKeysIn.js +32 -0
  48. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseMerge.js +42 -0
  49. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseMergeDeep.js +75 -0
  50. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseRest.js +26 -0
  51. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseSetToString.js +31 -0
  52. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseTimes.js +25 -0
  53. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_baseUnary.js +23 -0
  54. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_cloneArrayBuffer.js +25 -0
  55. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_cloneBuffer.js +35 -0
  56. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_cloneTypedArray.js +25 -0
  57. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_copyArray.js +25 -0
  58. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_copyObject.js +37 -0
  59. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_coreJsData.js +15 -0
  60. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_createAssigner.js +37 -0
  61. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_createBaseFor.js +28 -0
  62. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_defineProperty.js +20 -0
  63. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_freeGlobal.js +13 -0
  64. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getMapData.js +25 -0
  65. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getNative.js +26 -0
  66. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getPrototype.js +15 -0
  67. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getRawTag.js +43 -0
  68. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_getValue.js +22 -0
  69. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashClear.js +24 -0
  70. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashDelete.js +26 -0
  71. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashGet.js +36 -0
  72. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashHas.js +30 -0
  73. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_hashSet.js +31 -0
  74. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_initCloneObject.js +25 -0
  75. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isIndex.js +28 -0
  76. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isIterateeCall.js +32 -0
  77. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isKeyable.js +22 -0
  78. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isMasked.js +28 -0
  79. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_isPrototype.js +24 -0
  80. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheClear.js +22 -0
  81. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheDelete.js +35 -0
  82. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheGet.js +26 -0
  83. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheHas.js +25 -0
  84. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_listCacheSet.js +31 -0
  85. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheClear.js +30 -0
  86. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheDelete.js +27 -0
  87. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheGet.js +25 -0
  88. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheHas.js +25 -0
  89. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_mapCacheSet.js +29 -0
  90. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_nativeCreate.js +14 -0
  91. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_nativeKeysIn.js +25 -0
  92. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_nodeUtil.js +29 -0
  93. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_objectToString.js +29 -0
  94. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_overArg.js +24 -0
  95. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_overRest.js +35 -0
  96. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_root.js +17 -0
  97. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_safeGet.js +24 -0
  98. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_setToString.js +23 -0
  99. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_shortOut.js +34 -0
  100. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_stackClear.js +24 -0
  101. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_stackDelete.js +25 -0
  102. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_stackGet.js +23 -0
  103. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_stackHas.js +23 -0
  104. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_stackSet.js +42 -0
  105. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/_toSource.js +33 -0
  106. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/constant.js +35 -0
  107. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/eq.js +46 -0
  108. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/identity.js +30 -0
  109. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isArguments.js +43 -0
  110. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isArray.js +35 -0
  111. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isArrayLike.js +42 -0
  112. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isArrayLikeObject.js +42 -0
  113. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isBuffer.js +41 -0
  114. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isFunction.js +38 -0
  115. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isLength.js +42 -0
  116. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isObject.js +40 -0
  117. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isObjectLike.js +38 -0
  118. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isPlainObject.js +60 -0
  119. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/isTypedArray.js +34 -0
  120. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/keysIn.js +41 -0
  121. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/merge.js +48 -0
  122. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/stubFalse.js +27 -0
  123. package/dist/node_modules/.pnpm/lodash@4.17.21/node_modules/lodash/toPlainObject.js +41 -0
  124. package/dist/node_modules/.pnpm/lower-case-first@2.0.2/node_modules/lower-case-first/dist/index.js +21 -0
  125. package/dist/node_modules/.pnpm/lower-case@2.0.2/node_modules/lower-case/dist/index.js +64 -0
  126. package/dist/node_modules/.pnpm/no-case@3.0.4/node_modules/no-case/dist/index.js +41 -0
  127. package/dist/node_modules/.pnpm/param-case@3.0.4/node_modules/param-case/dist/index.js +23 -0
  128. package/dist/node_modules/.pnpm/pascal-case@3.1.2/node_modules/pascal-case/dist/index.js +37 -0
  129. package/dist/node_modules/.pnpm/path-case@3.0.4/node_modules/path-case/dist/index.js +23 -0
  130. package/dist/node_modules/.pnpm/sentence-case@3.0.4/node_modules/sentence-case/dist/index.js +34 -0
  131. package/dist/node_modules/.pnpm/snake-case@3.0.4/node_modules/snake-case/dist/index.js +23 -0
  132. package/dist/node_modules/.pnpm/sponge-case@1.0.1/node_modules/sponge-case/dist/index.js +20 -0
  133. package/dist/node_modules/.pnpm/swap-case@2.0.2/node_modules/swap-case/dist/index.js +23 -0
  134. package/dist/node_modules/.pnpm/title-case@3.0.3/node_modules/title-case/dist/index.js +35 -0
  135. package/dist/node_modules/.pnpm/tslib@2.8.1/node_modules/tslib/tslib.es6.js +547 -0
  136. package/dist/node_modules/.pnpm/upper-case-first@2.0.2/node_modules/upper-case-first/dist/index.js +21 -0
  137. package/dist/node_modules/.pnpm/upper-case@2.0.2/node_modules/upper-case/dist/index.js +56 -0
  138. package/dist/rollup.js +87 -0
  139. package/dist/routes/apollo-server.d.ts +6 -0
  140. package/dist/routes/apollo-server.js +39 -0
  141. package/dist/routes/graphql-yoga.d.ts +6 -0
  142. package/dist/routes/graphql-yoga.js +56 -0
  143. package/dist/routes/health.d.ts +10 -0
  144. package/dist/routes/health.js +44 -0
  145. package/dist/types/index.d.ts +67 -0
  146. package/dist/types/standard-schema.d.ts +59 -0
  147. package/dist/utils/apollo.js +62 -0
  148. package/dist/{client-codegen.js → utils/client-codegen.js} +52 -16
  149. package/dist/utils/define.d.ts +17 -0
  150. package/dist/utils/define.js +25 -0
  151. package/dist/utils/index.d.ts +15 -0
  152. package/dist/utils/index.js +107 -0
  153. package/dist/{codegen.js → utils/server-codegen.js} +55 -35
  154. package/dist/utils/server-type-generation.js +52 -0
  155. package/package.json +37 -13
  156. package/dist/client-watcher.d.ts +0 -7
  157. package/dist/client-watcher.js +0 -100
  158. package/dist/codegen.d.ts +0 -13
  159. package/dist/context.d.ts +0 -10
  160. package/dist/dev.js +0 -95
  161. package/dist/prerender.js +0 -279
  162. package/dist/scanner.js +0 -118
  163. package/dist/types.d.ts +0 -46
  164. package/dist/utils.d.ts +0 -13
  165. package/dist/utils.js +0 -23
  166. /package/dist/{context.js → graphql/index.js} +0 -0
  167. /package/dist/{types.js → graphql/server.js} +0 -0
@@ -0,0 +1,107 @@
1
+ import { join, relative } from "pathe";
2
+ import { readFile } from "node:fs/promises";
3
+ import { hash } from "ohash";
4
+ import { parseAsync } from "oxc-parser";
5
+ import { glob } from "tinyglobby";
6
+
7
+ //#region src/utils/index.ts
8
+ const GLOB_SCAN_PATTERN = "**/*.{graphql,gql,js,mjs,cjs,ts,mts,cts,tsx,jsx}";
9
+ function getImportId(p, lazy) {
10
+ return (lazy ? "_lazy_" : "_") + hash(p).replace(/-/g, "").slice(0, 6);
11
+ }
12
+ const RELATIVE_RE = /^\.{1,2}\//;
13
+ function relativeWithDot(from, to) {
14
+ const rel = relative(from, to);
15
+ return RELATIVE_RE.test(rel) ? rel : `./${rel}`;
16
+ }
17
+ async function scanGraphql(nitro) {
18
+ const files = await scanFiles(nitro, "graphql");
19
+ return files.map((f) => f.fullPath);
20
+ }
21
+ async function scanResolvers(nitro) {
22
+ const files = await scanFiles(nitro, "graphql", "**/*.resolver.{ts,js}");
23
+ const exportName = [];
24
+ for (const file of files) {
25
+ const fileContent = await readFile(file.fullPath, "utf-8");
26
+ const parsed = await parseAsync(file.fullPath, fileContent);
27
+ const exports = {
28
+ imports: [],
29
+ specifier: file.fullPath
30
+ };
31
+ for (const node of parsed.program.body) if (node.type === "ExportNamedDeclaration" && node.declaration && node.declaration.type === "VariableDeclaration") {
32
+ for (const decl of node.declaration.declarations) if (decl.type === "VariableDeclarator" && decl.init && decl.id.type === "Identifier") {
33
+ if (decl.init && decl.init.type === "CallExpression") {
34
+ if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineResolver") exports.imports.push({
35
+ name: decl.id.name,
36
+ type: "resolver",
37
+ as: `_${hash(decl.id.name + file.path).replace(/-/g, "").slice(0, 6)}`
38
+ });
39
+ if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineQuery") exports.imports.push({
40
+ name: decl.id.name,
41
+ type: "query",
42
+ as: `_${hash(decl.id.name + file.path).replace(/-/g, "").slice(0, 6)}`
43
+ });
44
+ if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineMutation") exports.imports.push({
45
+ name: decl.id.name,
46
+ type: "mutation",
47
+ as: `_${hash(decl.id.name + file.path).replace(/-/g, "").slice(0, 6)}`
48
+ });
49
+ if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineType") exports.imports.push({
50
+ name: decl.id.name,
51
+ type: "type",
52
+ as: `_${hash(decl.id.name + file.path).replace(/-/g, "").slice(0, 6)}`
53
+ });
54
+ if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineSubscription") exports.imports.push({
55
+ name: decl.id.name,
56
+ type: "subscription",
57
+ as: `_${hash(decl.id.name + file.path).replace(/-/g, "").slice(0, 6)}`
58
+ });
59
+ }
60
+ }
61
+ }
62
+ if (exports.imports.length > 0) exportName.push(exports);
63
+ }
64
+ return exportName;
65
+ }
66
+ async function scanDirectives(nitro) {
67
+ const files = await scanFiles(nitro, "graphql", "**/*.directive.{ts,js}");
68
+ return files.map((f) => f.fullPath);
69
+ }
70
+ async function scanTypeDefs(nitro) {
71
+ const files = await scanFiles(nitro, "graphql", "**/*.typedef.{ts,js}");
72
+ return files.map((f) => f.fullPath);
73
+ }
74
+ async function scanDefs(nitro) {
75
+ const files = await scanFiles(nitro, "graphql", "**/*.graphql");
76
+ return files.map((f) => f.fullPath);
77
+ }
78
+ async function scanFiles(nitro, name, globPattern = GLOB_SCAN_PATTERN) {
79
+ const files = await Promise.all(nitro.options.scanDirs.map((dir) => scanDir(nitro, dir, name, globPattern))).then((r) => r.flat());
80
+ return files;
81
+ }
82
+ async function scanDir(nitro, dir, name, globPattern = GLOB_SCAN_PATTERN) {
83
+ const fileNames = await glob(join(name, globPattern), {
84
+ cwd: dir,
85
+ dot: true,
86
+ ignore: nitro.options.ignore,
87
+ absolute: true
88
+ }).catch((error) => {
89
+ if (error?.code === "ENOTDIR") {
90
+ nitro.logger.warn(`Ignoring \`${join(dir, name)}\`. It must be a directory.`);
91
+ return [];
92
+ }
93
+ throw error;
94
+ });
95
+ return fileNames.map((fullPath) => {
96
+ return {
97
+ fullPath,
98
+ path: relative(join(dir, name), fullPath)
99
+ };
100
+ }).sort((a, b) => a.path.localeCompare(b.path));
101
+ }
102
+ function unique(arr) {
103
+ return [...new Set(arr)];
104
+ }
105
+
106
+ //#endregion
107
+ export { GLOB_SCAN_PATTERN, getImportId, relativeWithDot, scanDefs, scanDirectives, scanGraphql, scanResolvers, scanTypeDefs, unique };
@@ -1,12 +1,13 @@
1
+ import { defu as defu$1 } from "defu";
2
+ import { parse } from "graphql";
3
+ import { printSchemaWithDirectives } from "@graphql-tools/utils";
4
+ import consola from "consola";
1
5
  import { codegen } from "@graphql-codegen/core";
2
6
  import * as typescriptPlugin from "@graphql-codegen/typescript";
3
7
  import * as typescriptResolversPlugin from "@graphql-codegen/typescript-resolvers";
4
- import { printSchemaWithDirectives } from "@graphql-tools/utils";
5
- import { defu } from "defu";
6
- import { parse } from "graphql";
7
8
  import { CurrencyResolver, DateTimeResolver, JSONResolver, NonEmptyStringResolver, UUIDResolver } from "graphql-scalars";
8
9
 
9
- //#region src/codegen.ts
10
+ //#region src/utils/server-codegen.ts
10
11
  function pluginContent(_schema, _documents, _config, _info) {
11
12
  return {
12
13
  prepend: [
@@ -44,15 +45,12 @@ async function generateTypes(schema, config = {}, outputPath) {
44
45
  },
45
46
  defaultScalarType: "unknown",
46
47
  defaultMapper: `ResolverReturnType<{T}>`,
47
- contextType: "./context#GraphQLContext",
48
+ contextType: "h3#H3Event",
48
49
  maybeValue: "T | null | undefined",
49
50
  inputMaybeValue: "T | undefined",
50
- enumsAsTypes: true,
51
- useTypeImports: true,
52
- strictScalars: true,
53
- emitLegacyCommonJSImports: false
51
+ declarationKind: "interface"
54
52
  };
55
- const mergedConfig = defu(config, defaultConfig);
53
+ const mergedConfig = defu$1(config, defaultConfig);
56
54
  const output = await codegen({
57
55
  filename: outputPath || "types.generated.ts",
58
56
  schema: parse(printSchemaWithDirectives(schema)),
@@ -66,40 +64,62 @@ async function generateTypes(schema, config = {}, outputPath) {
66
64
  ],
67
65
  pluginMap: {
68
66
  pluginContent: { plugin: pluginContent },
69
- imports: { plugin: () => ({
70
- prepend: [`type Primitive =
71
- | null
72
- | undefined
73
- | string
74
- | number
75
- | boolean
76
- | symbol
77
- | bigint;
67
+ imports: { plugin: () => {
68
+ return {
69
+ prepend: [
70
+ `import schemas from '#graphql/schemas'`,
71
+ `import type { StandardSchemaV1 } from 'nitro-graphql'`,
72
+ `
73
+ export type SchemaType = Partial<Record<Partial<keyof ResolversTypes>, StandardSchemaV1>>
74
+ type SchemaKeys = keyof typeof schemas;
75
+
76
+ type InferInput<T> = T extends StandardSchemaV1 ? StandardSchemaV1.InferInput<T> : unknown;
77
+ type InferOutput<T> = T extends StandardSchemaV1 ? StandardSchemaV1.InferOutput<T> : unknown;
78
+
79
+ type InferInputFromSchema<T extends SchemaKeys> = InferInput<(typeof schemas)[T]>;
80
+ type InferOutputFromSchema<T extends SchemaKeys> = InferOutput<(typeof schemas)[T]>;
81
+
82
+ type Primitive =
83
+ | null
84
+ | undefined
85
+ | string
86
+ | number
87
+ | boolean
88
+ | symbol
89
+ | bigint;
78
90
 
79
91
  type BuiltIns = Primitive | void | Date | RegExp;
80
92
 
93
+
81
94
  type ResolverReturnType<T> = T extends BuiltIns
82
- ? T
83
- : T extends (...args: any[]) => unknown
84
- ? T | undefined
85
- : T extends object
86
- ? T extends Array<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
87
- ? ItemType[] extends T // Test for arrays (non-tuples) specifically
88
- ? Array<ResolverReturnType<ItemType>>
89
- : ResolverReturnTypeObject<T> // Tuples behave properly
90
- : ResolverReturnTypeObject<T>
91
- : unknown;
95
+ ? T
96
+ : T extends (...args: any[]) => unknown
97
+ ? T | undefined
98
+ : T extends object
99
+ ? T extends Array<infer ItemType> // Test for arrays/tuples, per https://github.com/microsoft/TypeScript/issues/35156
100
+ ? ItemType[] extends T // Test for arrays (non-tuples) specifically
101
+ ? Array<ResolverReturnType<ItemType>>
102
+ : ResolverReturnTypeObject<T> // Tuples behave properly
103
+ : ResolverReturnTypeObject<T>
104
+ : unknown;
92
105
 
93
- type ResolverReturnTypeObject<T extends object> = {
94
- [K in keyof T]: ResolverReturnType<T[K]>
95
- };`, ""],
96
- content: ""
97
- }) },
106
+ type ResolverReturnTypeObject<T extends object> =
107
+ T extends { __typename?: infer TTypename }
108
+ ? TTypename extends SchemaKeys
109
+ ? InferOutputFromSchema<TTypename>
110
+ : { [K in keyof T]: ResolverReturnType<T[K]> }
111
+ : { [K in keyof T]: ResolverReturnType<T[K]> };
112
+ `,
113
+ ""
114
+ ],
115
+ content: ""
116
+ };
117
+ } },
98
118
  typescript: typescriptPlugin,
99
119
  typescriptResolvers: typescriptResolversPlugin
100
120
  }
101
121
  }).catch((e) => {
102
- console.warn("[nitro-graphql] Code generation error:", e);
122
+ consola.withTag("graphql").error("Error generating types:", e);
103
123
  return "";
104
124
  });
105
125
  return output;
@@ -0,0 +1,52 @@
1
+ import { generateClientTypes, loadGraphQLDocuments } from "./client-codegen.js";
2
+ import { generateTypes } from "./server-codegen.js";
3
+ import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
4
+ import { dirname, join, resolve } from "pathe";
5
+ import { buildASTSchema, buildSchema } from "graphql";
6
+ import { loadFilesSync } from "@graphql-tools/load-files";
7
+ import { mergeTypeDefs } from "@graphql-tools/merge";
8
+ import { printSchemaWithDirectives } from "@graphql-tools/utils";
9
+ import consola from "consola";
10
+
11
+ //#region src/utils/server-type-generation.ts
12
+ async function serverTypeGeneration(app) {
13
+ try {
14
+ const defs = app.scanDefs || [];
15
+ const loadDefs = loadFilesSync(defs);
16
+ const mergedDefs = mergeTypeDefs(loadDefs);
17
+ const schema = buildASTSchema(mergedDefs, {
18
+ assumeValidSDL: true,
19
+ assumeValid: true
20
+ });
21
+ const data = await generateTypes(schema, app.options.graphql?.codegen?.server ?? {});
22
+ const printSchema = printSchemaWithDirectives(schema);
23
+ const schemaPath = resolve(app.graphql.buildDir, "schema.graphql");
24
+ mkdirSync(dirname(schemaPath), { recursive: true });
25
+ writeFileSync(schemaPath, printSchema, "utf-8");
26
+ const serverTypesPath = resolve(app.options.buildDir, "types", "nitro-graphql-server.d.ts");
27
+ mkdirSync(dirname(serverTypesPath), { recursive: true });
28
+ writeFileSync(serverTypesPath, data, "utf-8");
29
+ } catch (error) {
30
+ consola.error("Server schema generation error:", error);
31
+ }
32
+ }
33
+ async function clientTypeGeneration(app, path) {
34
+ try {
35
+ const root = app.graphql.watchDirs.find((dir) => path.startsWith(dir)) || path;
36
+ if (!root) return;
37
+ const docs = await loadGraphQLDocuments(root);
38
+ const graphqlString = readFileSync(join(app.graphql.buildDir, "schema.graphql"), "utf-8");
39
+ const schema = buildSchema(graphqlString);
40
+ const types = await generateClientTypes(schema, docs);
41
+ if (types === false) return;
42
+ const clientTypesPath = resolve(app.options.buildDir, "types", "nitro-graphql-client.d.ts");
43
+ const sdkTypesPath = resolve(app.graphql.clientDir, "sdk.ts");
44
+ writeFileSync(clientTypesPath, types.types, "utf-8");
45
+ writeFileSync(sdkTypesPath, types.sdk, "utf-8");
46
+ } catch (error) {
47
+ consola.error("Client schema generation error:", error);
48
+ }
49
+ }
50
+
51
+ //#endregion
52
+ export { clientTypeGeneration, serverTypeGeneration };
package/package.json CHANGED
@@ -1,13 +1,19 @@
1
1
  {
2
2
  "name": "nitro-graphql",
3
3
  "type": "module",
4
- "version": "0.0.4",
4
+ "version": "0.0.6",
5
5
  "description": "GraphQL integration for Nitro",
6
6
  "license": "MIT",
7
7
  "sideEffects": false,
8
+ "imports": {
9
+ "#graphql/server": {
10
+ "types": "./dist/graphql/server.d.ts",
11
+ "import": "./dist/graphql/server.js"
12
+ }
13
+ },
8
14
  "exports": {
9
15
  ".": {
10
- "types": "./dist/types.d.ts",
16
+ "types": "./dist/index.d.ts",
11
17
  "import": "./dist/index.js"
12
18
  },
13
19
  "./codegen": {
@@ -27,25 +33,36 @@
27
33
  "import": "./dist/context.js"
28
34
  },
29
35
  "./utils": {
30
- "types": "./dist/utils.d.ts",
31
- "import": "./dist/utils.js"
36
+ "types": "./dist/utils/index.d.ts",
37
+ "import": "./dist/utils/index.js"
38
+ },
39
+ "./utils/define": {
40
+ "types": "./dist/utils/define.d.ts",
41
+ "import": "./dist/utils/define.js"
42
+ },
43
+ "./internal": {
44
+ "types": "./dist/internal/index.d.ts",
45
+ "import": "./dist/internal/index.js"
32
46
  },
33
- "./types": {
34
- "types": "./dist/types.d.ts",
35
- "import": "./dist/types.mjs"
47
+ "./nuxt": {
48
+ "types": "./dist/ecosystem/nuxt.d.ts",
49
+ "import": "./dist/ecosystem/nuxt.js"
36
50
  }
37
51
  },
38
52
  "main": "./dist/index.js",
39
- "types": "./dist/types.d.ts",
53
+ "module": "./dist/index.js",
54
+ "types": "./dist/index.d.ts",
40
55
  "files": [
41
56
  "dist"
42
57
  ],
43
58
  "peerDependencies": {
59
+ "graphql": "^16.11.0",
44
60
  "nitropack": "^2.11.13"
45
61
  },
46
62
  "dependencies": {
63
+ "@apollo/utils.withrequired": "^3.0.0",
64
+ "@as-integrations/h3": "^2.0.0",
47
65
  "@graphql-codegen/core": "^4.0.2",
48
- "@graphql-codegen/plugin-helpers": "^5.1.1",
49
66
  "@graphql-codegen/typescript": "^4.1.6",
50
67
  "@graphql-codegen/typescript-generic-sdk": "^4.0.2",
51
68
  "@graphql-codegen/typescript-operations": "^4.6.1",
@@ -59,19 +76,26 @@
59
76
  "chokidar": "^4.0.3",
60
77
  "consola": "^3.4.2",
61
78
  "defu": "^6.1.4",
62
- "graphql": "^16.11.0",
63
79
  "graphql-scalars": "^1.24.2",
64
- "graphql-yoga": "^5.14.0",
80
+ "knitwork": "^1.2.0",
81
+ "ohash": "^2.0.11",
82
+ "oxc-parser": "^0.77.0",
65
83
  "pathe": "^2.0.3",
66
- "perfect-debounce": "^1.0.0",
67
84
  "tinyglobby": "^0.2.14"
68
85
  },
69
86
  "devDependencies": {
70
87
  "@antfu/eslint-config": "^4.16.2",
88
+ "@apollo/server": "^4.12.2",
89
+ "@graphql-codegen/import-types-preset": "^3.0.1",
90
+ "@nuxt/kit": "4.0.0-rc.0",
91
+ "@nuxt/schema": "4.0.0-rc.0",
71
92
  "@types/node": "^20.19.7",
72
93
  "bumpp": "^10.2.0",
73
94
  "changelogen": "^0.6.2",
74
- "eslint": "^9.30.1",
95
+ "crossws": "0.3.5",
96
+ "eslint": "^9.31.0",
97
+ "graphql": "^16.11.0",
98
+ "graphql-yoga": "^5.15.1",
75
99
  "h3": "^1.15.3",
76
100
  "nitropack": "^2.11.13",
77
101
  "tsdown": "^0.12.9",
@@ -1,7 +0,0 @@
1
- import { NitroGraphQLOptions } from "./types.js";
2
- import { Nitro } from "nitropack/types";
3
-
4
- //#region src/client-watcher.d.ts
5
- declare function setupClientWatcher(nitro: Nitro, options: NitroGraphQLOptions): Promise<void>;
6
- //#endregion
7
- export { setupClientWatcher };
@@ -1,100 +0,0 @@
1
- import { scanGraphQLFiles } from "./scanner.js";
2
- import { mkdir, writeFile } from "node:fs/promises";
3
- import { mergeTypeDefs } from "@graphql-tools/merge";
4
- import { makeExecutableSchema } from "@graphql-tools/schema";
5
- import { consola } from "consola";
6
- import { join } from "pathe";
7
- import { debounce } from "perfect-debounce";
8
-
9
- //#region src/client-watcher.ts
10
- const logger = consola.withTag("graphql");
11
- async function regenerateClientTypes(nitro, options) {
12
- try {
13
- if (!options.client?.enabled) return;
14
- const scanResult = await scanGraphQLFiles(nitro);
15
- if (scanResult.typeDefs.length === 0) {
16
- logger.warn("⚠️ No server schema found for client type generation");
17
- return;
18
- }
19
- const mergedTypeDefs = mergeTypeDefs(scanResult.typeDefs);
20
- const schema = makeExecutableSchema({
21
- typeDefs: mergedTypeDefs,
22
- resolvers: {}
23
- });
24
- const getClientPatterns = () => {
25
- if (options.client?.watchPatterns) return options.client.watchPatterns;
26
- const basePatterns = [
27
- join(nitro.options.srcDir, "**/*.graphql"),
28
- join(nitro.options.srcDir, "**/*.gql"),
29
- `!${join(nitro.options.srcDir, "graphql/**/*")}`
30
- ];
31
- if (nitro.options.framework?.name === "nuxt") if (options.client?.nuxtPatterns) basePatterns.unshift(...options.client.nuxtPatterns);
32
- else basePatterns.unshift(join(nitro.options.srcDir, "app/graphql/**/*.graphql"), join(nitro.options.srcDir, "app/graphql/**/*.gql"));
33
- return basePatterns;
34
- };
35
- const clientPatterns = getClientPatterns();
36
- const { generateClientTypes } = await import("./client-codegen.js");
37
- const generatedTypes = await generateClientTypes(schema, clientPatterns, options.client.config, options.client.outputPath);
38
- if (generatedTypes) {
39
- const outputPath = options.client.outputPath || join(nitro.options.buildDir, "types", "graphql-client.generated.ts");
40
- const typesDir = join(nitro.options.buildDir, "types");
41
- await mkdir(typesDir, { recursive: true });
42
- await writeFile(outputPath, generatedTypes);
43
- logger.success("✨ Client types updated");
44
- }
45
- } catch (error) {
46
- const errorMessage = error instanceof Error ? error.message : String(error);
47
- logger.error("❌ Client type generation failed:", errorMessage);
48
- }
49
- }
50
- async function setupClientWatcher(nitro, options) {
51
- if (!options.client?.enabled) {
52
- logger.info("🚫 Client type generation disabled");
53
- return;
54
- }
55
- const getClientPatterns = () => {
56
- if (options.client?.watchPatterns) return options.client.watchPatterns;
57
- const basePatterns = [join(nitro.options.srcDir, "**/*.graphql"), join(nitro.options.srcDir, "**/*.gql")];
58
- if (nitro.options.framework?.name === "nuxt") if (options.client?.nuxtPatterns) basePatterns.unshift(...options.client.nuxtPatterns);
59
- else basePatterns.unshift(join(nitro.options.srcDir, "app/graphql/**/*.graphql"), join(nitro.options.srcDir, "app/graphql/**/*.gql"));
60
- return basePatterns;
61
- };
62
- const clientPatterns = getClientPatterns();
63
- const generateClientTypesDebounced = debounce(async () => {
64
- await regenerateClientTypes(nitro, options);
65
- }, 300);
66
- const { watch } = await import("chokidar");
67
- const watcher = watch(clientPatterns, {
68
- persistent: true,
69
- ignoreInitial: true,
70
- ignored: [/(^|[/\\])\.\.\./, join(nitro.options.srcDir, "graphql/**/*")],
71
- followSymlinks: false,
72
- usePolling: true,
73
- interval: 500,
74
- binaryInterval: 500
75
- });
76
- watcher.on("add", (path) => {
77
- logger.info(`📁 Client file added: ${path}`);
78
- generateClientTypesDebounced();
79
- });
80
- watcher.on("change", (path) => {
81
- logger.info(`📝 Client file changed: ${path}`);
82
- generateClientTypesDebounced();
83
- });
84
- watcher.on("unlink", (path) => {
85
- logger.info(`🗑️ Client file removed: ${path}`);
86
- generateClientTypesDebounced();
87
- });
88
- watcher.on("error", (error) => {
89
- logger.error("❌ Client watcher error:", error);
90
- });
91
- nitro.hooks.hook("close", () => {
92
- logger.info("🔒 Closing client watcher");
93
- watcher.close();
94
- });
95
- await regenerateClientTypes(nitro, options);
96
- logger.success("✅ Client watcher ready");
97
- }
98
-
99
- //#endregion
100
- export { setupClientWatcher };
package/dist/codegen.d.ts DELETED
@@ -1,13 +0,0 @@
1
- import { GraphQLSchema } from "graphql";
2
-
3
- //#region src/codegen.d.ts
4
- interface CodegenServerConfig {
5
- contextType?: string;
6
- scalars?: Record<string, any>;
7
- defaultMapper?: string;
8
- mapperTypeSuffix?: string;
9
- [key: string]: any;
10
- }
11
- declare function generateTypes(schema: GraphQLSchema, config?: CodegenServerConfig, outputPath?: string): Promise<string>;
12
- //#endregion
13
- export { CodegenServerConfig, generateTypes };
package/dist/context.d.ts DELETED
@@ -1,10 +0,0 @@
1
- import { YogaInitialContext } from "graphql-yoga";
2
- import { H3Event } from "h3";
3
-
4
- //#region src/context.d.ts
5
- interface GraphQLContext extends YogaInitialContext {
6
- event: H3Event;
7
- storage: any;
8
- }
9
- //#endregion
10
- export { GraphQLContext };
package/dist/dev.js DELETED
@@ -1,95 +0,0 @@
1
- import { scanGraphQLFiles } from "./scanner.js";
2
- import { setupClientWatcher } from "./client-watcher.js";
3
- import { generateTypes } from "./codegen.js";
4
- import { mkdir, writeFile } from "node:fs/promises";
5
- import { mergeTypeDefs } from "@graphql-tools/merge";
6
- import { makeExecutableSchema } from "@graphql-tools/schema";
7
- import { consola } from "consola";
8
- import { join } from "pathe";
9
- import { debounce } from "perfect-debounce";
10
- import { existsSync } from "node:fs";
11
- import { watch } from "chokidar";
12
-
13
- //#region src/dev.ts
14
- const logger = consola.withTag("graphql");
15
- async function regenerateGraphQLTypes(nitro) {
16
- try {
17
- const scanResult = await scanGraphQLFiles(nitro);
18
- if (scanResult.typeDefs.length === 0) {
19
- logger.warn("⚠️ No schema files found");
20
- return;
21
- }
22
- const mergedTypeDefs = mergeTypeDefs(scanResult.typeDefs);
23
- const schema = makeExecutableSchema({
24
- typeDefs: mergedTypeDefs,
25
- resolvers: {}
26
- });
27
- const generatedTypes = await generateTypes(schema);
28
- const outputPath = join(nitro.options.buildDir, "types", "graphql-types.generated.ts");
29
- const typesDir = join(nitro.options.buildDir, "types");
30
- await mkdir(typesDir, { recursive: true });
31
- await writeFile(outputPath, generatedTypes);
32
- logger.success("✨ Types regenerated");
33
- } catch (error) {
34
- const errorMessage = error instanceof Error ? error.message : String(error);
35
- logger.error("❌ Type generation failed:", errorMessage);
36
- }
37
- }
38
- function devmode(nitro, options) {
39
- if (nitro.options.dev) {
40
- const generateTypesDebounced = debounce(async () => {
41
- await regenerateGraphQLTypes(nitro);
42
- }, 100);
43
- const graphqlDir = join(nitro.options.srcDir, "graphql");
44
- logger.info("🔍 Setting up independent GraphQL watcher for directory:", graphqlDir);
45
- try {
46
- if (!existsSync(graphqlDir)) {
47
- logger.warn(`⚠️ GraphQL directory not found: ${graphqlDir}`);
48
- return;
49
- }
50
- } catch (error) {
51
- logger.warn("⚠️ Could not check GraphQL directory:", error);
52
- return;
53
- }
54
- const graphqlWatcher = watch(graphqlDir, {
55
- ignoreInitial: true,
56
- persistent: true,
57
- usePolling: true,
58
- interval: 500,
59
- depth: 10
60
- });
61
- graphqlWatcher.on("add", (path) => {
62
- logger.info("📁 GraphQL file added:", path);
63
- generateTypesDebounced();
64
- });
65
- graphqlWatcher.on("change", (path) => {
66
- logger.info("📝 GraphQL file changed:", path);
67
- generateTypesDebounced();
68
- });
69
- graphqlWatcher.on("unlink", (path) => {
70
- logger.info("🗑️ GraphQL file deleted:", path);
71
- generateTypesDebounced();
72
- });
73
- graphqlWatcher.on("error", (error) => {
74
- logger.error("❌ GraphQL watcher error:", error);
75
- });
76
- graphqlWatcher.on("raw", (event, path) => {
77
- if (event === "change" && path && path.endsWith("graphql")) {
78
- logger.info("📝 GraphQL directory change detected, regenerating types");
79
- generateTypesDebounced();
80
- }
81
- });
82
- graphqlWatcher.on("ready", () => {
83
- logger.success("✅ GraphQL file watcher ready");
84
- });
85
- nitro.hooks.hook("close", async () => {
86
- await graphqlWatcher.close();
87
- logger.info("🔒 GraphQL watcher closed");
88
- });
89
- setupClientWatcher(nitro, options);
90
- logger.success("✅ Independent GraphQL watcher set up");
91
- }
92
- }
93
-
94
- //#endregion
95
- export { devmode };