nitro-graphql 1.4.3 → 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 || {};
@@ -49,66 +50,128 @@ async function rollupConfig(app) {
49
50
  }
50
51
  function virtualSchemas(app) {
51
52
  const getSchemas = () => {
52
- const schemas = [...app.scanSchemas, ...app.options.graphql?.typedefs ?? []];
53
- return schemas;
53
+ return [...app.scanSchemas, ...app.options.graphql?.typedefs ?? []];
54
54
  };
55
55
  app.options.virtual ??= {};
56
56
  app.options.virtual["#nitro-internal-virtual/server-schemas"] = () => {
57
- const imports = getSchemas();
58
- const code = `
59
- ${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")}
60
67
 
61
68
  export const schemas = [
62
- ${imports.map((h) => `{ def: ${getImportId(h)} }`).join(",\n")}
69
+ ${schemaArray.join(",\n")}
63
70
  ];
64
71
  `;
65
- return code;
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
+ }
66
78
  };
67
79
  }
68
80
  function virtualResolvers(app) {
69
81
  const getResolvers = () => {
70
- const resolvers = [...app.scanResolvers];
71
- return resolvers;
82
+ return [...app.scanResolvers];
72
83
  };
73
84
  app.options.virtual ??= {};
74
85
  app.options.virtual["#nitro-internal-virtual/server-resolvers"] = () => {
75
- const imports = getResolvers();
76
- const importsContent = [...imports.map(({ specifier, imports: imports$1, options }) => {
77
- return genImport(specifier, imports$1, options);
78
- })];
79
- const data = imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ resolver: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n");
80
- const content = [
81
- "export const resolvers = [",
82
- data,
83
- "]",
84
- ""
85
- ];
86
- content.unshift(...importsContent);
87
- const code = content.join("\n");
88
- return code;
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
+ }
89
128
  };
90
129
  }
91
130
  function virtualDirectives(app) {
92
131
  const getDirectives = () => {
93
- const directives = [...app.scanDirectives || []];
94
- return directives;
132
+ return [...app.scanDirectives || []];
95
133
  };
96
134
  app.options.virtual ??= {};
97
135
  app.options.virtual["#nitro-internal-virtual/server-directives"] = () => {
98
- const imports = getDirectives();
99
- const importsContent = [...imports.map(({ specifier, imports: imports$1, options }) => {
100
- return genImport(specifier, imports$1, options);
101
- })];
102
- const data = imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ directive: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n");
103
- const content = [
104
- "export const directives = [",
105
- data,
106
- "]",
107
- ""
108
- ];
109
- content.unshift(...importsContent);
110
- const code = content.join("\n");
111
- return code;
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
+ }
112
175
  };
113
176
  }
114
177
  function getGraphQLConfig(app) {
@@ -128,6 +191,60 @@ function virtualModuleConfig(app) {
128
191
  return `export const moduleConfig = ${JSON.stringify(moduleConfig, null, 2)};`;
129
192
  };
130
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
+ }
131
248
 
132
249
  //#endregion
133
250
  export { rollupConfig };
@@ -1,6 +1,6 @@
1
- import * as h30 from "h3";
1
+ import * as h31 from "h3";
2
2
 
3
3
  //#region src/routes/apollo-server.d.ts
4
- declare const _default: h30.EventHandler<h30.EventHandlerRequest, Promise<any>>;
4
+ declare const _default: h31.EventHandler<h31.EventHandlerRequest, Promise<any>>;
5
5
  //#endregion
6
6
  export { _default as default };
@@ -18,8 +18,7 @@ let buildSubgraphSchema = null;
18
18
  async function loadFederationSupport() {
19
19
  if (buildSubgraphSchema !== null) return buildSubgraphSchema;
20
20
  try {
21
- const apolloSubgraph = await import("@apollo/subgraph");
22
- buildSubgraphSchema = apolloSubgraph.buildSubgraphSchema;
21
+ buildSubgraphSchema = (await import("@apollo/subgraph")).buildSubgraphSchema;
23
22
  } catch {
24
23
  buildSubgraphSchema = false;
25
24
  }
@@ -27,8 +26,7 @@ async function loadFederationSupport() {
27
26
  }
28
27
  async function createMergedSchema() {
29
28
  try {
30
- const mergedSchemas = schemas.map((schema$1) => schema$1.def).join("\n\n");
31
- const typeDefs = mergeTypeDefs([mergedSchemas], {
29
+ const typeDefs = mergeTypeDefs([schemas.map((schema$1) => schema$1.def).join("\n\n")], {
32
30
  throwOnConflict: true,
33
31
  commentDescriptions: true,
34
32
  sort: true
@@ -38,13 +36,11 @@ async function createMergedSchema() {
38
36
  let schema;
39
37
  if (federationEnabled) {
40
38
  const buildSubgraph = await loadFederationSupport();
41
- if (buildSubgraph) {
42
- const typeDefsDoc = typeof typeDefs === "string" ? parse(typeDefs) : typeDefs;
43
- schema = buildSubgraph({
44
- typeDefs: typeDefsDoc,
45
- resolvers: mergedResolvers
46
- });
47
- } else {
39
+ if (buildSubgraph) schema = buildSubgraph({
40
+ typeDefs: typeof typeDefs === "string" ? parse(typeDefs) : typeDefs,
41
+ resolvers: mergedResolvers
42
+ });
43
+ else {
48
44
  console.warn("Federation enabled but @apollo/subgraph not available, falling back to regular schema");
49
45
  schema = makeExecutableSchema({
50
46
  typeDefs,
@@ -68,9 +64,8 @@ let apolloServer = null;
68
64
  let serverStarted = false;
69
65
  async function createApolloServer() {
70
66
  if (!apolloServer) {
71
- const schema = await createMergedSchema();
72
67
  apolloServer = new ApolloServer(defu({
73
- schema,
68
+ schema: await createMergedSchema(),
74
69
  introspection: true,
75
70
  plugins: [ApolloServerPluginLandingPageLocalDefault({ embed: true })]
76
71
  }, importedConfig));
@@ -84,12 +79,10 @@ async function createApolloServer() {
84
79
  let serverPromise = null;
85
80
  var apollo_server_default = defineEventHandler(async (event) => {
86
81
  if (!serverPromise) serverPromise = createApolloServer();
87
- const server = await serverPromise;
88
- const h3Handler = startServerAndCreateH3Handler(server, {
82
+ return startServerAndCreateH3Handler(await serverPromise, {
89
83
  context: async () => ({ event }),
90
84
  serverAlreadyStarted: true
91
- });
92
- return h3Handler(event);
85
+ })(event);
93
86
  });
94
87
 
95
88
  //#endregion
@@ -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 };