nitro-graphql 1.4.4 → 1.5.1
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 +791 -0
- package/dist/ecosystem/nuxt.js +1 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +81 -25
- package/dist/rollup.js +153 -28
- package/dist/routes/apollo-server.d.ts +2 -2
- package/dist/routes/apollo-server.js +8 -13
- package/dist/routes/debug.d.ts +61 -0
- package/dist/routes/debug.js +449 -0
- package/dist/routes/graphql-yoga.js +12 -18
- package/dist/types/index.d.ts +119 -1
- package/dist/types/standard-schema.d.ts +2 -2
- package/dist/utils/client-codegen.js +17 -26
- package/dist/utils/file-generator.d.ts +37 -0
- package/dist/utils/file-generator.js +72 -0
- package/dist/utils/index.js +63 -37
- package/dist/utils/path-resolver.d.ts +70 -0
- package/dist/utils/path-resolver.js +127 -0
- package/dist/utils/server-codegen.js +2 -3
- package/dist/utils/type-generation.js +84 -37
- package/package.json +15 -15
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import { downloadAndSaveSchema, generateClientTypes, generateExternalClientTypes, loadExternalSchema, loadGraphQLDocuments } from "./client-codegen.js";
|
|
2
|
+
import { writeFileIfNotExists } from "./file-generator.js";
|
|
3
|
+
import { getClientUtilsConfig, getDefaultPaths, getSdkConfig, getTypesConfig, resolveFilePath, shouldGenerateClientUtils, shouldGenerateTypes } from "./path-resolver.js";
|
|
2
4
|
import { generateTypes } from "./server-codegen.js";
|
|
3
5
|
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
6
|
import consola from "consola";
|
|
@@ -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
|
-
|
|
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,14 +30,21 @@ 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
|
-
|
|
33
|
+
writeFileIfNotExists(indexPath, indexContent, "client index.ts");
|
|
28
34
|
}
|
|
29
35
|
}
|
|
30
|
-
function generateNuxtOfetchClient(clientDir, serviceName = "default") {
|
|
31
|
-
|
|
32
|
-
const
|
|
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
|
-
if (!existsSync(ofetchPath))
|
|
47
|
+
if (!existsSync(ofetchPath)) writeFileIfNotExists(ofetchPath, `// This file is auto-generated once by nitro-graphql for quick start
|
|
35
48
|
// You can modify this file according to your needs
|
|
36
49
|
import type { Requester } from './sdk'
|
|
37
50
|
import { getSdk } from './sdk'
|
|
@@ -53,15 +66,23 @@ export function createGraphQLClient(endpoint: string): Requester {
|
|
|
53
66
|
}
|
|
54
67
|
}
|
|
55
68
|
|
|
56
|
-
export const $sdk = getSdk(createGraphQLClient('/api/graphql'))`,
|
|
69
|
+
export const $sdk = getSdk(createGraphQLClient('/api/graphql'))`, `${serviceName} ofetch.ts`);
|
|
57
70
|
}
|
|
58
|
-
function generateExternalOfetchClient(
|
|
59
|
-
|
|
60
|
-
const
|
|
71
|
+
function generateExternalOfetchClient(nitro, service, endpoint) {
|
|
72
|
+
if (!shouldGenerateClientUtils(nitro)) return;
|
|
73
|
+
const serviceName = service.name;
|
|
74
|
+
const placeholders = {
|
|
75
|
+
...getDefaultPaths(nitro),
|
|
76
|
+
serviceName
|
|
77
|
+
};
|
|
78
|
+
const clientUtilsConfig = getClientUtilsConfig(nitro);
|
|
79
|
+
const ofetchPath = resolveFilePath(service.paths?.ofetch ?? clientUtilsConfig.ofetch, clientUtilsConfig.enabled, true, "{clientGraphql}/{serviceName}/ofetch.ts", placeholders);
|
|
80
|
+
if (!ofetchPath) return;
|
|
81
|
+
const serviceDir = dirname(ofetchPath);
|
|
61
82
|
if (!existsSync(serviceDir)) mkdirSync(serviceDir, { recursive: true });
|
|
62
83
|
if (!existsSync(ofetchPath)) {
|
|
63
84
|
const capitalizedServiceName = serviceName.charAt(0).toUpperCase() + serviceName.slice(1);
|
|
64
|
-
|
|
85
|
+
writeFileIfNotExists(ofetchPath, `// This file is auto-generated once by nitro-graphql for quick start
|
|
65
86
|
// You can modify this file according to your needs
|
|
66
87
|
import type { Sdk, Requester } from './sdk'
|
|
67
88
|
import { getSdk } from './sdk'
|
|
@@ -83,8 +104,7 @@ export function create${capitalizedServiceName}GraphQLClient(endpoint: string =
|
|
|
83
104
|
}
|
|
84
105
|
}
|
|
85
106
|
|
|
86
|
-
export const $${serviceName}Sdk: Sdk = getSdk(create${capitalizedServiceName}GraphQLClient())
|
|
87
|
-
writeFileSync(ofetchPath, ofetchContent, "utf-8");
|
|
107
|
+
export const $${serviceName}Sdk: Sdk = getSdk(create${capitalizedServiceName}GraphQLClient())`, `${serviceName} external ofetch.ts`);
|
|
88
108
|
}
|
|
89
109
|
}
|
|
90
110
|
/**
|
|
@@ -179,6 +199,10 @@ function validateNoDuplicateTypes(schemas, schemaStrings) {
|
|
|
179
199
|
}
|
|
180
200
|
async function serverTypeGeneration(app) {
|
|
181
201
|
try {
|
|
202
|
+
if (!shouldGenerateTypes(app)) {
|
|
203
|
+
consola.debug("[nitro-graphql] Server type generation is disabled");
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
182
206
|
const schemas = app.scanSchemas || [];
|
|
183
207
|
if (!schemas.length) {
|
|
184
208
|
consola.info("No GraphQL definitions found for server type generation.");
|
|
@@ -198,9 +222,14 @@ async function serverTypeGeneration(app) {
|
|
|
198
222
|
const schemaPath = resolve(app.graphql.buildDir, "schema.graphql");
|
|
199
223
|
mkdirSync(dirname(schemaPath), { recursive: true });
|
|
200
224
|
writeFileSync(schemaPath, printSchema, "utf-8");
|
|
201
|
-
const
|
|
202
|
-
|
|
203
|
-
|
|
225
|
+
const placeholders = getDefaultPaths(app);
|
|
226
|
+
const typesConfig = getTypesConfig(app);
|
|
227
|
+
const serverTypesPath = resolveFilePath(typesConfig.server, typesConfig.enabled, true, "{typesDir}/nitro-graphql-server.d.ts", placeholders);
|
|
228
|
+
if (serverTypesPath) {
|
|
229
|
+
mkdirSync(dirname(serverTypesPath), { recursive: true });
|
|
230
|
+
writeFileSync(serverTypesPath, data, "utf-8");
|
|
231
|
+
consola.success(`[nitro-graphql] Generated server types at: ${serverTypesPath}`);
|
|
232
|
+
}
|
|
204
233
|
} catch (error) {
|
|
205
234
|
consola.error("Server schema generation error:", error);
|
|
206
235
|
}
|
|
@@ -251,20 +280,27 @@ async function generateMainClientTypes(nitro) {
|
|
|
251
280
|
return;
|
|
252
281
|
}
|
|
253
282
|
const graphqlString = readFileSync(schemaFilePath, "utf-8");
|
|
254
|
-
const
|
|
255
|
-
const types = await generateClientTypes(schema, loadDocs, nitro.options.graphql?.codegen?.client ?? {}, nitro.options.graphql?.codegen?.clientSDK ?? {});
|
|
283
|
+
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 ?? {});
|
|
256
284
|
if (types === false) return;
|
|
257
|
-
const
|
|
258
|
-
const
|
|
259
|
-
const
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
285
|
+
const placeholders = getDefaultPaths(nitro);
|
|
286
|
+
const typesConfig = getTypesConfig(nitro);
|
|
287
|
+
const sdkConfig = getSdkConfig(nitro);
|
|
288
|
+
const clientTypesPath = resolveFilePath(typesConfig.client, typesConfig.enabled, true, "{typesDir}/nitro-graphql-client.d.ts", placeholders);
|
|
289
|
+
if (clientTypesPath) {
|
|
290
|
+
mkdirSync(dirname(clientTypesPath), { recursive: true });
|
|
291
|
+
writeFileSync(clientTypesPath, types.types, "utf-8");
|
|
292
|
+
consola.success(`[nitro-graphql] Generated client types at: ${clientTypesPath}`);
|
|
293
|
+
}
|
|
294
|
+
const sdkPath = resolveFilePath(sdkConfig.main, sdkConfig.enabled, true, "{clientGraphql}/default/sdk.ts", placeholders);
|
|
295
|
+
if (sdkPath) {
|
|
296
|
+
mkdirSync(dirname(sdkPath), { recursive: true });
|
|
297
|
+
writeFileSync(sdkPath, types.sdk, "utf-8");
|
|
298
|
+
consola.success(`[nitro-graphql] Generated SDK at: ${sdkPath}`);
|
|
299
|
+
}
|
|
264
300
|
if (nitro.options.framework?.name === "nuxt") {
|
|
265
|
-
generateNuxtOfetchClient(nitro.graphql.clientDir, "default");
|
|
301
|
+
generateNuxtOfetchClient(nitro, nitro.graphql.clientDir, "default");
|
|
266
302
|
const externalServices = nitro.options.graphql?.externalServices || [];
|
|
267
|
-
generateGraphQLIndexFile(nitro.graphql.clientDir, externalServices);
|
|
303
|
+
generateGraphQLIndexFile(nitro, nitro.graphql.clientDir, externalServices);
|
|
268
304
|
}
|
|
269
305
|
}
|
|
270
306
|
async function generateExternalServicesTypes(nitro) {
|
|
@@ -294,14 +330,25 @@ async function generateExternalServicesTypes(nitro) {
|
|
|
294
330
|
consola.warn(`[graphql:${service.name}] Type generation failed`);
|
|
295
331
|
continue;
|
|
296
332
|
}
|
|
297
|
-
const
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
if (
|
|
333
|
+
const placeholders = {
|
|
334
|
+
...getDefaultPaths(nitro),
|
|
335
|
+
serviceName: service.name
|
|
336
|
+
};
|
|
337
|
+
const typesConfig = getTypesConfig(nitro);
|
|
338
|
+
const sdkConfig = getSdkConfig(nitro);
|
|
339
|
+
const serviceTypesPath = resolveFilePath(service.paths?.types ?? typesConfig.external, typesConfig.enabled, true, "{typesDir}/nitro-graphql-client-{serviceName}.d.ts", placeholders);
|
|
340
|
+
if (serviceTypesPath) {
|
|
341
|
+
mkdirSync(dirname(serviceTypesPath), { recursive: true });
|
|
342
|
+
writeFileSync(serviceTypesPath, types.types, "utf-8");
|
|
343
|
+
consola.success(`[graphql:${service.name}] Generated types at: ${serviceTypesPath}`);
|
|
344
|
+
}
|
|
345
|
+
const serviceSdkPath = resolveFilePath(service.paths?.sdk ?? sdkConfig.external, sdkConfig.enabled, true, "{clientGraphql}/{serviceName}/sdk.ts", placeholders);
|
|
346
|
+
if (serviceSdkPath) {
|
|
347
|
+
mkdirSync(dirname(serviceSdkPath), { recursive: true });
|
|
348
|
+
writeFileSync(serviceSdkPath, types.sdk, "utf-8");
|
|
349
|
+
consola.success(`[graphql:${service.name}] Generated SDK at: ${serviceSdkPath}`);
|
|
350
|
+
}
|
|
351
|
+
if (nitro.options.framework?.name === "nuxt") generateExternalOfetchClient(nitro, service, service.endpoint);
|
|
305
352
|
consola.success(`[graphql:${service.name}] External service types generated successfully`);
|
|
306
353
|
} catch (error) {
|
|
307
354
|
consola.error(`[graphql:${service.name}] External service generation failed:`, error);
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nitro-graphql",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.5.1",
|
|
5
5
|
"description": "GraphQL integration for Nitro",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -78,13 +78,13 @@
|
|
|
78
78
|
}
|
|
79
79
|
},
|
|
80
80
|
"dependencies": {
|
|
81
|
-
"@apollo/subgraph": "^2.11.
|
|
81
|
+
"@apollo/subgraph": "^2.11.3",
|
|
82
82
|
"@graphql-codegen/core": "^5.0.0",
|
|
83
83
|
"@graphql-codegen/import-types-preset": "^3.0.1",
|
|
84
|
-
"@graphql-codegen/typescript": "^5.0.
|
|
84
|
+
"@graphql-codegen/typescript": "^5.0.2",
|
|
85
85
|
"@graphql-codegen/typescript-generic-sdk": "^4.0.2",
|
|
86
|
-
"@graphql-codegen/typescript-operations": "^5.0.
|
|
87
|
-
"@graphql-codegen/typescript-resolvers": "^5.0
|
|
86
|
+
"@graphql-codegen/typescript-operations": "^5.0.2",
|
|
87
|
+
"@graphql-codegen/typescript-resolvers": "^5.1.0",
|
|
88
88
|
"@graphql-tools/graphql-file-loader": "^8.1.2",
|
|
89
89
|
"@graphql-tools/load": "^8.1.2",
|
|
90
90
|
"@graphql-tools/load-files": "^7.0.1",
|
|
@@ -96,27 +96,27 @@
|
|
|
96
96
|
"consola": "^3.4.2",
|
|
97
97
|
"defu": "^6.1.4",
|
|
98
98
|
"graphql-config": "^5.1.5",
|
|
99
|
-
"graphql-scalars": "^1.
|
|
99
|
+
"graphql-scalars": "^1.25.0",
|
|
100
100
|
"knitwork": "^1.2.0",
|
|
101
101
|
"ohash": "^2.0.11",
|
|
102
|
-
"oxc-parser": "^0.
|
|
102
|
+
"oxc-parser": "^0.95.0",
|
|
103
103
|
"pathe": "^2.0.3",
|
|
104
104
|
"tinyglobby": "^0.2.15"
|
|
105
105
|
},
|
|
106
106
|
"devDependencies": {
|
|
107
|
-
"@antfu/eslint-config": "^
|
|
108
|
-
"@nuxt/kit": "^4.1.
|
|
109
|
-
"@nuxt/schema": "^4.1.
|
|
110
|
-
"@types/node": "^24.
|
|
111
|
-
"bumpp": "^10.
|
|
107
|
+
"@antfu/eslint-config": "^6.0.0",
|
|
108
|
+
"@nuxt/kit": "^4.1.3",
|
|
109
|
+
"@nuxt/schema": "^4.1.3",
|
|
110
|
+
"@types/node": "^24.9.1",
|
|
111
|
+
"bumpp": "^10.3.1",
|
|
112
112
|
"changelogen": "^0.6.2",
|
|
113
113
|
"crossws": "0.3.5",
|
|
114
|
-
"eslint": "^9.
|
|
114
|
+
"eslint": "^9.38.0",
|
|
115
115
|
"graphql": "16.11.0",
|
|
116
116
|
"graphql-yoga": "^5.16.0",
|
|
117
117
|
"h3": "1.15.3",
|
|
118
|
-
"nitropack": "^2.12.
|
|
119
|
-
"tsdown": "^0.15.
|
|
118
|
+
"nitropack": "^2.12.7",
|
|
119
|
+
"tsdown": "^0.15.9",
|
|
120
120
|
"typescript": "^5.9.3"
|
|
121
121
|
},
|
|
122
122
|
"resolutions": {
|