nitro-graphql 2.0.0-beta.2 → 2.0.0-beta.20

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 (64) hide show
  1. package/README.md +855 -5
  2. package/dist/ecosystem/nuxt.mjs +109 -0
  3. package/dist/index.d.mts +12 -0
  4. package/dist/index.mjs +16 -0
  5. package/dist/rollup.d.mts +12 -0
  6. package/dist/rollup.mjs +282 -0
  7. package/dist/routes/apollo-server.d.mts +6 -0
  8. package/dist/routes/{apollo-server.js → apollo-server.mjs} +5 -5
  9. package/dist/routes/debug.d.mts +61 -0
  10. package/dist/routes/debug.mjs +445 -0
  11. package/dist/routes/graphql-yoga.d.mts +6 -0
  12. package/dist/routes/{graphql-yoga.js → graphql-yoga.mjs} +5 -5
  13. package/dist/routes/health.d.mts +10 -0
  14. package/dist/routes/{health.js → health.mjs} +1 -0
  15. package/dist/setup.d.mts +11 -0
  16. package/dist/setup.mjs +390 -0
  17. package/dist/types/index.d.mts +246 -0
  18. package/dist/utils/{apollo.js → apollo.mjs} +2 -2
  19. package/dist/utils/{client-codegen.d.ts → client-codegen.d.mts} +3 -3
  20. package/dist/utils/{client-codegen.js → client-codegen.mjs} +4 -4
  21. package/dist/utils/file-generator.d.mts +37 -0
  22. package/dist/utils/file-generator.mjs +72 -0
  23. package/dist/utils/{index.d.ts → index.d.mts} +2 -2
  24. package/dist/utils/{index.js → index.mjs} +63 -35
  25. package/dist/utils/path-resolver.d.mts +70 -0
  26. package/dist/utils/path-resolver.mjs +127 -0
  27. package/dist/utils/{server-codegen.d.ts → server-codegen.d.mts} +1 -1
  28. package/dist/utils/{server-codegen.js → server-codegen.mjs} +1 -1
  29. package/dist/utils/{type-generation.js → type-generation.mjs} +153 -42
  30. package/dist/virtual/debug-info.d.mts +9 -0
  31. package/dist/virtual/debug-info.mjs +26 -0
  32. package/dist/virtual/graphql-config.d.mts +9 -0
  33. package/dist/virtual/graphql-config.mjs +10 -0
  34. package/dist/virtual/module-config.d.mts +9 -0
  35. package/dist/virtual/module-config.mjs +10 -0
  36. package/dist/virtual/server-directives.d.mts +11 -0
  37. package/dist/virtual/server-directives.mjs +10 -0
  38. package/dist/virtual/server-resolvers.d.mts +11 -0
  39. package/dist/virtual/server-resolvers.mjs +10 -0
  40. package/dist/virtual/server-schemas.d.mts +11 -0
  41. package/dist/virtual/server-schemas.mjs +10 -0
  42. package/dist/vite.d.mts +27 -0
  43. package/dist/vite.mjs +49 -0
  44. package/package.json +72 -58
  45. package/dist/ecosystem/nuxt.js +0 -67
  46. package/dist/index.d.ts +0 -8
  47. package/dist/index.js +0 -264
  48. package/dist/rollup.js +0 -114
  49. package/dist/routes/apollo-server.d.ts +0 -6
  50. package/dist/routes/graphql-yoga.d.ts +0 -6
  51. package/dist/routes/health.d.ts +0 -6
  52. package/dist/types/index.d.ts +0 -128
  53. /package/dist/ecosystem/{nuxt.d.ts → nuxt.d.mts} +0 -0
  54. /package/dist/graphql/{index.d.ts → index.d.mts} +0 -0
  55. /package/dist/graphql/{index.js → index.mjs} +0 -0
  56. /package/dist/graphql/{server.d.ts → server.d.mts} +0 -0
  57. /package/dist/graphql/{server.js → server.mjs} +0 -0
  58. /package/dist/types/{standard-schema.d.ts → standard-schema.d.mts} +0 -0
  59. /package/dist/utils/{apollo.d.ts → apollo.d.mts} +0 -0
  60. /package/dist/utils/{define.d.ts → define.d.mts} +0 -0
  61. /package/dist/utils/{define.js → define.mjs} +0 -0
  62. /package/dist/utils/{directive-parser.d.ts → directive-parser.d.mts} +0 -0
  63. /package/dist/utils/{directive-parser.js → directive-parser.mjs} +0 -0
  64. /package/dist/utils/{type-generation.d.ts → type-generation.d.mts} +0 -0
@@ -1,5 +1,7 @@
1
- import { downloadAndSaveSchema, generateClientTypes, generateExternalClientTypes, loadExternalSchema, loadGraphQLDocuments } from "./client-codegen.js";
2
- import { generateTypes } from "./server-codegen.js";
1
+ import { downloadAndSaveSchema, generateClientTypes, generateExternalClientTypes, loadExternalSchema, loadGraphQLDocuments } from "./client-codegen.mjs";
2
+ import { writeFileIfNotExists } from "./file-generator.mjs";
3
+ import { getClientUtilsConfig, getDefaultPaths, getSdkConfig, getTypesConfig, resolveFilePath, shouldGenerateClientUtils, shouldGenerateTypes } from "./path-resolver.mjs";
4
+ import { generateTypes } from "./server-codegen.mjs";
3
5
  import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
4
6
  import consola from "consola";
5
7
  import { basename, dirname, join, resolve } from "pathe";
@@ -10,12 +12,16 @@ import { mergeTypeDefs } from "@graphql-tools/merge";
10
12
  import { printSchemaWithDirectives } from "@graphql-tools/utils";
11
13
 
12
14
  //#region src/utils/type-generation.ts
13
- function generateGraphQLIndexFile(clientDir, externalServices = []) {
14
- const indexPath = resolve(clientDir, "index.ts");
15
+ function generateGraphQLIndexFile(nitro, clientDir, externalServices = []) {
16
+ if (!shouldGenerateClientUtils(nitro)) return;
17
+ const placeholders = getDefaultPaths(nitro);
18
+ const clientUtilsConfig = getClientUtilsConfig(nitro);
19
+ const indexPath = resolveFilePath(clientUtilsConfig.index, clientUtilsConfig.enabled, true, "{clientGraphql}/index.ts", placeholders);
20
+ if (!indexPath) return;
15
21
  if (!existsSync(indexPath)) {
16
22
  let indexContent = `// This file is auto-generated once by nitro-graphql for quick start
17
23
  // You can modify this file according to your needs
18
- //
24
+ //
19
25
  // Export your main GraphQL service (auto-generated)
20
26
  export * from './default/ofetch'
21
27
 
@@ -24,23 +30,27 @@ export * from './default/ofetch'
24
30
  // export * from './yourServiceName/ofetch'
25
31
  `;
26
32
  for (const service of externalServices) indexContent += `export * from './${service.name}/ofetch'\n`;
27
- writeFileSync(indexPath, indexContent, "utf-8");
33
+ writeFileIfNotExists(indexPath, indexContent, "client index.ts");
28
34
  }
29
35
  }
30
- function generateOfetchClient(clientDir, serviceName, endpoint, isDefault = false) {
31
- const serviceDir = resolve(clientDir, serviceName);
32
- const ofetchPath = resolve(serviceDir, "ofetch.ts");
36
+ function generateNuxtOfetchClient(nitro, clientDir, serviceName = "default") {
37
+ if (!shouldGenerateClientUtils(nitro)) return;
38
+ const placeholders = {
39
+ ...getDefaultPaths(nitro),
40
+ serviceName
41
+ };
42
+ const clientUtilsConfig = getClientUtilsConfig(nitro);
43
+ const ofetchPath = resolveFilePath(clientUtilsConfig.ofetch, clientUtilsConfig.enabled, true, "{clientGraphql}/{serviceName}/ofetch.ts", placeholders);
44
+ if (!ofetchPath) return;
45
+ const serviceDir = dirname(ofetchPath);
33
46
  if (!existsSync(serviceDir)) mkdirSync(serviceDir, { recursive: true });
34
47
  if (existsSync(ofetchPath)) return;
35
- const capitalizedServiceName = serviceName.charAt(0).toUpperCase() + serviceName.slice(1);
36
- const functionName = isDefault ? "createGraphQLClient" : `create${capitalizedServiceName}GraphQLClient`;
37
- const exportName = isDefault ? "$sdk" : `$${serviceName}Sdk`;
38
- writeFileSync(ofetchPath, `// This file is auto-generated once by nitro-graphql for quick start
48
+ writeFileIfNotExists(ofetchPath, nitro.options.framework?.name === "nuxt" ? `// This file is auto-generated once by nitro-graphql for quick start
39
49
  // You can modify this file according to your needs
40
- import type { ${isDefault ? "Requester" : "Sdk, Requester"} } from './sdk'
50
+ import type { Requester } from './sdk'
41
51
  import { getSdk } from './sdk'
42
52
 
43
- export function ${functionName}(endpoint: string${isDefault ? "" : ` = '${endpoint}'`}): Requester {
53
+ export function createGraphQLClient(endpoint: string): Requester {
44
54
  return async <R>(doc: string, vars?: any): Promise<R> => {
45
55
  const headers = import.meta.server ? useRequestHeaders() : undefined
46
56
 
@@ -57,7 +67,86 @@ export function ${functionName}(endpoint: string${isDefault ? "" : ` = '${endpoi
57
67
  }
58
68
  }
59
69
 
60
- export const ${exportName}${isDefault ? "" : ": Sdk"} = getSdk(${functionName}(${isDefault ? "'/api/graphql'" : ""}))`, "utf-8");
70
+ export const $sdk = getSdk(createGraphQLClient('/api/graphql'))` : `// This file is auto-generated once by nitro-graphql for quick start
71
+ // You can modify this file according to your needs
72
+ import type { Requester } from './sdk'
73
+ import { ofetch } from 'ofetch'
74
+ import { getSdk } from './sdk'
75
+
76
+ export function createGraphQLClient(endpoint: string): Requester {
77
+ return async <R>(doc: string, vars?: any): Promise<R> => {
78
+ const result = await ofetch(endpoint, {
79
+ method: 'POST',
80
+ body: { query: doc, variables: vars },
81
+ headers: {
82
+ 'Content-Type': 'application/json',
83
+ },
84
+ })
85
+
86
+ return result as R
87
+ }
88
+ }
89
+
90
+ export const $sdk = getSdk(createGraphQLClient('/api/graphql'))`, `${serviceName} ofetch.ts`);
91
+ }
92
+ function generateExternalOfetchClient(nitro, service, endpoint) {
93
+ if (!shouldGenerateClientUtils(nitro)) return;
94
+ const serviceName = service.name;
95
+ const placeholders = {
96
+ ...getDefaultPaths(nitro),
97
+ serviceName
98
+ };
99
+ const clientUtilsConfig = getClientUtilsConfig(nitro);
100
+ const ofetchPath = resolveFilePath(service.paths?.ofetch ?? clientUtilsConfig.ofetch, clientUtilsConfig.enabled, true, "{clientGraphql}/{serviceName}/ofetch.ts", placeholders);
101
+ if (!ofetchPath) return;
102
+ const serviceDir = dirname(ofetchPath);
103
+ if (!existsSync(serviceDir)) mkdirSync(serviceDir, { recursive: true });
104
+ if (!existsSync(ofetchPath)) {
105
+ const capitalizedServiceName = serviceName.charAt(0).toUpperCase() + serviceName.slice(1);
106
+ writeFileIfNotExists(ofetchPath, nitro.options.framework?.name === "nuxt" ? `// This file is auto-generated once by nitro-graphql for quick start
107
+ // You can modify this file according to your needs
108
+ import type { Sdk, Requester } from './sdk'
109
+ import { getSdk } from './sdk'
110
+
111
+ export function create${capitalizedServiceName}GraphQLClient(endpoint: string = '${endpoint}'): Requester {
112
+ return async <R>(doc: string, vars?: any): Promise<R> => {
113
+ const headers = import.meta.server ? useRequestHeaders() : undefined
114
+
115
+ const result = await $fetch(endpoint, {
116
+ method: 'POST',
117
+ body: { query: doc, variables: vars },
118
+ headers: {
119
+ 'Content-Type': 'application/json',
120
+ ...headers,
121
+ },
122
+ })
123
+
124
+ return result as R
125
+ }
126
+ }
127
+
128
+ export const $${serviceName}Sdk: Sdk = getSdk(create${capitalizedServiceName}GraphQLClient())` : `// This file is auto-generated once by nitro-graphql for quick start
129
+ // You can modify this file according to your needs
130
+ import type { Sdk, Requester } from './sdk'
131
+ import { ofetch } from 'ofetch'
132
+ import { getSdk } from './sdk'
133
+
134
+ export function create${capitalizedServiceName}GraphQLClient(endpoint: string = '${endpoint}'): Requester {
135
+ return async <R>(doc: string, vars?: any): Promise<R> => {
136
+ const result = await ofetch(endpoint, {
137
+ method: 'POST',
138
+ body: { query: doc, variables: vars },
139
+ headers: {
140
+ 'Content-Type': 'application/json',
141
+ },
142
+ })
143
+
144
+ return result as R
145
+ }
146
+ }
147
+
148
+ export const $${serviceName}Sdk: Sdk = getSdk(create${capitalizedServiceName}GraphQLClient())`, `${serviceName} external ofetch.ts`);
149
+ }
61
150
  }
62
151
  /**
63
152
  * Check for duplicate type definitions using a simpler approach
@@ -151,6 +240,10 @@ function validateNoDuplicateTypes(schemas, schemaStrings) {
151
240
  }
152
241
  async function serverTypeGeneration(app) {
153
242
  try {
243
+ if (!shouldGenerateTypes(app)) {
244
+ consola.debug("[nitro-graphql] Server type generation is disabled");
245
+ return;
246
+ }
154
247
  const schemas = app.scanSchemas || [];
155
248
  if (!schemas.length) {
156
249
  consola.info("No GraphQL definitions found for server type generation.");
@@ -170,9 +263,14 @@ async function serverTypeGeneration(app) {
170
263
  const schemaPath = resolve(app.graphql.buildDir, "schema.graphql");
171
264
  mkdirSync(dirname(schemaPath), { recursive: true });
172
265
  writeFileSync(schemaPath, printSchema, "utf-8");
173
- const serverTypesPath = resolve(app.options.buildDir, "types", "nitro-graphql-server.d.ts");
174
- mkdirSync(dirname(serverTypesPath), { recursive: true });
175
- writeFileSync(serverTypesPath, data, "utf-8");
266
+ const placeholders = getDefaultPaths(app);
267
+ const typesConfig = getTypesConfig(app);
268
+ const serverTypesPath = resolveFilePath(typesConfig.server, typesConfig.enabled, true, "{typesDir}/nitro-graphql-server.d.ts", placeholders);
269
+ if (serverTypesPath) {
270
+ mkdirSync(dirname(serverTypesPath), { recursive: true });
271
+ writeFileSync(serverTypesPath, data, "utf-8");
272
+ consola.success(`[nitro-graphql] Generated server types at: ${serverTypesPath}`);
273
+ }
176
274
  } catch (error) {
177
275
  consola.error("Server schema generation error:", error);
178
276
  }
@@ -225,20 +323,24 @@ async function generateMainClientTypes(nitro) {
225
323
  const graphqlString = readFileSync(schemaFilePath, "utf-8");
226
324
  const types = await generateClientTypes(nitro.options.graphql?.federation?.enabled === true ? buildSubgraphSchema([{ typeDefs: parse(graphqlString) }]) : buildSchema(graphqlString), loadDocs, nitro.options.graphql?.codegen?.client ?? {}, nitro.options.graphql?.codegen?.clientSDK ?? {});
227
325
  if (types === false) return;
228
- const clientTypesPath = resolve(nitro.options.buildDir, "types", "nitro-graphql-client.d.ts");
229
- const defaultServiceDir = resolve(nitro.graphql.clientDir, "default");
230
- const sdkTypesPath = resolve(defaultServiceDir, "sdk.ts");
231
- mkdirSync(dirname(clientTypesPath), { recursive: true });
232
- writeFileSync(clientTypesPath, types.types, "utf-8");
233
- mkdirSync(defaultServiceDir, { recursive: true });
234
- let shouldWriteSdk = true;
235
- if (existsSync(sdkTypesPath)) shouldWriteSdk = readFileSync(sdkTypesPath, "utf-8") !== types.sdk;
236
- if (shouldWriteSdk) writeFileSync(sdkTypesPath, types.sdk, "utf-8");
237
- if (nitro.options.framework?.name === "nuxt") {
238
- generateOfetchClient(nitro.graphql.clientDir, "default", "/api/graphql", true);
239
- const externalServices = nitro.options.graphql?.externalServices || [];
240
- generateGraphQLIndexFile(nitro.graphql.clientDir, externalServices);
326
+ const placeholders = getDefaultPaths(nitro);
327
+ const typesConfig = getTypesConfig(nitro);
328
+ const sdkConfig = getSdkConfig(nitro);
329
+ const clientTypesPath = resolveFilePath(typesConfig.client, typesConfig.enabled, true, "{typesDir}/nitro-graphql-client.d.ts", placeholders);
330
+ if (clientTypesPath) {
331
+ mkdirSync(dirname(clientTypesPath), { recursive: true });
332
+ writeFileSync(clientTypesPath, types.types, "utf-8");
333
+ consola.success(`[nitro-graphql] Generated client types at: ${clientTypesPath}`);
334
+ }
335
+ const sdkPath = resolveFilePath(sdkConfig.main, sdkConfig.enabled, true, "{clientGraphql}/default/sdk.ts", placeholders);
336
+ if (sdkPath) {
337
+ mkdirSync(dirname(sdkPath), { recursive: true });
338
+ writeFileSync(sdkPath, types.sdk, "utf-8");
339
+ consola.success(`[nitro-graphql] Generated SDK at: ${sdkPath}`);
241
340
  }
341
+ generateNuxtOfetchClient(nitro, nitro.graphql.clientDir, "default");
342
+ const externalServices = nitro.options.graphql?.externalServices || [];
343
+ if (externalServices.length > 0) generateGraphQLIndexFile(nitro, nitro.graphql.clientDir, externalServices);
242
344
  }
243
345
  async function generateExternalServicesTypes(nitro) {
244
346
  const externalServices = nitro.options.graphql?.externalServices || [];
@@ -267,16 +369,25 @@ async function generateExternalServicesTypes(nitro) {
267
369
  consola.warn(`[graphql:${service.name}] Type generation failed`);
268
370
  continue;
269
371
  }
270
- const serviceTypesPath = resolve(nitro.options.buildDir, "types", `nitro-graphql-client-${service.name}.d.ts`);
271
- const serviceDir = resolve(nitro.graphql.clientDir, service.name);
272
- const serviceSdkPath = resolve(serviceDir, "sdk.ts");
273
- mkdirSync(dirname(serviceTypesPath), { recursive: true });
274
- writeFileSync(serviceTypesPath, types.types, "utf-8");
275
- mkdirSync(serviceDir, { recursive: true });
276
- let shouldWriteServiceSdk = true;
277
- if (existsSync(serviceSdkPath)) shouldWriteServiceSdk = readFileSync(serviceSdkPath, "utf-8") !== types.sdk;
278
- if (shouldWriteServiceSdk) writeFileSync(serviceSdkPath, types.sdk, "utf-8");
279
- if (nitro.options.framework?.name === "nuxt") generateOfetchClient(nitro.graphql.clientDir, service.name, service.endpoint, false);
372
+ const placeholders = {
373
+ ...getDefaultPaths(nitro),
374
+ serviceName: service.name
375
+ };
376
+ const typesConfig = getTypesConfig(nitro);
377
+ const sdkConfig = getSdkConfig(nitro);
378
+ const serviceTypesPath = resolveFilePath(service.paths?.types ?? typesConfig.external, typesConfig.enabled, true, "{typesDir}/nitro-graphql-client-{serviceName}.d.ts", placeholders);
379
+ if (serviceTypesPath) {
380
+ mkdirSync(dirname(serviceTypesPath), { recursive: true });
381
+ writeFileSync(serviceTypesPath, types.types, "utf-8");
382
+ consola.success(`[graphql:${service.name}] Generated types at: ${serviceTypesPath}`);
383
+ }
384
+ const serviceSdkPath = resolveFilePath(service.paths?.sdk ?? sdkConfig.external, sdkConfig.enabled, true, "{clientGraphql}/{serviceName}/sdk.ts", placeholders);
385
+ if (serviceSdkPath) {
386
+ mkdirSync(dirname(serviceSdkPath), { recursive: true });
387
+ writeFileSync(serviceSdkPath, types.sdk, "utf-8");
388
+ consola.success(`[graphql:${service.name}] Generated SDK at: ${serviceSdkPath}`);
389
+ }
390
+ generateExternalOfetchClient(nitro, service, service.endpoint);
280
391
  consola.success(`[graphql:${service.name}] External service types generated successfully`);
281
392
  } catch (error) {
282
393
  consola.error(`[graphql:${service.name}] External service generation failed:`, error);
@@ -0,0 +1,9 @@
1
+ //#region src/virtual/debug-info.d.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/debug-info
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ declare const debugInfo: Record<string, any>;
8
+ //#endregion
9
+ export { debugInfo };
@@ -0,0 +1,26 @@
1
+ //#region src/virtual/debug-info.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/debug-info
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ const debugInfo = {
8
+ isDev: false,
9
+ framework: "",
10
+ graphqlFramework: "",
11
+ federation: {},
12
+ scanned: {
13
+ schemas: 0,
14
+ schemaFiles: [],
15
+ resolvers: 0,
16
+ resolverFiles: [],
17
+ directives: 0,
18
+ directiveFiles: [],
19
+ documents: 0,
20
+ documentFiles: []
21
+ },
22
+ virtualModules: {}
23
+ };
24
+
25
+ //#endregion
26
+ export { debugInfo };
@@ -0,0 +1,9 @@
1
+ //#region src/virtual/graphql-config.d.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/graphql-config
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ declare const importedConfig: {};
8
+ //#endregion
9
+ export { importedConfig };
@@ -0,0 +1,10 @@
1
+ //#region src/virtual/graphql-config.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/graphql-config
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ const importedConfig = {};
8
+
9
+ //#endregion
10
+ export { importedConfig };
@@ -0,0 +1,9 @@
1
+ //#region src/virtual/module-config.d.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/module-config
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ declare const moduleConfig: Record<string, any>;
8
+ //#endregion
9
+ export { moduleConfig };
@@ -0,0 +1,10 @@
1
+ //#region src/virtual/module-config.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/module-config
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ const moduleConfig = {};
8
+
9
+ //#endregion
10
+ export { moduleConfig };
@@ -0,0 +1,11 @@
1
+ //#region src/virtual/server-directives.d.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/server-directives
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ declare const directives: Array<{
8
+ directive: any;
9
+ }>;
10
+ //#endregion
11
+ export { directives };
@@ -0,0 +1,10 @@
1
+ //#region src/virtual/server-directives.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/server-directives
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ const directives = [];
8
+
9
+ //#endregion
10
+ export { directives };
@@ -0,0 +1,11 @@
1
+ //#region src/virtual/server-resolvers.d.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/server-resolvers
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ declare const resolvers: Array<{
8
+ resolver: any;
9
+ }>;
10
+ //#endregion
11
+ export { resolvers };
@@ -0,0 +1,10 @@
1
+ //#region src/virtual/server-resolvers.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/server-resolvers
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ const resolvers = [];
8
+
9
+ //#endregion
10
+ export { resolvers };
@@ -0,0 +1,11 @@
1
+ //#region src/virtual/server-schemas.d.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/server-schemas
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ declare const schemas: Array<{
8
+ def: string;
9
+ }>;
10
+ //#endregion
11
+ export { schemas };
@@ -0,0 +1,10 @@
1
+ //#region src/virtual/server-schemas.ts
2
+ /**
3
+ * Virtual module stub for #nitro-graphql/server-schemas
4
+ * This file is only used during build/bundling to prevent import resolution errors.
5
+ * At runtime, Nitro will override this with the actual virtual module.
6
+ */
7
+ const schemas = [];
8
+
9
+ //#endregion
10
+ export { schemas };
@@ -0,0 +1,27 @@
1
+ import { NitroGraphQLOptions } from "./types/index.mjs";
2
+ import { Plugin } from "vite";
3
+
4
+ //#region src/vite.d.ts
5
+
6
+ /**
7
+ * Vite plugin to load GraphQL files as strings AND auto-register Nitro module
8
+ * This prevents Vite from trying to parse .graphql/.gql files as JavaScript
9
+ * and automatically sets up the nitro-graphql module via the nitro: hook
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { defineConfig } from 'vite'
14
+ * import { nitro } from 'nitro/vite'
15
+ * import { graphql } from 'nitro-graphql/vite'
16
+ *
17
+ * export default defineConfig({
18
+ * plugins: [
19
+ * graphql({ framework: 'graphql-yoga' }), // Auto-registers Nitro module
20
+ * nitro()
21
+ * ]
22
+ * })
23
+ * ```
24
+ */
25
+ declare function graphql(options?: NitroGraphQLOptions): Plugin;
26
+ //#endregion
27
+ export { graphql };
package/dist/vite.mjs ADDED
@@ -0,0 +1,49 @@
1
+ import { setupNitroGraphQL } from "./setup.mjs";
2
+ import defu from "defu";
3
+ import { readFile } from "node:fs/promises";
4
+
5
+ //#region src/vite.ts
6
+ /**
7
+ * Vite plugin to load GraphQL files as strings AND auto-register Nitro module
8
+ * This prevents Vite from trying to parse .graphql/.gql files as JavaScript
9
+ * and automatically sets up the nitro-graphql module via the nitro: hook
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * import { defineConfig } from 'vite'
14
+ * import { nitro } from 'nitro/vite'
15
+ * import { graphql } from 'nitro-graphql/vite'
16
+ *
17
+ * export default defineConfig({
18
+ * plugins: [
19
+ * graphql({ framework: 'graphql-yoga' }), // Auto-registers Nitro module
20
+ * nitro()
21
+ * ]
22
+ * })
23
+ * ```
24
+ */
25
+ function graphql(options) {
26
+ return {
27
+ name: "nitro-graphql:vite",
28
+ enforce: "pre",
29
+ async load(id) {
30
+ if (!/\.(?:graphql|gql)$/i.test(id)) return null;
31
+ try {
32
+ const content = await readFile(id, "utf-8");
33
+ return `export default ${JSON.stringify(content)}`;
34
+ } catch (error) {
35
+ if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return null;
36
+ throw error;
37
+ }
38
+ },
39
+ nitro: { async setup(nitro) {
40
+ if (options) nitro.options.graphql = defu(nitro.options.graphql || {}, options);
41
+ nitro.options.graphql = nitro.options.graphql || {};
42
+ nitro.options.graphql._vitePlugin = true;
43
+ await setupNitroGraphQL(nitro);
44
+ } }
45
+ };
46
+ }
47
+
48
+ //#endregion
49
+ export { graphql };