nitro-graphql 2.0.0-beta.70 → 2.0.0-beta.73

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 (228) hide show
  1. package/README.md +1 -1
  2. package/dist/cli/adapter.d.mts +0 -3
  3. package/dist/cli/adapter.mjs +7 -32
  4. package/dist/cli/commands/build.d.mts +2 -2
  5. package/dist/cli/commands/build.mjs +1 -3
  6. package/dist/cli/commands/dev.d.mts +2 -2
  7. package/dist/cli/commands/dev.mjs +1 -3
  8. package/dist/cli/commands/generate.mjs +10 -10
  9. package/dist/cli/commands/init.mjs +4 -4
  10. package/dist/cli/commands/validate.mjs +1 -3
  11. package/dist/cli/completions.mjs +1 -3
  12. package/dist/cli/config.d.mts +4 -5
  13. package/dist/cli/config.mjs +4 -3
  14. package/dist/cli/index.d.mts +3 -6
  15. package/dist/cli/index.mjs +2 -4
  16. package/dist/cli/server/debug-handler.mjs +2 -5
  17. package/dist/cli/server/dev-server.mjs +1 -3
  18. package/dist/cli/server/graphql-handler.mjs +3 -4
  19. package/dist/cli/server/health-handler.mjs +1 -2
  20. package/dist/cli/server/loader.mjs +1 -3
  21. package/dist/cli/server/sandbox-handler.mjs +1 -3
  22. package/dist/cli/server/watcher.mjs +2 -4
  23. package/dist/cli/server/ws-handler.mjs +1 -3
  24. package/dist/config.mjs +1 -2
  25. package/dist/core/codegen/client.d.mts +4 -27
  26. package/dist/core/codegen/client.mjs +26 -447
  27. package/dist/core/codegen/document-loader.mjs +1 -3
  28. package/dist/core/codegen/file-header.d.mts +7 -0
  29. package/dist/core/codegen/file-header.mjs +12 -0
  30. package/dist/core/codegen/index.d.mts +7 -5
  31. package/dist/core/codegen/index.mjs +7 -6
  32. package/dist/core/codegen/{runtime.d.mts → runtime-generator.d.mts} +1 -3
  33. package/dist/core/codegen/{runtime.mjs → runtime-generator.mjs} +7 -6
  34. package/dist/core/codegen/schema-loader.d.mts +2 -7
  35. package/dist/core/codegen/schema-loader.mjs +66 -73
  36. package/dist/core/codegen/server-type-helpers.d.mts +14 -0
  37. package/dist/core/codegen/server-type-helpers.mjs +76 -0
  38. package/dist/core/codegen/server.d.mts +1 -15
  39. package/dist/core/codegen/server.mjs +15 -107
  40. package/dist/core/codegen/subscription-extractor.d.mts +20 -0
  41. package/dist/core/codegen/subscription-extractor.mjs +30 -0
  42. package/dist/core/codegen/validation.mjs +1 -3
  43. package/dist/core/codegen/vue-subscription-builder.d.mts +10 -0
  44. package/dist/core/codegen/vue-subscription-builder.mjs +351 -0
  45. package/dist/core/constants.d.mts +5 -53
  46. package/dist/core/constants.mjs +13 -56
  47. package/dist/core/create-config.d.mts +31 -0
  48. package/dist/core/create-config.mjs +42 -0
  49. package/dist/core/debug/index.d.mts +2 -2
  50. package/dist/core/debug/index.mjs +2 -3
  51. package/dist/core/debug/template.d.mts +1 -5
  52. package/dist/core/debug/template.mjs +5 -4
  53. package/dist/core/extend/index.mjs +1 -2
  54. package/dist/core/extend/loader.d.mts +0 -1
  55. package/dist/core/extend/loader.mjs +60 -92
  56. package/dist/core/index.d.mts +22 -21
  57. package/dist/core/index.mjs +19 -16
  58. package/dist/core/manifest.mjs +4 -4
  59. package/dist/core/pubsub/index.d.mts +2 -109
  60. package/dist/core/pubsub/index.mjs +2 -148
  61. package/dist/core/pubsub/memory-pubsub.d.mts +109 -0
  62. package/dist/core/pubsub/memory-pubsub.mjs +146 -0
  63. package/dist/core/scanning/ast-scanner.mjs +5 -5
  64. package/dist/core/scanning/directives.mjs +3 -5
  65. package/dist/core/scanning/documents.d.mts +4 -3
  66. package/dist/core/scanning/documents.mjs +4 -5
  67. package/dist/core/scanning/{common.d.mts → file-scanner.d.mts} +1 -1
  68. package/dist/core/scanning/{common.mjs → file-scanner.mjs} +3 -13
  69. package/dist/core/scanning/index.d.mts +3 -3
  70. package/dist/core/scanning/index.mjs +3 -4
  71. package/dist/core/scanning/resolvers.mjs +9 -11
  72. package/dist/core/scanning/schemas.d.mts +1 -5
  73. package/dist/core/scanning/schemas.mjs +2 -26
  74. package/dist/core/schema/builder.d.mts +4 -2
  75. package/dist/core/schema/builder.mjs +4 -4
  76. package/dist/core/schema/federation.mjs +1 -3
  77. package/dist/core/schema/index.d.mts +2 -2
  78. package/dist/core/schema/index.mjs +2 -3
  79. package/dist/core/server/apollo.d.mts +20 -0
  80. package/dist/core/server/apollo.mjs +54 -0
  81. package/dist/core/server/index.d.mts +4 -2
  82. package/dist/core/server/index.mjs +3 -3
  83. package/dist/core/server/sandbox.mjs +1 -2
  84. package/dist/core/server/types.d.mts +7 -14
  85. package/dist/core/server/types.mjs +15 -1
  86. package/dist/core/server/yoga.d.mts +1 -7
  87. package/dist/core/server/yoga.mjs +4 -15
  88. package/dist/core/types/adapter.d.mts +3 -40
  89. package/dist/core/types/adapter.mjs +1 -1
  90. package/dist/core/types/codegen.d.mts +22 -45
  91. package/dist/core/types/codegen.mjs +1 -1
  92. package/dist/core/types/config.d.mts +33 -121
  93. package/dist/core/types/config.mjs +1 -1
  94. package/dist/core/types/define.d.mts +8 -5
  95. package/dist/core/types/define.mjs +1 -1
  96. package/dist/core/types/index.d.mts +5 -4
  97. package/dist/core/types/index.mjs +1 -1
  98. package/dist/core/types/scanning.d.mts +4 -1
  99. package/dist/core/types/scanning.mjs +1 -1
  100. package/dist/core/types/standard-schema.d.mts +64 -0
  101. package/dist/core/types/standard-schema.mjs +1 -0
  102. package/dist/core/utils/directive-parser.d.mts +8 -63
  103. package/dist/core/utils/directive-parser.mjs +114 -167
  104. package/dist/core/utils/errors.mjs +1 -3
  105. package/dist/core/utils/file-io.d.mts +1 -5
  106. package/dist/core/utils/file-io.mjs +1 -13
  107. package/dist/core/utils/imports.mjs +3 -4
  108. package/dist/core/utils/index.d.mts +5 -4
  109. package/dist/core/utils/index.mjs +6 -6
  110. package/dist/core/utils/logger.d.mts +1 -10
  111. package/dist/core/utils/logger.mjs +1 -21
  112. package/dist/core/utils/ofetch-templates.mjs +4 -6
  113. package/dist/core/utils/runtime.d.mts +1 -30
  114. package/dist/core/utils/runtime.mjs +2 -46
  115. package/dist/core/utils/string.d.mts +10 -0
  116. package/dist/core/utils/string.mjs +12 -0
  117. package/dist/core/utils/subscribe-templates.mjs +1 -2
  118. package/dist/core/validation/external-services.mjs +3 -3
  119. package/dist/core/validation/index.mjs +1 -2
  120. package/dist/core/watcher/create-watcher.d.mts +103 -0
  121. package/dist/core/watcher/create-watcher.mjs +143 -0
  122. package/dist/core/watcher/index.d.mts +2 -107
  123. package/dist/core/watcher/index.mjs +2 -141
  124. package/dist/define.d.mts +4 -2
  125. package/dist/define.mjs +8 -9
  126. package/dist/index.d.mts +4 -3
  127. package/dist/index.mjs +3 -4
  128. package/dist/nitro/adapter.d.mts +6 -11
  129. package/dist/nitro/adapter.mjs +12 -45
  130. package/dist/nitro/apollo.mjs +1 -3
  131. package/dist/nitro/codegen/client-types.d.mts +12 -0
  132. package/dist/nitro/codegen/client-types.mjs +73 -0
  133. package/dist/nitro/codegen/external-types.d.mts +8 -0
  134. package/dist/nitro/codegen/external-types.mjs +47 -0
  135. package/dist/nitro/codegen/index.d.mts +4 -0
  136. package/dist/nitro/codegen/index.mjs +4 -0
  137. package/dist/nitro/codegen/server-types.d.mts +12 -0
  138. package/dist/nitro/codegen/server-types.mjs +81 -0
  139. package/dist/nitro/defaults.d.mts +28 -0
  140. package/dist/nitro/defaults.mjs +34 -0
  141. package/dist/nitro/index.d.mts +11 -3
  142. package/dist/nitro/index.mjs +4 -6
  143. package/dist/nitro/paths.d.mts +3 -2
  144. package/dist/nitro/paths.mjs +14 -5
  145. package/dist/nitro/rollup.mjs +8 -8
  146. package/dist/nitro/routes/_ws-handler.d.mts +6 -0
  147. package/dist/nitro/routes/_ws-handler.mjs +49 -0
  148. package/dist/nitro/routes/apollo-sandbox-script.d.mts +2 -2
  149. package/dist/nitro/routes/apollo-sandbox-script.mjs +1 -3
  150. package/dist/nitro/routes/apollo-server-ws.d.mts +1 -1
  151. package/dist/nitro/routes/apollo-server-ws.mjs +10 -48
  152. package/dist/nitro/routes/apollo-server.d.mts +2 -2
  153. package/dist/nitro/routes/apollo-server.mjs +17 -58
  154. package/dist/nitro/routes/debug.d.mts +2 -2
  155. package/dist/nitro/routes/debug.mjs +4 -7
  156. package/dist/nitro/routes/graphql-yoga-ws.d.mts +3 -3
  157. package/dist/nitro/routes/graphql-yoga-ws.mjs +7 -48
  158. package/dist/nitro/routes/graphql-yoga.d.mts +2 -2
  159. package/dist/nitro/routes/graphql-yoga.mjs +8 -8
  160. package/dist/nitro/routes/health.d.mts +2 -2
  161. package/dist/nitro/routes/health.mjs +23 -27
  162. package/dist/nitro/setup/extend-loader.d.mts +6 -5
  163. package/dist/nitro/setup/extend-loader.mjs +31 -61
  164. package/dist/nitro/setup/file-watcher.mjs +9 -8
  165. package/dist/nitro/setup/logging.d.mts +1 -8
  166. package/dist/nitro/setup/logging.mjs +7 -24
  167. package/dist/nitro/setup/rollup-integration.mjs +36 -4
  168. package/dist/nitro/setup/routes.mjs +1 -3
  169. package/dist/nitro/setup/scanner.d.mts +8 -43
  170. package/dist/nitro/setup/scanner.mjs +56 -60
  171. package/dist/nitro/setup/security.d.mts +10 -0
  172. package/dist/nitro/setup/security.mjs +17 -0
  173. package/dist/nitro/setup/ts-config.mjs +1 -3
  174. package/dist/nitro/setup/type-generation.d.mts +13 -0
  175. package/dist/nitro/setup/type-generation.mjs +16 -0
  176. package/dist/nitro/setup.d.mts +4 -3
  177. package/dist/nitro/setup.mjs +88 -77
  178. package/dist/nitro/state.d.mts +32 -0
  179. package/dist/nitro/state.mjs +58 -0
  180. package/dist/nitro/types/augmentation.d.mts +59 -0
  181. package/dist/nitro/types/augmentation.mjs +1 -0
  182. package/dist/nitro/types/config.d.mts +327 -0
  183. package/dist/nitro/types/config.mjs +1 -0
  184. package/dist/nitro/types/define.d.mts +13 -0
  185. package/dist/nitro/types/define.mjs +1 -0
  186. package/dist/nitro/types/index.d.mts +10 -0
  187. package/dist/nitro/types/index.mjs +1 -0
  188. package/dist/nitro/virtual/debug-info.d.mts +9 -0
  189. package/dist/nitro/virtual/debug-info.mjs +35 -0
  190. package/dist/nitro/virtual/graphql-config.d.mts +9 -0
  191. package/dist/nitro/virtual/graphql-config.mjs +33 -0
  192. package/dist/nitro/virtual/index.d.mts +25 -0
  193. package/dist/nitro/virtual/index.mjs +45 -0
  194. package/dist/nitro/virtual/module-config.d.mts +9 -0
  195. package/dist/nitro/virtual/module-config.mjs +10 -0
  196. package/dist/nitro/virtual/pubsub.d.mts +9 -0
  197. package/dist/nitro/virtual/pubsub.mjs +17 -0
  198. package/dist/nitro/virtual/server-directives.d.mts +9 -0
  199. package/dist/nitro/virtual/server-directives.mjs +12 -0
  200. package/dist/nitro/virtual/server-resolvers.d.mts +9 -0
  201. package/dist/nitro/virtual/server-resolvers.mjs +17 -0
  202. package/dist/nitro/virtual/server-schemas.d.mts +9 -0
  203. package/dist/nitro/virtual/server-schemas.mjs +31 -0
  204. package/dist/nitro/virtual/stubs.d.mts +42 -10
  205. package/dist/nitro/virtual/stubs.mjs +1 -7
  206. package/dist/nitro/virtual/utils.d.mts +15 -0
  207. package/dist/nitro/virtual/utils.mjs +26 -0
  208. package/dist/nitro/virtual/validation-schemas.d.mts +9 -0
  209. package/dist/nitro/virtual/validation-schemas.mjs +33 -0
  210. package/dist/nuxt.mjs +4 -5
  211. package/dist/stubs/index.mjs +1 -1
  212. package/dist/subscribe/index.mjs +3 -4
  213. package/native/index.js +52 -52
  214. package/package.json +41 -41
  215. package/dist/cli/commands/index.d.mts +0 -5
  216. package/dist/cli/commands/index.mjs +0 -6
  217. package/dist/core/codegen/plugin.d.mts +0 -19
  218. package/dist/core/codegen/plugin.mjs +0 -30
  219. package/dist/core/config.d.mts +0 -45
  220. package/dist/core/config.mjs +0 -78
  221. package/dist/nitro/codegen.d.mts +0 -18
  222. package/dist/nitro/codegen.mjs +0 -173
  223. package/dist/nitro/config.d.mts +0 -50
  224. package/dist/nitro/config.mjs +0 -57
  225. package/dist/nitro/types.d.mts +0 -549
  226. package/dist/nitro/types.mjs +0 -1
  227. package/dist/nitro/virtual/generators.d.mts +0 -38
  228. package/dist/nitro/virtual/generators.mjs +0 -192
@@ -1,7 +1,5 @@
1
1
  import { DEFAULT_GRAPHQL_SCALARS } from "../constants.mjs";
2
- import { pluginContent } from "./plugin.mjs";
3
- import { loadGraphQLDocuments } from "./document-loader.mjs";
4
- import { downloadAndSaveSchema, graphQLLoadSchemaSync, loadExternalSchema } from "./schema-loader.mjs";
2
+ import { GENERATED_FILE_HEADER } from "./file-header.mjs";
5
3
  import { defu as defu$1 } from "defu";
6
4
  import consola from "consola";
7
5
  import { codegen } from "@graphql-codegen/core";
@@ -11,8 +9,7 @@ import { plugin as plugin$1 } from "@graphql-codegen/typescript";
11
9
  import { plugin as plugin$2 } from "@graphql-codegen/typescript-generic-sdk";
12
10
  import { plugin as plugin$3 } from "@graphql-codegen/typescript-operations";
13
11
  import { printSchemaWithDirectives } from "@graphql-tools/utils";
14
- import { Kind, parse } from "graphql";
15
-
12
+ import { parse } from "graphql";
16
13
  //#region src/core/codegen/client.ts
17
14
  /**
18
15
  * Default client codegen configuration
@@ -24,22 +21,16 @@ const DEFAULT_CLIENT_CODEGEN_CONFIG = {
24
21
  strictScalars: true,
25
22
  maybeValue: "T | null | undefined",
26
23
  inputMaybeValue: "T | undefined",
27
- documentMode: "string",
28
24
  pureMagicComment: true,
29
25
  dedupeOperationSuffix: true,
30
26
  rawRequest: true,
31
27
  scalars: DEFAULT_GRAPHQL_SCALARS
32
28
  };
33
29
  /**
34
- * Generate generic SDK content for schema-only generation
30
+ * Generate generic SDK stub for schema-only generation (no documents)
35
31
  */
36
32
  function generateGenericSdkContent() {
37
- return `// THIS FILE IS GENERATED, DO NOT EDIT!
38
- /* eslint-disable eslint-comments/no-unlimited-disable */
39
- /* tslint:disable */
40
- /* eslint-disable */
41
- /* prettier-ignore */
42
-
33
+ return `${GENERATED_FILE_HEADER}
43
34
  import type { GraphQLResolveInfo } from 'graphql'
44
35
  export type RequireFields<T, K extends keyof T> = Omit<T, K> & { [P in K]-?: NonNullable<T[P]> }
45
36
 
@@ -62,38 +53,30 @@ export function getSdk(requester: Requester): Sdk {
62
53
  }
63
54
  /**
64
55
  * Generate client-side GraphQL types
65
- * Pure function that generates TypeScript types from a GraphQL schema and documents
66
56
  */
67
57
  async function generateClientTypesCore(input) {
68
58
  const { schema, schemaString, documents, config = {}, sdkConfig = {}, outputPath, serviceName, virtualTypesPath } = input;
69
59
  if (documents.length === 0 && !serviceName) return false;
70
60
  const schemaSDL = schemaString || (schema ? printSchemaWithDirectives(schema) : null);
71
61
  if (!schemaSDL) return false;
72
- const mergedConfig = defu$1(DEFAULT_CLIENT_CODEGEN_CONFIG, config);
73
- const mergedSdkConfig = defu$1(mergedConfig, sdkConfig);
62
+ const mergedConfig = defu$1(config, DEFAULT_CLIENT_CODEGEN_CONFIG);
63
+ const resolvedSdkConfig = defu$1(sdkConfig, mergedConfig);
64
+ const parsedSchema = parse(schemaSDL);
74
65
  try {
75
66
  if (documents.length === 0) return {
76
- types: await codegen({
67
+ types: GENERATED_FILE_HEADER + await codegen({
77
68
  filename: outputPath || "client-types.generated.ts",
78
- schema: parse(schemaSDL),
69
+ schema: parsedSchema,
79
70
  documents: [],
80
71
  config: mergedConfig,
81
- plugins: [{ pluginContent: {} }, { typescript: {} }],
82
- pluginMap: {
83
- pluginContent: { plugin: pluginContent },
84
- typescript: { plugin: plugin$1 }
85
- }
72
+ plugins: [{ typescript: {} }],
73
+ pluginMap: { typescript: { plugin: plugin$1 } }
86
74
  }),
87
75
  sdk: generateGenericSdkContent()
88
76
  };
89
77
  const enableTypedDocumentNode = config.typedDocumentNode === true;
90
- const plugins = [
91
- { pluginContent: {} },
92
- { typescript: {} },
93
- { typescriptOperations: {} }
94
- ];
78
+ const plugins = [{ typescript: {} }, { typescriptOperations: {} }];
95
79
  const pluginMap = {
96
- pluginContent: { plugin: pluginContent },
97
80
  typescript: { plugin: plugin$1 },
98
81
  typescriptOperations: { plugin: plugin$3 }
99
82
  };
@@ -101,9 +84,9 @@ async function generateClientTypesCore(input) {
101
84
  plugins.push({ typedDocumentNode: {} });
102
85
  pluginMap.typedDocumentNode = { plugin };
103
86
  }
104
- const output = await codegen({
87
+ const generated = await codegen({
105
88
  filename: outputPath || "client-types.generated.ts",
106
- schema: parse(schemaSDL),
89
+ schema: parsedSchema,
107
90
  documents: [...documents],
108
91
  config: mergedConfig,
109
92
  plugins,
@@ -112,24 +95,20 @@ async function generateClientTypesCore(input) {
112
95
  const typesPath = virtualTypesPath || (serviceName ? `#graphql/client/${serviceName}` : "#graphql/client");
113
96
  const sdkOutput = await preset.buildGeneratesSection({
114
97
  baseOutputDir: outputPath || "client-types.generated.ts",
115
- schema: parse(schemaSDL),
98
+ schema: parsedSchema,
116
99
  documents: [...documents],
117
- config: mergedSdkConfig,
100
+ config: resolvedSdkConfig,
118
101
  presetConfig: { typesPath },
119
- plugins: [{ pluginContent: {} }, { typescriptGenericSdk: {} }],
120
- pluginMap: {
121
- pluginContent: { plugin: pluginContent },
122
- typescriptGenericSdk: { plugin: plugin$2 }
123
- }
102
+ plugins: [{ typescriptGenericSdk: {} }],
103
+ pluginMap: { typescriptGenericSdk: { plugin: plugin$2 } }
124
104
  });
105
+ const results = await Promise.all(sdkOutput.map(async (config) => ({
106
+ file: config.filename,
107
+ content: await codegen(config)
108
+ })));
125
109
  return {
126
- types: output,
127
- sdk: (await Promise.all(sdkOutput.map(async (config) => {
128
- return {
129
- file: config.filename,
130
- content: await codegen(config)
131
- };
132
- })))[0]?.content || ""
110
+ types: GENERATED_FILE_HEADER + generated,
111
+ sdk: GENERATED_FILE_HEADER + (results[0]?.content || "")
133
112
  };
134
113
  } catch (error) {
135
114
  consola.error("[nitro-graphql] Client type generation failed:", error.message);
@@ -137,7 +116,7 @@ async function generateClientTypesCore(input) {
137
116
  }
138
117
  }
139
118
  /**
140
- * Generate client types for external GraphQL service
119
+ * Generate client types for an external GraphQL service
141
120
  */
142
121
  async function generateExternalClientTypesCore(service, schema, documents, virtualTypesPath) {
143
122
  return generateClientTypesCore({
@@ -149,405 +128,5 @@ async function generateExternalClientTypesCore(service, schema, documents, virtu
149
128
  virtualTypesPath
150
129
  });
151
130
  }
152
- /**
153
- * Convert first character to uppercase (PascalCase)
154
- */
155
- function toPascalCase(str) {
156
- return str.charAt(0).toUpperCase() + str.slice(1);
157
- }
158
- /**
159
- * Extract subscription operations from GraphQL documents
160
- */
161
- function extractSubscriptions(docs) {
162
- const subscriptions = [];
163
- for (const doc of docs) {
164
- if (!doc.document) continue;
165
- for (const def of doc.document.definitions) if (def.kind === Kind.OPERATION_DEFINITION && def.operation === "subscription") {
166
- const operationDef = def;
167
- const name = operationDef.name?.value;
168
- if (!name) continue;
169
- const firstSelection = operationDef.selectionSet.selections[0];
170
- if (firstSelection.kind !== Kind.FIELD) continue;
171
- const fieldName = firstSelection.name.value;
172
- const hasVariables = (operationDef.variableDefinitions?.length || 0) > 0;
173
- subscriptions.push({
174
- name,
175
- typeName: toPascalCase(name),
176
- fieldName,
177
- hasVariables
178
- });
179
- }
180
- }
181
- return subscriptions;
182
- }
183
- /**
184
- * Generate subscription builder code (Drizzle-style API) + Vue Composables
185
- * Returns empty string if subscriptions are not enabled or no subscription operations found
186
- */
187
- function generateSubscriptionBuilder(docs, subscriptionsEnabled) {
188
- if (!subscriptionsEnabled) return "";
189
- const subscriptions = extractSubscriptions(docs);
190
- if (subscriptions.length === 0) return "";
191
- let output = `
192
- // === Subscription Imports ===
193
- import { ref, onUnmounted, computed } from 'vue'
194
- import type { Ref } from 'vue'
195
- import type {
196
- ConnectionState,
197
- SubscriptionHandle,
198
- SubscriptionSession,
199
- SubscriptionTransport,
200
- TransportOptions,
201
- } from 'nitro-graphql/subscribe'
202
- import { subscriptionClient } from './subscribe'
203
-
204
- // === Subscription Types ===
205
- export type { ConnectionState, SubscriptionHandle, SubscriptionSession, SubscriptionTransport, TransportOptions }
206
-
207
- // Forward declaration for UseSubscriptionSessionReturn (defined below)
208
- export interface UseSubscriptionSessionReturn {
209
- /** The underlying session object */
210
- session: SubscriptionSession
211
- /** Subscribe using the shared session (updates reactive refs) */
212
- subscribe: <TData = unknown>(
213
- query: string,
214
- variables: unknown,
215
- onData?: (data: TData) => void,
216
- onError?: (error: Error) => void,
217
- ) => SubscriptionHandle
218
- /** Close all subscriptions and the connection */
219
- close: () => void
220
- /** Is the session connected (reactive) */
221
- isConnected: Ref<boolean>
222
- /** Current connection state (reactive) */
223
- state: Ref<ConnectionState>
224
- /** Number of active subscriptions (reactive) */
225
- subscriptionCount: Ref<number>
226
- }
227
-
228
- export interface UseSubscriptionOptions<T> {
229
- /** Auto-start subscription on mount (default: false) */
230
- immediate?: boolean
231
- /** Callback when subscription starts */
232
- onStart?: () => void
233
- /** Callback when subscription stops */
234
- onStop?: () => void
235
- /** Callback when data is received */
236
- onData?: (data: T) => void
237
- /** Callback when error occurs */
238
- onError?: (error: Error) => void
239
- /** Callback when WebSocket connects */
240
- onConnected?: () => void
241
- /** Callback when WebSocket reconnects */
242
- onReconnected?: () => void
243
- /** Callback when WebSocket disconnects */
244
- onDisconnected?: () => void
245
- /** Callback when connection state changes */
246
- onStateChange?: (state: ConnectionState) => void
247
- /** Use existing session for multiplexing (pass result from useSubscriptionSession) */
248
- session?: UseSubscriptionSessionReturn
249
- /** Transport type: 'websocket' (default), 'sse', or 'auto' (WS first, SSE fallback) */
250
- transport?: SubscriptionTransport
251
- }
252
-
253
- export interface UseSubscriptionReturn<T> {
254
- /** Reactive subscription data */
255
- data: Ref<T | null>
256
- /** Reactive error state */
257
- error: Ref<Error | null>
258
- /** Is subscription active */
259
- isActive: Ref<boolean>
260
- /** Connection state */
261
- state: Ref<ConnectionState>
262
- /** Active transport type ('websocket' | 'sse') */
263
- transport: Ref<'websocket' | 'sse'>
264
- /** Start subscription */
265
- start: () => void
266
- /** Stop subscription */
267
- stop: () => void
268
- /** Restart subscription */
269
- restart: () => void
270
- }
271
-
272
- // === Subscription Builder (Drizzle-style API) ===
273
- interface SubscriptionBuilder<TData> {
274
- onData(fn: (data: TData) => void): SubscriptionBuilder<TData>
275
- onError(fn: (error: Error) => void): SubscriptionBuilder<TData>
276
- start(): SubscriptionHandle
277
- subscribe(fn: (data: TData) => void): SubscriptionHandle
278
- }
279
-
280
- function createSubscriptionBuilder<TData>(query: string, variables: unknown): SubscriptionBuilder<TData> {
281
- let onDataFn: ((data: TData) => void) | undefined
282
- let onErrorFn: ((error: Error) => void) | undefined
283
-
284
- const builder: SubscriptionBuilder<TData> = {
285
- onData(fn: (data: TData) => void) {
286
- onDataFn = fn
287
- return builder
288
- },
289
- onError(fn: (error: Error) => void) {
290
- onErrorFn = fn
291
- return builder
292
- },
293
- start(): SubscriptionHandle {
294
- return subscriptionClient.subscribe(query, variables, onDataFn, onErrorFn)
295
- },
296
- subscribe(fn: (data: TData) => void): SubscriptionHandle {
297
- return subscriptionClient.subscribe(query, variables, fn, undefined)
298
- },
299
- }
300
-
301
- return builder
302
- }
303
-
304
- export const subscription = {
305
- `;
306
- for (const sub of subscriptions) if (sub.hasVariables) output += ` ${sub.typeName}(variables: Types.${sub.typeName}SubscriptionVariables): SubscriptionBuilder<Types.${sub.typeName}Subscription['${sub.fieldName}']> {
307
- return createSubscriptionBuilder<Types.${sub.typeName}Subscription['${sub.fieldName}']>(${sub.typeName}Document, variables)
308
- },
309
- `;
310
- else output += ` ${sub.typeName}(): SubscriptionBuilder<Types.${sub.typeName}Subscription['${sub.fieldName}']> {
311
- return createSubscriptionBuilder<Types.${sub.typeName}Subscription['${sub.fieldName}']>(${sub.typeName}Document, undefined)
312
- },
313
- `;
314
- output += `}
315
-
316
- // === Framework-Agnostic Session (for non-Vue usage) ===
317
- /**
318
- * Create a multiplexed subscription session (framework-agnostic)
319
- * All subscriptions share a single WebSocket connection.
320
- *
321
- * @example
322
- * // Vanilla JS / Node.js / React / etc.
323
- * const session = createSubscriptionSession()
324
- * const sub1 = session.subscribe(query1, vars1, onData1)
325
- * const sub2 = session.subscribe(query2, vars2, onData2)
326
- * // Both use the same WebSocket connection
327
- * sub1.unsubscribe()
328
- * session.close() // Close all
329
- *
330
- * @returns SubscriptionSession - Framework-agnostic session object
331
- */
332
- export function createSubscriptionSession(): SubscriptionSession {
333
- return subscriptionClient.createSession()
334
- }
335
-
336
- // === Vue Composable: useSubscriptionSession (Multiplexing) ===
337
- export interface UseSubscriptionSessionReturn {
338
- /** The underlying session object */
339
- session: SubscriptionSession
340
- /** Subscribe using the shared session */
341
- subscribe: <TData = unknown>(
342
- query: string,
343
- variables: unknown,
344
- onData?: (data: TData) => void,
345
- onError?: (error: Error) => void,
346
- ) => SubscriptionHandle
347
- /** Close all subscriptions and the connection */
348
- close: () => void
349
- /** Is the session connected (reactive) */
350
- isConnected: Ref<boolean>
351
- /** Current connection state (reactive) */
352
- state: Ref<ConnectionState>
353
- /** Number of active subscriptions (reactive) */
354
- subscriptionCount: Ref<number>
355
- }
356
-
357
- /**
358
- * Vue composable for multiplexed subscription session
359
- * Provides reactive state and automatic cleanup on unmount.
360
- *
361
- * @example
362
- * // Vue 3 component
363
- * const session = useSubscriptionSession()
364
- * const { data } = useCountdown({ from: 10 }, { session })
365
- * // Session auto-closes on component unmount
366
- *
367
- * @returns UseSubscriptionSessionReturn - Vue-reactive session wrapper
368
- */
369
- export function useSubscriptionSession(): UseSubscriptionSessionReturn {
370
- const session = subscriptionClient.createSession()
371
-
372
- // Use refs for reactivity (session getters are not reactive)
373
- const isConnected = ref(session.isConnected)
374
- const state = ref<ConnectionState>(session.state)
375
- const subscriptionCount = ref(session.subscriptionCount)
376
-
377
- // Update refs when session state changes
378
- function updateRefs() {
379
- isConnected.value = session.isConnected
380
- state.value = session.state
381
- subscriptionCount.value = session.subscriptionCount
382
- }
383
-
384
- // Subscribe to session state changes for automatic reactivity
385
- const unsubscribeStateChange = session.onStateChange(() => {
386
- updateRefs()
387
- })
388
-
389
- function subscribe<TData = unknown>(
390
- query: string,
391
- variables: unknown,
392
- onData?: (data: TData) => void,
393
- onError?: (error: Error) => void,
394
- ): SubscriptionHandle {
395
- return session.subscribe(query, variables, onData as (data: unknown) => void, onError)
396
- }
397
-
398
- function close() {
399
- session.close()
400
- }
401
-
402
- onUnmounted(() => {
403
- unsubscribeStateChange()
404
- close()
405
- })
406
-
407
- return {
408
- session,
409
- subscribe,
410
- close,
411
- isConnected,
412
- state,
413
- subscriptionCount,
414
- }
415
- }
416
-
417
- // === Vue Composables ===
418
- function createUseSubscription<TData, TVariables = undefined>(
419
- query: string,
420
- getVariables: () => TVariables,
421
- ): (options?: UseSubscriptionOptions<TData>) => UseSubscriptionReturn<TData> {
422
- return (options: UseSubscriptionOptions<TData> = {}): UseSubscriptionReturn<TData> => {
423
- const data = ref<TData | null>(null) as Ref<TData | null>
424
- const error = ref<Error | null>(null)
425
- const isActive = ref(false)
426
- const state = ref<ConnectionState>('idle')
427
- const transport = ref<'websocket' | 'sse'>('websocket')
428
- let handle: SubscriptionHandle | null = null
429
-
430
- // Resolve transport options
431
- const transportOptions: TransportOptions = {
432
- transport: options.transport,
433
- }
434
-
435
- function start() {
436
- stop()
437
- isActive.value = true
438
- error.value = null
439
- options.onStart?.()
440
-
441
- const variables = getVariables()
442
-
443
- if (options.session) {
444
- // Use existing session for multiplexing (WebSocket only)
445
- handle = options.session.subscribe<TData>(
446
- query,
447
- variables,
448
- (d: TData) => {
449
- data.value = d
450
- options.onData?.(d)
451
- },
452
- (e: Error) => {
453
- error.value = e
454
- options.onError?.(e)
455
- },
456
- )
457
- transport.value = 'websocket'
458
- } else {
459
- // Create dedicated connection with transport selection
460
- handle = subscriptionClient.subscribe<TData>(
461
- query,
462
- variables,
463
- (d: TData) => {
464
- data.value = d
465
- options.onData?.(d)
466
- },
467
- (e: Error) => {
468
- error.value = e
469
- options.onError?.(e)
470
- },
471
- transportOptions,
472
- )
473
- // Update transport ref from handle
474
- transport.value = handle.transport
475
- }
476
- }
477
-
478
- function stop() {
479
- if (handle) {
480
- handle.unsubscribe()
481
- handle = null
482
- isActive.value = false
483
- options.onStop?.()
484
- }
485
- }
486
-
487
- function restart() {
488
- stop()
489
- start()
490
- }
491
-
492
- if (options.immediate) {
493
- start()
494
- }
495
-
496
- onUnmounted(stop)
497
-
498
- return { data, error, isActive, state, transport, start, stop, restart }
499
- }
500
- }
501
-
502
- // === Subscription Return Types ===
503
- `;
504
- for (const sub of subscriptions) {
505
- const typeName = `Types.${sub.typeName}Subscription['${sub.fieldName}']`;
506
- output += `/** Return type for use${sub.typeName} composable */
507
- export type Use${sub.typeName}Return = UseSubscriptionReturn<${typeName}>
508
- `;
509
- }
510
- output += `
511
- // === Vue Composables ===
512
- `;
513
- for (const sub of subscriptions) {
514
- const typeName = `Types.${sub.typeName}Subscription['${sub.fieldName}']`;
515
- const varsType = `Types.${sub.typeName}SubscriptionVariables`;
516
- if (sub.hasVariables) output += `/**
517
- * Vue composable for ${sub.typeName} subscription
518
- * @param variables - Subscription variables
519
- * @param options - Subscription options (immediate, onData, onError, session, etc.)
520
- * @returns Reactive subscription state: { data, error, isActive, state, start, stop, restart }
521
- */
522
- export function use${sub.typeName}(
523
- variables: ${varsType},
524
- options?: UseSubscriptionOptions<${typeName}>,
525
- ): Use${sub.typeName}Return {
526
- return createUseSubscription<${typeName}, ${varsType}>(
527
- ${sub.typeName}Document,
528
- () => variables,
529
- )(options)
530
- }
531
-
532
- `;
533
- else output += `/**
534
- * Vue composable for ${sub.typeName} subscription
535
- * @param options - Subscription options (immediate, onData, onError, session, etc.)
536
- * @returns Reactive subscription state: { data, error, isActive, state, start, stop, restart }
537
- */
538
- export function use${sub.typeName}(
539
- options?: UseSubscriptionOptions<${typeName}>,
540
- ): Use${sub.typeName}Return {
541
- return createUseSubscription<${typeName}, undefined>(
542
- ${sub.typeName}Document,
543
- () => undefined,
544
- )(options)
545
- }
546
-
547
- `;
548
- }
549
- return output;
550
- }
551
-
552
131
  //#endregion
553
- export { DEFAULT_CLIENT_CODEGEN_CONFIG, downloadAndSaveSchema, extractSubscriptions, generateClientTypesCore, generateExternalClientTypesCore, generateSubscriptionBuilder, graphQLLoadSchemaSync, loadExternalSchema, loadGraphQLDocuments };
132
+ export { DEFAULT_CLIENT_CODEGEN_CONFIG, generateClientTypesCore, generateExternalClientTypesCore };
@@ -1,6 +1,5 @@
1
1
  import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader";
2
2
  import { loadDocuments } from "@graphql-tools/load";
3
-
4
3
  //#region src/core/codegen/document-loader.ts
5
4
  /**
6
5
  * Load GraphQL documents from files
@@ -13,6 +12,5 @@ async function loadGraphQLDocuments(patterns) {
13
12
  throw e;
14
13
  }
15
14
  }
16
-
17
15
  //#endregion
18
- export { loadGraphQLDocuments };
16
+ export { loadGraphQLDocuments };
@@ -0,0 +1,7 @@
1
+ //#region src/core/codegen/file-header.d.ts
2
+ /**
3
+ * Standard header prepended to all generated files
4
+ */
5
+ declare const GENERATED_FILE_HEADER = "// THIS FILE IS GENERATED, DO NOT EDIT!\n/* eslint-disable eslint-comments/no-unlimited-disable */\n/* tslint:disable */\n/* eslint-disable */\n/* prettier-ignore */\n";
6
+ //#endregion
7
+ export { GENERATED_FILE_HEADER };
@@ -0,0 +1,12 @@
1
+ //#region src/core/codegen/file-header.ts
2
+ /**
3
+ * Standard header prepended to all generated files
4
+ */
5
+ const GENERATED_FILE_HEADER = `// THIS FILE IS GENERATED, DO NOT EDIT!
6
+ /* eslint-disable eslint-comments/no-unlimited-disable */
7
+ /* tslint:disable */
8
+ /* eslint-disable */
9
+ /* prettier-ignore */
10
+ `;
11
+ //#endregion
12
+ export { GENERATED_FILE_HEADER };
@@ -1,8 +1,10 @@
1
+ import { DEFAULT_CLIENT_CODEGEN_CONFIG, generateClientTypesCore, generateExternalClientTypesCore } from "./client.mjs";
1
2
  import { loadGraphQLDocuments } from "./document-loader.mjs";
3
+ import { GENERATED_FILE_HEADER } from "./file-header.mjs";
4
+ import { generateResolverModule, generateRuntimeIndex, generateSchemaModule } from "./runtime-generator.mjs";
2
5
  import { GraphQLLoadSchemaOptions, GraphQLTypeDefPointer, downloadAndSaveSchema, graphQLLoadSchemaSync, loadExternalSchema } from "./schema-loader.mjs";
3
- import { DEFAULT_CLIENT_CODEGEN_CONFIG, SubscriptionInfo, extractSubscriptions, generateClientTypesCore, generateExternalClientTypesCore, generateSubscriptionBuilder } from "./client.mjs";
4
- import { GENERATED_FILE_HEADER, pluginContent } from "./plugin.mjs";
5
- import { generateResolverModule, generateRuntimeIndex, generateSchemaModule } from "./runtime.mjs";
6
- import { DEFAULT_SERVER_CODEGEN_CONFIG, generateServerTypesCore, generateTypes } from "./server.mjs";
6
+ import { DEFAULT_SERVER_CODEGEN_CONFIG, generateServerTypesCore } from "./server.mjs";
7
+ import { SubscriptionInfo, extractSubscriptions } from "./subscription-extractor.mjs";
7
8
  import { validateNoDuplicateTypes, validateSchemaFiles } from "./validation.mjs";
8
- export { DEFAULT_CLIENT_CODEGEN_CONFIG, DEFAULT_SERVER_CODEGEN_CONFIG, GENERATED_FILE_HEADER, type GraphQLLoadSchemaOptions, type GraphQLTypeDefPointer, type SubscriptionInfo, downloadAndSaveSchema, extractSubscriptions, generateClientTypesCore, generateExternalClientTypesCore, generateResolverModule, generateRuntimeIndex, generateSchemaModule, generateServerTypesCore, generateSubscriptionBuilder, generateTypes, graphQLLoadSchemaSync, loadExternalSchema, loadGraphQLDocuments, pluginContent, validateNoDuplicateTypes, validateSchemaFiles };
9
+ import { generateSubscriptionBuilder } from "./vue-subscription-builder.mjs";
10
+ export { DEFAULT_CLIENT_CODEGEN_CONFIG, DEFAULT_SERVER_CODEGEN_CONFIG, GENERATED_FILE_HEADER, type GraphQLLoadSchemaOptions, type GraphQLTypeDefPointer, type SubscriptionInfo, downloadAndSaveSchema, extractSubscriptions, generateClientTypesCore, generateExternalClientTypesCore, generateResolverModule, generateRuntimeIndex, generateSchemaModule, generateServerTypesCore, generateSubscriptionBuilder, graphQLLoadSchemaSync, loadExternalSchema, loadGraphQLDocuments, validateNoDuplicateTypes, validateSchemaFiles };
@@ -1,9 +1,10 @@
1
- import { GENERATED_FILE_HEADER, pluginContent } from "./plugin.mjs";
1
+ import { GENERATED_FILE_HEADER } from "./file-header.mjs";
2
+ import { DEFAULT_CLIENT_CODEGEN_CONFIG, generateClientTypesCore, generateExternalClientTypesCore } from "./client.mjs";
2
3
  import { loadGraphQLDocuments } from "./document-loader.mjs";
4
+ import { generateResolverModule, generateRuntimeIndex, generateSchemaModule } from "./runtime-generator.mjs";
3
5
  import { downloadAndSaveSchema, graphQLLoadSchemaSync, loadExternalSchema } from "./schema-loader.mjs";
4
- import { DEFAULT_CLIENT_CODEGEN_CONFIG, extractSubscriptions, generateClientTypesCore, generateExternalClientTypesCore, generateSubscriptionBuilder } from "./client.mjs";
5
- import { generateResolverModule, generateRuntimeIndex, generateSchemaModule } from "./runtime.mjs";
6
- import { DEFAULT_SERVER_CODEGEN_CONFIG, generateServerTypesCore, generateTypes } from "./server.mjs";
6
+ import { DEFAULT_SERVER_CODEGEN_CONFIG, generateServerTypesCore } from "./server.mjs";
7
+ import { extractSubscriptions } from "./subscription-extractor.mjs";
7
8
  import { validateNoDuplicateTypes, validateSchemaFiles } from "./validation.mjs";
8
-
9
- export { DEFAULT_CLIENT_CODEGEN_CONFIG, DEFAULT_SERVER_CODEGEN_CONFIG, GENERATED_FILE_HEADER, downloadAndSaveSchema, extractSubscriptions, generateClientTypesCore, generateExternalClientTypesCore, generateResolverModule, generateRuntimeIndex, generateSchemaModule, generateServerTypesCore, generateSubscriptionBuilder, generateTypes, graphQLLoadSchemaSync, loadExternalSchema, loadGraphQLDocuments, pluginContent, validateNoDuplicateTypes, validateSchemaFiles };
9
+ import { generateSubscriptionBuilder } from "./vue-subscription-builder.mjs";
10
+ export { DEFAULT_CLIENT_CODEGEN_CONFIG, DEFAULT_SERVER_CODEGEN_CONFIG, GENERATED_FILE_HEADER, downloadAndSaveSchema, extractSubscriptions, generateClientTypesCore, generateExternalClientTypesCore, generateResolverModule, generateRuntimeIndex, generateSchemaModule, generateServerTypesCore, generateSubscriptionBuilder, graphQLLoadSchemaSync, loadExternalSchema, loadGraphQLDocuments, validateNoDuplicateTypes, validateSchemaFiles };
@@ -1,7 +1,5 @@
1
1
  import { ScannedResolver } from "../types/scanning.mjs";
2
- import "../types/index.mjs";
3
-
4
- //#region src/core/codegen/runtime.d.ts
2
+ //#region src/core/codegen/runtime-generator.d.ts
5
3
  /**
6
4
  * Generate resolver module code
7
5
  * Outputs Nitro-compatible format with { resolver: ... } wrapper
@@ -1,6 +1,8 @@
1
1
  import { relative } from "pathe";
2
-
3
- //#region src/core/codegen/runtime.ts
2
+ //#region src/core/codegen/runtime-generator.ts
3
+ const TS_EXTENSION_RE = /\.ts$/;
4
+ const BACKTICK_RE = /`/g;
5
+ const DOLLAR_SIGN_RE = /\$/g;
4
6
  /**
5
7
  * Generate resolver module code
6
8
  * Outputs Nitro-compatible format with { resolver: ... } wrapper
@@ -13,7 +15,7 @@ export const resolvers = []
13
15
  const resolverEntries = [];
14
16
  const reexports = [];
15
17
  for (const resolver of resolvers) {
16
- const relativePath = relative(baseDir, resolver.specifier).replace(/\.ts$/, "");
18
+ const relativePath = relative(baseDir, resolver.specifier).replace(TS_EXTENSION_RE, "");
17
19
  const exportNames = resolver.imports.map((i) => i.name);
18
20
  if (exportNames.length > 0) {
19
21
  imports.push(`import { ${exportNames.join(", ")} } from '${relativePath}'`);
@@ -38,7 +40,7 @@ ${reexports.join("\n")}
38
40
  function generateSchemaModule(schemaString) {
39
41
  return `// Auto-generated by nitro-graphql - do not edit
40
42
  export const schemaString = \`
41
- ${schemaString.replace(/`/g, "\\`").replace(/\$/g, "\\$")}
43
+ ${schemaString.replace(BACKTICK_RE, "\\`").replace(DOLLAR_SIGN_RE, "\\$")}
42
44
  \`
43
45
 
44
46
  // Pre-parsed for convenience
@@ -55,6 +57,5 @@ export { resolvers } from './resolvers'
55
57
  export { schemaString, typeDefs } from './schema'
56
58
  `;
57
59
  }
58
-
59
60
  //#endregion
60
- export { generateResolverModule, generateRuntimeIndex, generateSchemaModule };
61
+ export { generateResolverModule, generateRuntimeIndex, generateSchemaModule };