nitro-graphql 2.0.0-beta.24 → 2.0.0-beta.26
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/README.md +127 -520
- package/dist/define.d.mts +16 -14
- package/dist/define.mjs +16 -14
- package/dist/rollup.mjs +2 -2
- package/dist/routes/apollo-server.d.mts +2 -2
- package/dist/routes/graphql-yoga.mjs +4 -0
- package/dist/routes/health.d.mts +2 -2
- package/dist/setup.mjs +37 -46
- package/dist/types/define.d.mts +12 -1
- package/dist/types/define.mjs +1 -0
- package/dist/types/index.d.mts +1 -1
- package/dist/types/index.mjs +1 -0
- package/dist/types/standard-schema.mjs +1 -0
- package/dist/utils/client-codegen.d.mts +4 -1
- package/dist/utils/client-codegen.mjs +2 -2
- package/dist/utils/errors.d.mts +73 -0
- package/dist/utils/errors.mjs +89 -0
- package/dist/utils/index.d.mts +2 -1
- package/dist/utils/index.mjs +16 -5
- package/dist/utils/server-codegen.mjs +1 -1
- package/dist/utils/type-generation.d.mts +7 -2
- package/dist/utils/type-generation.mjs +20 -19
- package/package.json +5 -1
package/dist/utils/index.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { directiveParser, generateDirectiveSchema, generateDirectiveSchemas } from "./directive-parser.mjs";
|
|
2
|
+
import { createDefaultMaskError } from "./errors.mjs";
|
|
2
3
|
import { readFile } from "node:fs/promises";
|
|
3
|
-
import { join, relative } from "pathe";
|
|
4
|
+
import { basename, join, relative } from "pathe";
|
|
4
5
|
import { hash } from "ohash";
|
|
5
6
|
import { parseAsync } from "oxc-parser";
|
|
6
7
|
import { glob } from "tinyglobby";
|
|
@@ -76,6 +77,17 @@ async function scanResolvers(nitro) {
|
|
|
76
77
|
for (const file of files) try {
|
|
77
78
|
const fileContent = await readFile(file.fullPath, "utf-8");
|
|
78
79
|
const parsed = await parseAsync(file.fullPath, fileContent);
|
|
80
|
+
if (parsed.errors && parsed.errors.length > 0) {
|
|
81
|
+
if (nitro.options.dev) {
|
|
82
|
+
const fileName = basename(file.fullPath);
|
|
83
|
+
const firstError = parsed.errors[0];
|
|
84
|
+
const location = firstError.labels?.[0];
|
|
85
|
+
const lineInfo = location ? `:${location.start}` : "";
|
|
86
|
+
const message = firstError.message.split(",")[0];
|
|
87
|
+
console.error(`✖ ${fileName}${lineInfo} - ${message}`);
|
|
88
|
+
}
|
|
89
|
+
continue;
|
|
90
|
+
}
|
|
79
91
|
const exports = {
|
|
80
92
|
imports: [],
|
|
81
93
|
specifier: file.fullPath
|
|
@@ -125,7 +137,6 @@ async function scanResolvers(nitro) {
|
|
|
125
137
|
}
|
|
126
138
|
}
|
|
127
139
|
if (nitro.options.dev) {
|
|
128
|
-
const relPath = relative(nitro.options.rootDir, file.fullPath);
|
|
129
140
|
if (hasDefaultExport && !hasNamedExport) nitro.logger.warn(`[nitro-graphql] ${relPath}: Using default export instead of named export. Resolvers must use named exports like "export const myResolver = defineQuery(...)". Default exports are not detected.`);
|
|
130
141
|
if (exports.imports.length === 0 && hasNamedExport) {
|
|
131
142
|
const validFunctions = VALID_DEFINE_FUNCTIONS.join(", ");
|
|
@@ -135,8 +146,8 @@ async function scanResolvers(nitro) {
|
|
|
135
146
|
}
|
|
136
147
|
if (exports.imports.length > 0) exportName.push(exports);
|
|
137
148
|
} catch (error) {
|
|
138
|
-
const relPath = relative(nitro.options.rootDir, file.fullPath);
|
|
139
|
-
nitro.logger.error(`[nitro-graphql] Failed to parse resolver file ${relPath}:`, error);
|
|
149
|
+
const relPath$1 = relative(nitro.options.rootDir, file.fullPath);
|
|
150
|
+
nitro.logger.error(`[nitro-graphql] Failed to parse resolver file ${relPath$1}:`, error);
|
|
140
151
|
}
|
|
141
152
|
return exportName;
|
|
142
153
|
}
|
|
@@ -275,4 +286,4 @@ async function scanDir(nitro, dir, name, globPattern = GLOB_SCAN_PATTERN) {
|
|
|
275
286
|
}
|
|
276
287
|
|
|
277
288
|
//#endregion
|
|
278
|
-
export { GLOB_SCAN_PATTERN, directiveParser, generateDirectiveSchema, generateDirectiveSchemas, generateLayerIgnorePatterns, getImportId, getLayerAppDirectories, getLayerDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives, scanDocs, scanExternalServiceDocs, scanGraphql, scanResolvers, scanSchemas, validateExternalServices };
|
|
289
|
+
export { GLOB_SCAN_PATTERN, createDefaultMaskError, directiveParser, generateDirectiveSchema, generateDirectiveSchemas, generateLayerIgnorePatterns, getImportId, getLayerAppDirectories, getLayerDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives, scanDocs, scanExternalServiceDocs, scanGraphql, scanResolvers, scanSchemas, validateExternalServices };
|
|
@@ -61,7 +61,7 @@ async function generateTypes(selectFremework, schema, config = {}, outputPath) {
|
|
|
61
61
|
return {
|
|
62
62
|
prepend: [
|
|
63
63
|
`import schemas from '#graphql/schema'`,
|
|
64
|
-
`import type { StandardSchemaV1 } from 'nitro-graphql'`,
|
|
64
|
+
`import type { StandardSchemaV1 } from 'nitro-graphql/types'`,
|
|
65
65
|
`
|
|
66
66
|
export interface NPMConfig {
|
|
67
67
|
framework: '${selectFremework || "graphql-yoga"}';
|
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import { Nitro } from "nitro/types";
|
|
2
2
|
|
|
3
3
|
//#region src/utils/type-generation.d.ts
|
|
4
|
-
declare function serverTypeGeneration(app: Nitro
|
|
5
|
-
|
|
4
|
+
declare function serverTypeGeneration(app: Nitro, options?: {
|
|
5
|
+
silent?: boolean;
|
|
6
|
+
}): Promise<void>;
|
|
7
|
+
declare function clientTypeGeneration(nitro: Nitro, options?: {
|
|
8
|
+
silent?: boolean;
|
|
9
|
+
isInitial?: boolean;
|
|
10
|
+
}): Promise<void>;
|
|
6
11
|
//#endregion
|
|
7
12
|
export { clientTypeGeneration, serverTypeGeneration };
|
|
@@ -12,6 +12,7 @@ import { mergeTypeDefs } from "@graphql-tools/merge";
|
|
|
12
12
|
import { printSchemaWithDirectives } from "@graphql-tools/utils";
|
|
13
13
|
|
|
14
14
|
//#region src/utils/type-generation.ts
|
|
15
|
+
const logger = consola.withTag("nitro-graphql");
|
|
15
16
|
function generateGraphQLIndexFile(nitro, clientDir, externalServices = []) {
|
|
16
17
|
if (!shouldGenerateClientUtils(nitro)) return;
|
|
17
18
|
const placeholders = getDefaultPaths(nitro);
|
|
@@ -238,15 +239,15 @@ function validateNoDuplicateTypes(schemas, schemaStrings) {
|
|
|
238
239
|
}
|
|
239
240
|
return true;
|
|
240
241
|
}
|
|
241
|
-
async function serverTypeGeneration(app) {
|
|
242
|
+
async function serverTypeGeneration(app, options = {}) {
|
|
242
243
|
try {
|
|
243
244
|
if (!shouldGenerateTypes(app)) {
|
|
244
|
-
|
|
245
|
+
logger.debug("Server type generation is disabled");
|
|
245
246
|
return;
|
|
246
247
|
}
|
|
247
248
|
const schemas = app.scanSchemas || [];
|
|
248
249
|
if (!schemas.length) {
|
|
249
|
-
consola.info("No GraphQL definitions found for server type generation.");
|
|
250
|
+
if (!options.silent) consola.info("No GraphQL definitions found for server type generation.");
|
|
250
251
|
return;
|
|
251
252
|
}
|
|
252
253
|
const schemaStrings = loadFilesSync(schemas).map((schema$1) => typeof schema$1 === "string" ? schema$1 : schema$1.loc?.source?.body || "").filter(Boolean);
|
|
@@ -269,18 +270,18 @@ async function serverTypeGeneration(app) {
|
|
|
269
270
|
if (serverTypesPath) {
|
|
270
271
|
mkdirSync(dirname(serverTypesPath), { recursive: true });
|
|
271
272
|
writeFileSync(serverTypesPath, data, "utf-8");
|
|
272
|
-
|
|
273
|
+
if (!options.silent) logger.success(`Generated server types at: ${serverTypesPath}`);
|
|
273
274
|
}
|
|
274
275
|
} catch (error) {
|
|
275
|
-
|
|
276
|
+
logger.error("Server schema generation error:", error);
|
|
276
277
|
}
|
|
277
278
|
}
|
|
278
|
-
async function clientTypeGeneration(nitro) {
|
|
279
|
+
async function clientTypeGeneration(nitro, options = {}) {
|
|
279
280
|
try {
|
|
280
|
-
if (nitro.scanSchemas && nitro.scanSchemas.length > 0) await generateMainClientTypes(nitro);
|
|
281
|
-
if (nitro.options.graphql?.externalServices?.length) await generateExternalServicesTypes(nitro);
|
|
281
|
+
if (nitro.scanSchemas && nitro.scanSchemas.length > 0) await generateMainClientTypes(nitro, options);
|
|
282
|
+
if (nitro.options.graphql?.externalServices?.length) await generateExternalServicesTypes(nitro, options);
|
|
282
283
|
} catch (error) {
|
|
283
|
-
|
|
284
|
+
logger.error("Client schema generation error:", error);
|
|
284
285
|
}
|
|
285
286
|
}
|
|
286
287
|
/**
|
|
@@ -311,17 +312,17 @@ function checkOldStructure(clientDir) {
|
|
|
311
312
|
🚫 The old files will cause import conflicts until moved!`);
|
|
312
313
|
}
|
|
313
314
|
}
|
|
314
|
-
async function generateMainClientTypes(nitro) {
|
|
315
|
+
async function generateMainClientTypes(nitro, options = {}) {
|
|
315
316
|
checkOldStructure(nitro.graphql.clientDir);
|
|
316
317
|
const docs = nitro.scanDocuments;
|
|
317
318
|
const loadDocs = await loadGraphQLDocuments(docs);
|
|
318
319
|
const schemaFilePath = join(nitro.graphql.buildDir, "schema.graphql");
|
|
319
320
|
if (!existsSync(schemaFilePath)) {
|
|
320
|
-
consola.info("Schema file not ready yet for client type generation. Server types need to be generated first.");
|
|
321
|
+
if (!options.silent) consola.info("Schema file not ready yet for client type generation. Server types need to be generated first.");
|
|
321
322
|
return;
|
|
322
323
|
}
|
|
323
324
|
const graphqlString = readFileSync(schemaFilePath, "utf-8");
|
|
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 ?? {});
|
|
325
|
+
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 ?? {}, void 0, void 0, void 0, options);
|
|
325
326
|
if (types === false) return;
|
|
326
327
|
const placeholders = getDefaultPaths(nitro);
|
|
327
328
|
const typesConfig = getTypesConfig(nitro);
|
|
@@ -330,22 +331,22 @@ async function generateMainClientTypes(nitro) {
|
|
|
330
331
|
if (clientTypesPath) {
|
|
331
332
|
mkdirSync(dirname(clientTypesPath), { recursive: true });
|
|
332
333
|
writeFileSync(clientTypesPath, types.types, "utf-8");
|
|
333
|
-
|
|
334
|
+
if (!options.silent) logger.success(`Generated client types at: ${clientTypesPath}`);
|
|
334
335
|
}
|
|
335
336
|
const sdkPath = resolveFilePath(sdkConfig.main, sdkConfig.enabled, true, "{clientGraphql}/default/sdk.ts", placeholders);
|
|
336
337
|
if (sdkPath) {
|
|
337
338
|
mkdirSync(dirname(sdkPath), { recursive: true });
|
|
338
339
|
writeFileSync(sdkPath, types.sdk, "utf-8");
|
|
339
|
-
|
|
340
|
+
if (!options.silent) logger.success(`Generated SDK at: ${sdkPath}`);
|
|
340
341
|
}
|
|
341
342
|
generateNuxtOfetchClient(nitro, nitro.graphql.clientDir, "default");
|
|
342
343
|
const externalServices = nitro.options.graphql?.externalServices || [];
|
|
343
344
|
if (externalServices.length > 0) generateGraphQLIndexFile(nitro, nitro.graphql.clientDir, externalServices);
|
|
344
345
|
}
|
|
345
|
-
async function generateExternalServicesTypes(nitro) {
|
|
346
|
+
async function generateExternalServicesTypes(nitro, options = {}) {
|
|
346
347
|
const externalServices = nitro.options.graphql?.externalServices || [];
|
|
347
348
|
for (const service of externalServices) try {
|
|
348
|
-
consola.info(`[graphql:${service.name}] Processing external service`);
|
|
349
|
+
if (!options.silent) consola.info(`[graphql:${service.name}] Processing external service`);
|
|
349
350
|
await downloadAndSaveSchema(service, nitro.options.buildDir);
|
|
350
351
|
const schema = await loadExternalSchema(service, nitro.options.buildDir);
|
|
351
352
|
if (!schema) {
|
|
@@ -379,16 +380,16 @@ async function generateExternalServicesTypes(nitro) {
|
|
|
379
380
|
if (serviceTypesPath) {
|
|
380
381
|
mkdirSync(dirname(serviceTypesPath), { recursive: true });
|
|
381
382
|
writeFileSync(serviceTypesPath, types.types, "utf-8");
|
|
382
|
-
consola.success(`[graphql:${service.name}] Generated types at: ${serviceTypesPath}`);
|
|
383
|
+
if (!options.silent) consola.success(`[graphql:${service.name}] Generated types at: ${serviceTypesPath}`);
|
|
383
384
|
}
|
|
384
385
|
const serviceSdkPath = resolveFilePath(service.paths?.sdk ?? sdkConfig.external, sdkConfig.enabled, true, "{clientGraphql}/{serviceName}/sdk.ts", placeholders);
|
|
385
386
|
if (serviceSdkPath) {
|
|
386
387
|
mkdirSync(dirname(serviceSdkPath), { recursive: true });
|
|
387
388
|
writeFileSync(serviceSdkPath, types.sdk, "utf-8");
|
|
388
|
-
consola.success(`[graphql:${service.name}] Generated SDK at: ${serviceSdkPath}`);
|
|
389
|
+
if (!options.silent) consola.success(`[graphql:${service.name}] Generated SDK at: ${serviceSdkPath}`);
|
|
389
390
|
}
|
|
390
391
|
generateExternalOfetchClient(nitro, service, service.endpoint);
|
|
391
|
-
consola.success(`[graphql:${service.name}] External service types generated successfully`);
|
|
392
|
+
if (!options.silent) consola.success(`[graphql:${service.name}] External service types generated successfully`);
|
|
392
393
|
} catch (error) {
|
|
393
394
|
consola.error(`[graphql:${service.name}] External service generation failed:`, error);
|
|
394
395
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nitro-graphql",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.26",
|
|
5
5
|
"description": "GraphQL integration for Nitro",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -59,6 +59,10 @@
|
|
|
59
59
|
"./vite": {
|
|
60
60
|
"types": "./dist/vite.d.mts",
|
|
61
61
|
"import": "./dist/vite.mjs"
|
|
62
|
+
},
|
|
63
|
+
"./types": {
|
|
64
|
+
"types": "./dist/types/index.d.mts",
|
|
65
|
+
"import": "./dist/types/index.mjs"
|
|
62
66
|
}
|
|
63
67
|
},
|
|
64
68
|
"module": "./dist/index.mjs",
|