nitro-graphql 1.4.4 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/rollup.js CHANGED
@@ -12,6 +12,7 @@ async function rollupConfig(app) {
12
12
  virtualDirectives(app);
13
13
  getGraphQLConfig(app);
14
14
  virtualModuleConfig(app);
15
+ virtualDebugInfo(app);
15
16
  app.hooks.hook("rollup:before", (nitro, rollupConfig$1) => {
16
17
  rollupConfig$1.plugins = rollupConfig$1.plugins || [];
17
18
  const { include = /\.(graphql|gql)$/i, exclude, validate = false } = app.options.graphql?.loader || {};
@@ -53,14 +54,27 @@ function virtualSchemas(app) {
53
54
  };
54
55
  app.options.virtual ??= {};
55
56
  app.options.virtual["#nitro-internal-virtual/server-schemas"] = () => {
56
- const imports = getSchemas();
57
- return `
58
- ${imports.map((handler) => `import ${getImportId(handler)} from '${handler}';`).join("\n")}
57
+ try {
58
+ const imports = getSchemas();
59
+ if (imports.length === 0) {
60
+ if (app.options.dev) app.logger.warn("[nitro-graphql] No schemas found. Virtual module will export empty array.");
61
+ return "export const schemas = []";
62
+ }
63
+ const importStatements = imports.map((handler) => `import ${getImportId(handler)} from '${handler}';`);
64
+ const schemaArray = imports.map((h) => `{ def: ${getImportId(h)} }`);
65
+ const code = `
66
+ ${importStatements.join("\n")}
59
67
 
60
68
  export const schemas = [
61
- ${imports.map((h) => `{ def: ${getImportId(h)} }`).join(",\n")}
69
+ ${schemaArray.join(",\n")}
62
70
  ];
63
71
  `;
72
+ if (app.options.dev) app.logger.success(`[nitro-graphql] Generated virtual schema module: ${imports.length} schema(s)`);
73
+ return code;
74
+ } catch (error) {
75
+ app.logger.error("[nitro-graphql] Failed to generate virtual schema module:", error);
76
+ return "export const schemas = []";
77
+ }
64
78
  };
65
79
  }
66
80
  function virtualResolvers(app) {
@@ -69,18 +83,48 @@ function virtualResolvers(app) {
69
83
  };
70
84
  app.options.virtual ??= {};
71
85
  app.options.virtual["#nitro-internal-virtual/server-resolvers"] = () => {
72
- const imports = getResolvers();
73
- const importsContent = [...imports.map(({ specifier, imports: imports$1, options }) => {
74
- return genImport(specifier, imports$1, options);
75
- })];
76
- const content = [
77
- "export const resolvers = [",
78
- imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ resolver: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n"),
79
- "]",
80
- ""
81
- ];
82
- content.unshift(...importsContent);
83
- return content.join("\n");
86
+ try {
87
+ const imports = getResolvers();
88
+ if (imports.length === 0) {
89
+ if (app.options.dev) app.logger.warn("[nitro-graphql] No resolvers found. Virtual module will export empty array.");
90
+ return "export const resolvers = []";
91
+ }
92
+ const importsContent = [];
93
+ const invalidImports = [];
94
+ for (const { specifier, imports: importList, options } of imports) try {
95
+ if (!importList || importList.length === 0) {
96
+ invalidImports.push(`${specifier}: No exports found`);
97
+ continue;
98
+ }
99
+ const importCode = genImport(specifier, importList, options);
100
+ importsContent.push(importCode);
101
+ } catch (error) {
102
+ const message = error instanceof Error ? error.message : String(error);
103
+ invalidImports.push(`${specifier}: ${message}`);
104
+ if (app.options.dev) app.logger.error(`[nitro-graphql] Failed to generate import for ${specifier}:`, error);
105
+ }
106
+ if (invalidImports.length > 0 && app.options.dev) {
107
+ app.logger.warn("[nitro-graphql] Some resolver imports could not be generated:");
108
+ for (const msg of invalidImports) app.logger.warn(` - ${msg}`);
109
+ }
110
+ const data = imports.map(({ imports: importList }) => importList.map((i) => `{ resolver: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n");
111
+ const code = [
112
+ ...importsContent,
113
+ "",
114
+ "export const resolvers = [",
115
+ data,
116
+ "]",
117
+ ""
118
+ ].join("\n");
119
+ if (app.options.dev) {
120
+ const totalExports = imports.reduce((sum, r) => sum + r.imports.length, 0);
121
+ app.logger.success(`[nitro-graphql] Generated virtual resolver module: ${totalExports} export(s) from ${imports.length} file(s)`);
122
+ }
123
+ return code;
124
+ } catch (error) {
125
+ app.logger.error("[nitro-graphql] Failed to generate virtual resolver module:", error);
126
+ return "export const resolvers = []";
127
+ }
84
128
  };
85
129
  }
86
130
  function virtualDirectives(app) {
@@ -89,18 +133,45 @@ function virtualDirectives(app) {
89
133
  };
90
134
  app.options.virtual ??= {};
91
135
  app.options.virtual["#nitro-internal-virtual/server-directives"] = () => {
92
- const imports = getDirectives();
93
- const importsContent = [...imports.map(({ specifier, imports: imports$1, options }) => {
94
- return genImport(specifier, imports$1, options);
95
- })];
96
- const content = [
97
- "export const directives = [",
98
- imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ directive: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n"),
99
- "]",
100
- ""
101
- ];
102
- content.unshift(...importsContent);
103
- return content.join("\n");
136
+ try {
137
+ const imports = getDirectives();
138
+ if (imports.length === 0) return "export const directives = []";
139
+ const importsContent = [];
140
+ const invalidImports = [];
141
+ for (const { specifier, imports: importList, options } of imports) try {
142
+ if (!importList || importList.length === 0) {
143
+ invalidImports.push(`${specifier}: No exports found`);
144
+ continue;
145
+ }
146
+ const importCode = genImport(specifier, importList, options);
147
+ importsContent.push(importCode);
148
+ } catch (error) {
149
+ const message = error instanceof Error ? error.message : String(error);
150
+ invalidImports.push(`${specifier}: ${message}`);
151
+ if (app.options.dev) app.logger.error(`[nitro-graphql] Failed to generate import for directive ${specifier}:`, error);
152
+ }
153
+ if (invalidImports.length > 0 && app.options.dev) {
154
+ app.logger.warn("[nitro-graphql] Some directive imports could not be generated:");
155
+ for (const msg of invalidImports) app.logger.warn(` - ${msg}`);
156
+ }
157
+ const data = imports.map(({ imports: importList }) => importList.map((i) => `{ directive: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n");
158
+ const code = [
159
+ ...importsContent,
160
+ "",
161
+ "export const directives = [",
162
+ data,
163
+ "]",
164
+ ""
165
+ ].join("\n");
166
+ if (app.options.dev) {
167
+ const totalExports = imports.reduce((sum, d) => sum + d.imports.length, 0);
168
+ app.logger.success(`[nitro-graphql] Generated virtual directive module: ${totalExports} directive(s) from ${imports.length} file(s)`);
169
+ }
170
+ return code;
171
+ } catch (error) {
172
+ app.logger.error("[nitro-graphql] Failed to generate virtual directive module:", error);
173
+ return "export const directives = []";
174
+ }
104
175
  };
105
176
  }
106
177
  function getGraphQLConfig(app) {
@@ -120,6 +191,60 @@ function virtualModuleConfig(app) {
120
191
  return `export const moduleConfig = ${JSON.stringify(moduleConfig, null, 2)};`;
121
192
  };
122
193
  }
194
+ function virtualDebugInfo(app) {
195
+ app.options.virtual ??= {};
196
+ app.options.virtual["#nitro-internal-virtual/debug-info"] = () => {
197
+ const virtualModuleCodes = {};
198
+ try {
199
+ const schemasGenerator = app.options.virtual["#nitro-internal-virtual/server-schemas"];
200
+ if (schemasGenerator && typeof schemasGenerator === "function") virtualModuleCodes["server-schemas"] = schemasGenerator();
201
+ } catch (error) {
202
+ virtualModuleCodes["server-schemas"] = `// Error generating: ${error instanceof Error ? error.message : String(error)}`;
203
+ }
204
+ try {
205
+ const resolversGenerator = app.options.virtual["#nitro-internal-virtual/server-resolvers"];
206
+ if (resolversGenerator && typeof resolversGenerator === "function") virtualModuleCodes["server-resolvers"] = resolversGenerator();
207
+ } catch (error) {
208
+ virtualModuleCodes["server-resolvers"] = `// Error generating: ${error instanceof Error ? error.message : String(error)}`;
209
+ }
210
+ try {
211
+ const directivesGenerator = app.options.virtual["#nitro-internal-virtual/server-directives"];
212
+ if (directivesGenerator && typeof directivesGenerator === "function") virtualModuleCodes["server-directives"] = directivesGenerator();
213
+ } catch (error) {
214
+ virtualModuleCodes["server-directives"] = `// Error generating: ${error instanceof Error ? error.message : String(error)}`;
215
+ }
216
+ try {
217
+ const moduleConfigGenerator = app.options.virtual["#nitro-internal-virtual/module-config"];
218
+ if (moduleConfigGenerator && typeof moduleConfigGenerator === "function") virtualModuleCodes["module-config"] = moduleConfigGenerator();
219
+ } catch (error) {
220
+ virtualModuleCodes["module-config"] = `// Error generating: ${error instanceof Error ? error.message : String(error)}`;
221
+ }
222
+ try {
223
+ const graphqlConfigGenerator = app.options.virtual["#nitro-internal-virtual/graphql-config"];
224
+ if (graphqlConfigGenerator && typeof graphqlConfigGenerator === "function") virtualModuleCodes["graphql-config"] = graphqlConfigGenerator();
225
+ } catch (error) {
226
+ virtualModuleCodes["graphql-config"] = `// Error generating: ${error instanceof Error ? error.message : String(error)}`;
227
+ }
228
+ const debugInfo = {
229
+ isDev: app.options.dev,
230
+ framework: app.options.framework.name,
231
+ graphqlFramework: app.options.graphql?.framework,
232
+ federation: app.options.graphql?.federation,
233
+ scanned: {
234
+ schemas: app.scanSchemas?.length || 0,
235
+ schemaFiles: app.scanSchemas || [],
236
+ resolvers: app.scanResolvers?.length || 0,
237
+ resolverFiles: app.scanResolvers || [],
238
+ directives: app.scanDirectives?.length || 0,
239
+ directiveFiles: app.scanDirectives || [],
240
+ documents: app.scanDocuments?.length || 0,
241
+ documentFiles: app.scanDocuments || []
242
+ },
243
+ virtualModules: virtualModuleCodes
244
+ };
245
+ return `export const debugInfo = ${JSON.stringify(debugInfo, null, 2)};`;
246
+ };
247
+ }
123
248
 
124
249
  //#endregion
125
250
  export { rollupConfig };
@@ -26,8 +26,7 @@ async function loadFederationSupport() {
26
26
  }
27
27
  async function createMergedSchema() {
28
28
  try {
29
- const mergedSchemas = schemas.map((schema$1) => schema$1.def).join("\n\n");
30
- const typeDefs = mergeTypeDefs([mergedSchemas], {
29
+ const typeDefs = mergeTypeDefs([schemas.map((schema$1) => schema$1.def).join("\n\n")], {
31
30
  throwOnConflict: true,
32
31
  commentDescriptions: true,
33
32
  sort: true
@@ -37,13 +36,11 @@ async function createMergedSchema() {
37
36
  let schema;
38
37
  if (federationEnabled) {
39
38
  const buildSubgraph = await loadFederationSupport();
40
- if (buildSubgraph) {
41
- const typeDefsDoc = typeof typeDefs === "string" ? parse(typeDefs) : typeDefs;
42
- schema = buildSubgraph({
43
- typeDefs: typeDefsDoc,
44
- resolvers: mergedResolvers
45
- });
46
- } else {
39
+ if (buildSubgraph) schema = buildSubgraph({
40
+ typeDefs: typeof typeDefs === "string" ? parse(typeDefs) : typeDefs,
41
+ resolvers: mergedResolvers
42
+ });
43
+ else {
47
44
  console.warn("Federation enabled but @apollo/subgraph not available, falling back to regular schema");
48
45
  schema = makeExecutableSchema({
49
46
  typeDefs,
@@ -67,9 +64,8 @@ let apolloServer = null;
67
64
  let serverStarted = false;
68
65
  async function createApolloServer() {
69
66
  if (!apolloServer) {
70
- const schema = await createMergedSchema();
71
67
  apolloServer = new ApolloServer(defu({
72
- schema,
68
+ schema: await createMergedSchema(),
73
69
  introspection: true,
74
70
  plugins: [ApolloServerPluginLandingPageLocalDefault({ embed: true })]
75
71
  }, importedConfig));
@@ -83,8 +79,7 @@ async function createApolloServer() {
83
79
  let serverPromise = null;
84
80
  var apollo_server_default = defineEventHandler(async (event) => {
85
81
  if (!serverPromise) serverPromise = createApolloServer();
86
- const server = await serverPromise;
87
- return startServerAndCreateH3Handler(server, {
82
+ return startServerAndCreateH3Handler(await serverPromise, {
88
83
  context: async () => ({ event }),
89
84
  serverAlreadyStarted: true
90
85
  })(event);
@@ -0,0 +1,61 @@
1
+ import * as h33 from "h3";
2
+
3
+ //#region src/routes/debug.d.ts
4
+
5
+ /**
6
+ * Debug endpoint for inspecting virtual modules and GraphQL setup
7
+ * Only available in development mode
8
+ *
9
+ * Routes:
10
+ * - /_nitro/graphql/debug - HTML dashboard
11
+ * - /_nitro/graphql/debug?format=json - JSON API
12
+ */
13
+ declare const _default: h33.EventHandler<h33.EventHandlerRequest, Promise<string | {
14
+ timestamp: string;
15
+ environment: {
16
+ dev: any;
17
+ framework: any;
18
+ };
19
+ graphql: {
20
+ framework: any;
21
+ federation: any;
22
+ };
23
+ scanned: {
24
+ schemas: any;
25
+ schemaFiles: any;
26
+ resolvers: any;
27
+ resolverFiles: any;
28
+ directives: any;
29
+ directiveFiles: any;
30
+ documents: any;
31
+ documentFiles: any;
32
+ };
33
+ runtime: {
34
+ loadedResolvers: number;
35
+ loadedSchemas: number;
36
+ loadedDirectives: any;
37
+ };
38
+ virtualModules: any;
39
+ virtualModuleSamples: {
40
+ 'server-resolvers': {
41
+ resolverCount: number;
42
+ sample: {
43
+ hasResolver: boolean;
44
+ resolverKeys: string[];
45
+ }[];
46
+ };
47
+ 'server-schemas': {
48
+ schemaCount: number;
49
+ sample: {
50
+ defLength: number;
51
+ defPreview: string;
52
+ }[];
53
+ };
54
+ 'server-directives': {
55
+ directiveCount: any;
56
+ };
57
+ 'module-config': any;
58
+ };
59
+ }>>;
60
+ //#endregion
61
+ export { _default as default };