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
|
@@ -101,15 +101,13 @@ async function downloadAndSaveSchema(service, buildDir) {
|
|
|
101
101
|
if (downloadMode === "always") {
|
|
102
102
|
shouldDownload = true;
|
|
103
103
|
if (fileExists && hasUrlSchemas) try {
|
|
104
|
-
const
|
|
104
|
+
const remoteSchemaString = printSchemaWithDirectives(loadSchemaSync(schemas.filter(isUrl), {
|
|
105
105
|
loaders: [new UrlLoader()],
|
|
106
106
|
...Object.keys(headers).length > 0 && { headers }
|
|
107
|
-
});
|
|
108
|
-
const remoteSchemaString = printSchemaWithDirectives(remoteSchema);
|
|
107
|
+
}));
|
|
109
108
|
const remoteHash = createHash("md5").update(remoteSchemaString).digest("hex");
|
|
110
109
|
const localSchemaString = readFileSync(schemaFilePath, "utf-8");
|
|
111
|
-
|
|
112
|
-
if (remoteHash === localHash) {
|
|
110
|
+
if (remoteHash === createHash("md5").update(localSchemaString).digest("hex")) {
|
|
113
111
|
shouldDownload = false;
|
|
114
112
|
consola$1.info(`[graphql:${service.name}] Schema is up-to-date, using cached version`);
|
|
115
113
|
}
|
|
@@ -134,24 +132,21 @@ async function downloadAndSaveSchema(service, buildDir) {
|
|
|
134
132
|
} else if (downloadMode === true || downloadMode === "once") shouldDownload = !fileExists;
|
|
135
133
|
if (shouldDownload) {
|
|
136
134
|
if (hasUrlSchemas && hasLocalSchemas) {
|
|
137
|
-
const
|
|
135
|
+
const schemaString = printSchemaWithDirectives(loadSchemaSync(schemas, {
|
|
138
136
|
loaders: [new GraphQLFileLoader(), new UrlLoader()],
|
|
139
137
|
...Object.keys(headers).length > 0 && { headers }
|
|
140
|
-
});
|
|
141
|
-
const schemaString = printSchemaWithDirectives(schema);
|
|
138
|
+
}));
|
|
142
139
|
mkdirSync(dirname(schemaFilePath), { recursive: true });
|
|
143
140
|
writeFileSync(schemaFilePath, schemaString, "utf-8");
|
|
144
141
|
} else if (hasUrlSchemas) {
|
|
145
|
-
const
|
|
142
|
+
const schemaString = printSchemaWithDirectives(loadSchemaSync(schemas, {
|
|
146
143
|
loaders: [new UrlLoader()],
|
|
147
144
|
...Object.keys(headers).length > 0 && { headers }
|
|
148
|
-
});
|
|
149
|
-
const schemaString = printSchemaWithDirectives(schema);
|
|
145
|
+
}));
|
|
150
146
|
mkdirSync(dirname(schemaFilePath), { recursive: true });
|
|
151
147
|
writeFileSync(schemaFilePath, schemaString, "utf-8");
|
|
152
148
|
} else if (hasLocalSchemas) {
|
|
153
|
-
const
|
|
154
|
-
const schemaString = printSchemaWithDirectives(schema);
|
|
149
|
+
const schemaString = printSchemaWithDirectives(loadSchemaSync(schemas, { loaders: [new GraphQLFileLoader()] }));
|
|
155
150
|
mkdirSync(dirname(schemaFilePath), { recursive: true });
|
|
156
151
|
writeFileSync(schemaFilePath, schemaString, "utf-8");
|
|
157
152
|
}
|
|
@@ -176,7 +171,7 @@ async function generateClientTypes(schema, docs, config = {}, sdkConfig = {}, ou
|
|
|
176
171
|
return false;
|
|
177
172
|
}
|
|
178
173
|
const serviceLabel = serviceName ? `:${serviceName}` : "";
|
|
179
|
-
const
|
|
174
|
+
const mergedConfig = defu$1({
|
|
180
175
|
emitLegacyCommonJSImports: false,
|
|
181
176
|
useTypeImports: true,
|
|
182
177
|
enumsAsTypes: true,
|
|
@@ -200,8 +195,7 @@ async function generateClientTypes(schema, docs, config = {}, sdkConfig = {}, ou
|
|
|
200
195
|
output: "File"
|
|
201
196
|
}
|
|
202
197
|
}
|
|
203
|
-
};
|
|
204
|
-
const mergedConfig = defu$1(defaultConfig, config);
|
|
198
|
+
}, config);
|
|
205
199
|
const mergedSdkConfig = defu$1(mergedConfig, sdkConfig);
|
|
206
200
|
try {
|
|
207
201
|
if (docs.length === 0) return {
|
|
@@ -271,15 +265,14 @@ export function getSdk(requester: Requester): Sdk {
|
|
|
271
265
|
typescriptGenericSdk: { plugin: plugin$1 }
|
|
272
266
|
}
|
|
273
267
|
});
|
|
274
|
-
const sdkContent = (await Promise.all(sdkOutput.map(async (config$1) => {
|
|
275
|
-
return {
|
|
276
|
-
file: config$1.filename,
|
|
277
|
-
content: await codegen(config$1)
|
|
278
|
-
};
|
|
279
|
-
})))[0]?.content || "";
|
|
280
268
|
return {
|
|
281
269
|
types: output,
|
|
282
|
-
sdk:
|
|
270
|
+
sdk: (await Promise.all(sdkOutput.map(async (config$1) => {
|
|
271
|
+
return {
|
|
272
|
+
file: config$1.filename,
|
|
273
|
+
content: await codegen(config$1)
|
|
274
|
+
};
|
|
275
|
+
})))[0]?.content || ""
|
|
283
276
|
};
|
|
284
277
|
} catch (error) {
|
|
285
278
|
consola$1.warn(`[graphql${serviceLabel}] Client type generation failed:`, error);
|
|
@@ -290,9 +283,7 @@ export function getSdk(requester: Requester): Sdk {
|
|
|
290
283
|
* Generate client types for external GraphQL service
|
|
291
284
|
*/
|
|
292
285
|
async function generateExternalClientTypes(service, schema, docs) {
|
|
293
|
-
|
|
294
|
-
const sdkConfig = service.codegen?.clientSDK || {};
|
|
295
|
-
return generateClientTypes(schema, docs, config, sdkConfig, void 0, service.name);
|
|
286
|
+
return generateClientTypes(schema, docs, service.codegen?.client || {}, service.codegen?.clientSDK || {}, void 0, service.name);
|
|
296
287
|
}
|
|
297
288
|
|
|
298
289
|
//#endregion
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Nitro } from "nitropack/types";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/file-generator.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Safely write a file to disk, creating parent directories if needed
|
|
7
|
+
*/
|
|
8
|
+
declare function writeFile(filePath: string, content: string, description?: string): void;
|
|
9
|
+
/**
|
|
10
|
+
* Write a file only if it doesn't already exist
|
|
11
|
+
* Returns true if file was created, false if it already existed
|
|
12
|
+
*/
|
|
13
|
+
declare function writeFileIfNotExists(filePath: string, content: string, description?: string): boolean;
|
|
14
|
+
/**
|
|
15
|
+
* Write a file and always overwrite (used for generated files like SDK)
|
|
16
|
+
* This is for files that are auto-generated and should be updated on every build
|
|
17
|
+
*/
|
|
18
|
+
declare function writeGeneratedFile(filePath: string, content: string, description?: string): void;
|
|
19
|
+
/**
|
|
20
|
+
* Check if a path is configured and should be generated
|
|
21
|
+
* Returns the resolved path if should generate, null otherwise
|
|
22
|
+
*/
|
|
23
|
+
declare function getGenerationPath(resolvedPath: string | null, description?: string): string | null;
|
|
24
|
+
/**
|
|
25
|
+
* Log skipped file generation (helpful for debugging library mode)
|
|
26
|
+
*/
|
|
27
|
+
declare function logSkipped(fileName: string, reason?: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Log generated file path
|
|
30
|
+
*/
|
|
31
|
+
declare function logGenerated(fileName: string, path: string): void;
|
|
32
|
+
/**
|
|
33
|
+
* Validate that a Nitro instance has the required GraphQL configuration
|
|
34
|
+
*/
|
|
35
|
+
declare function validateGraphQLConfig(nitro: Nitro): boolean;
|
|
36
|
+
//#endregion
|
|
37
|
+
export { getGenerationPath, logGenerated, logSkipped, validateGraphQLConfig, writeFile, writeFileIfNotExists, writeGeneratedFile };
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
2
|
+
import consola from "consola";
|
|
3
|
+
import { dirname } from "pathe";
|
|
4
|
+
|
|
5
|
+
//#region src/utils/file-generator.ts
|
|
6
|
+
/**
|
|
7
|
+
* Safely write a file to disk, creating parent directories if needed
|
|
8
|
+
*/
|
|
9
|
+
function writeFile(filePath, content, description) {
|
|
10
|
+
try {
|
|
11
|
+
const dir = dirname(filePath);
|
|
12
|
+
if (!existsSync(dir)) mkdirSync(dir, { recursive: true });
|
|
13
|
+
writeFileSync(filePath, content, "utf-8");
|
|
14
|
+
if (description) consola.success(`[nitro-graphql] Generated: ${description}`);
|
|
15
|
+
} catch (error) {
|
|
16
|
+
consola.error(`[nitro-graphql] Failed to write file: ${filePath}`, error);
|
|
17
|
+
throw error;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Write a file only if it doesn't already exist
|
|
22
|
+
* Returns true if file was created, false if it already existed
|
|
23
|
+
*/
|
|
24
|
+
function writeFileIfNotExists(filePath, content, description) {
|
|
25
|
+
if (existsSync(filePath)) return false;
|
|
26
|
+
writeFile(filePath, content, description);
|
|
27
|
+
return true;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Write a file and always overwrite (used for generated files like SDK)
|
|
31
|
+
* This is for files that are auto-generated and should be updated on every build
|
|
32
|
+
*/
|
|
33
|
+
function writeGeneratedFile(filePath, content, description) {
|
|
34
|
+
writeFile(filePath, content, description);
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Check if a path is configured and should be generated
|
|
38
|
+
* Returns the resolved path if should generate, null otherwise
|
|
39
|
+
*/
|
|
40
|
+
function getGenerationPath(resolvedPath, description) {
|
|
41
|
+
if (!resolvedPath) {
|
|
42
|
+
if (description) consola.debug(`[nitro-graphql] Skipping generation: ${description} (disabled in config)`);
|
|
43
|
+
return null;
|
|
44
|
+
}
|
|
45
|
+
return resolvedPath;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Log skipped file generation (helpful for debugging library mode)
|
|
49
|
+
*/
|
|
50
|
+
function logSkipped(fileName, reason) {
|
|
51
|
+
const message = reason ? `Skipped ${fileName}: ${reason}` : `Skipped ${fileName}`;
|
|
52
|
+
consola.debug(`[nitro-graphql] ${message}`);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Log generated file path
|
|
56
|
+
*/
|
|
57
|
+
function logGenerated(fileName, path) {
|
|
58
|
+
consola.info(`[nitro-graphql] Generated ${fileName} at: ${path}`);
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Validate that a Nitro instance has the required GraphQL configuration
|
|
62
|
+
*/
|
|
63
|
+
function validateGraphQLConfig(nitro) {
|
|
64
|
+
if (!nitro.options.graphql?.framework) {
|
|
65
|
+
consola.warn("[nitro-graphql] No GraphQL framework specified. Some features may not work correctly.");
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
//#endregion
|
|
72
|
+
export { getGenerationPath, logGenerated, logSkipped, validateGraphQLConfig, writeFile, writeFileIfNotExists, writeGeneratedFile };
|
package/dist/utils/index.js
CHANGED
|
@@ -49,50 +49,78 @@ async function scanGraphql(nitro) {
|
|
|
49
49
|
async function scanResolvers(nitro) {
|
|
50
50
|
const files = await scanFiles(nitro, "graphql", "**/*.resolver.{ts,js}");
|
|
51
51
|
const exportName = [];
|
|
52
|
-
|
|
52
|
+
const VALID_DEFINE_FUNCTIONS = [
|
|
53
|
+
"defineResolver",
|
|
54
|
+
"defineQuery",
|
|
55
|
+
"defineMutation",
|
|
56
|
+
"defineType",
|
|
57
|
+
"defineSubscription",
|
|
58
|
+
"defineDirective"
|
|
59
|
+
];
|
|
60
|
+
for (const file of files) try {
|
|
53
61
|
const fileContent = await readFile(file.fullPath, "utf-8");
|
|
54
62
|
const parsed = await parseAsync(file.fullPath, fileContent);
|
|
55
63
|
const exports = {
|
|
56
64
|
imports: [],
|
|
57
65
|
specifier: file.fullPath
|
|
58
66
|
};
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
type
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
type
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
type
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
type
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
type
|
|
90
|
-
|
|
91
|
-
|
|
67
|
+
let hasDefaultExport = false;
|
|
68
|
+
let hasNamedExport = false;
|
|
69
|
+
const namedExports = [];
|
|
70
|
+
for (const node of parsed.program.body) {
|
|
71
|
+
if (node.type === "ExportDefaultDeclaration") hasDefaultExport = true;
|
|
72
|
+
if (node.type === "ExportNamedDeclaration" && node.declaration && node.declaration.type === "VariableDeclaration") {
|
|
73
|
+
for (const decl of node.declaration.declarations) if (decl.type === "VariableDeclarator" && decl.init && decl.id.type === "Identifier") {
|
|
74
|
+
hasNamedExport = true;
|
|
75
|
+
namedExports.push(decl.id.name);
|
|
76
|
+
if (decl.init && decl.init.type === "CallExpression") {
|
|
77
|
+
if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineResolver") exports.imports.push({
|
|
78
|
+
name: decl.id.name,
|
|
79
|
+
type: "resolver",
|
|
80
|
+
as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
|
|
81
|
+
});
|
|
82
|
+
if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineQuery") exports.imports.push({
|
|
83
|
+
name: decl.id.name,
|
|
84
|
+
type: "query",
|
|
85
|
+
as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
|
|
86
|
+
});
|
|
87
|
+
if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineMutation") exports.imports.push({
|
|
88
|
+
name: decl.id.name,
|
|
89
|
+
type: "mutation",
|
|
90
|
+
as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
|
|
91
|
+
});
|
|
92
|
+
if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineType") exports.imports.push({
|
|
93
|
+
name: decl.id.name,
|
|
94
|
+
type: "type",
|
|
95
|
+
as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
|
|
96
|
+
});
|
|
97
|
+
if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineSubscription") exports.imports.push({
|
|
98
|
+
name: decl.id.name,
|
|
99
|
+
type: "subscription",
|
|
100
|
+
as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
|
|
101
|
+
});
|
|
102
|
+
if (decl.init.callee.type === "Identifier" && decl.init.callee.name === "defineDirective") exports.imports.push({
|
|
103
|
+
name: decl.id.name,
|
|
104
|
+
type: "directive",
|
|
105
|
+
as: `_${hash(decl.id.name + file.fullPath).replace(/-/g, "").slice(0, 6)}`
|
|
106
|
+
});
|
|
107
|
+
}
|
|
92
108
|
}
|
|
93
109
|
}
|
|
94
110
|
}
|
|
111
|
+
if (nitro.options.dev) {
|
|
112
|
+
const relPath = relative(nitro.options.rootDir, file.fullPath);
|
|
113
|
+
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.`);
|
|
114
|
+
if (exports.imports.length === 0 && hasNamedExport) {
|
|
115
|
+
const validFunctions = VALID_DEFINE_FUNCTIONS.join(", ");
|
|
116
|
+
nitro.logger.warn(`[nitro-graphql] ${relPath}: File has named exports [${namedExports.join(", ")}] but none use the required define functions (${validFunctions}). Exports will not be registered.`);
|
|
117
|
+
}
|
|
118
|
+
if (!hasDefaultExport && !hasNamedExport) nitro.logger.warn(`[nitro-graphql] ${relPath}: No exports found. Resolver files must export resolvers using defineResolver, defineQuery, defineMutation, etc.`);
|
|
119
|
+
}
|
|
95
120
|
if (exports.imports.length > 0) exportName.push(exports);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
const relPath = relative(nitro.options.rootDir, file.fullPath);
|
|
123
|
+
nitro.logger.error(`[nitro-graphql] Failed to parse resolver file ${relPath}:`, error);
|
|
96
124
|
}
|
|
97
125
|
return exportName;
|
|
98
126
|
}
|
|
@@ -143,9 +171,7 @@ async function scanDocs(nitro) {
|
|
|
143
171
|
const relativePath = f.path;
|
|
144
172
|
for (const pattern of externalPatterns) {
|
|
145
173
|
const clientDirPattern = `${nitro.graphql.dir.client}/`;
|
|
146
|
-
|
|
147
|
-
const fileDir = relativePath.split("/")[0];
|
|
148
|
-
if (patternDir === fileDir) return false;
|
|
174
|
+
if (pattern.replace(/* @__PURE__ */ new RegExp(`^${clientDirPattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`), "").split("/")[0] === relativePath.split("/")[0]) return false;
|
|
149
175
|
}
|
|
150
176
|
return true;
|
|
151
177
|
}).map((f) => f.fullPath);
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { ClientUtilsConfig, FileGenerationConfig, ScaffoldConfig, SdkConfig, TypesConfig } from "../types/index.js";
|
|
2
|
+
import { Nitro } from "nitropack/types";
|
|
3
|
+
|
|
4
|
+
//#region src/utils/path-resolver.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Placeholder values for path resolution
|
|
8
|
+
*/
|
|
9
|
+
interface PathPlaceholders {
|
|
10
|
+
serviceName?: string;
|
|
11
|
+
buildDir: string;
|
|
12
|
+
rootDir: string;
|
|
13
|
+
framework: 'nuxt' | 'nitro';
|
|
14
|
+
typesDir: string;
|
|
15
|
+
serverGraphql: string;
|
|
16
|
+
clientGraphql: string;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Replace placeholders in a path string
|
|
20
|
+
* Supports: {serviceName}, {buildDir}, {rootDir}, {framework}, {typesDir}, {serverGraphql}, {clientGraphql}
|
|
21
|
+
*/
|
|
22
|
+
declare function replacePlaceholders(path: string, placeholders: PathPlaceholders): string;
|
|
23
|
+
/**
|
|
24
|
+
* Get default paths based on framework and user configuration
|
|
25
|
+
*/
|
|
26
|
+
declare function getDefaultPaths(nitro: Nitro): Required<PathPlaceholders>;
|
|
27
|
+
/**
|
|
28
|
+
* Check if a file should be generated based on config
|
|
29
|
+
* Returns: true if should generate, false if should skip
|
|
30
|
+
*/
|
|
31
|
+
declare function shouldGenerateFile(config: FileGenerationConfig | undefined, categoryEnabled: boolean | undefined, topLevelEnabled: boolean): boolean;
|
|
32
|
+
/**
|
|
33
|
+
* Resolve the file path based on configuration
|
|
34
|
+
* Returns: resolved absolute path or null if file should not be generated
|
|
35
|
+
*/
|
|
36
|
+
declare function resolveFilePath(config: FileGenerationConfig | undefined, categoryEnabled: boolean | undefined, topLevelEnabled: boolean, defaultPath: string, placeholders: PathPlaceholders): string | null;
|
|
37
|
+
/**
|
|
38
|
+
* Check if scaffold files should be generated (category-level check)
|
|
39
|
+
*/
|
|
40
|
+
declare function shouldGenerateScaffold(nitro: Nitro): boolean;
|
|
41
|
+
/**
|
|
42
|
+
* Get scaffold configuration (handles false case)
|
|
43
|
+
*/
|
|
44
|
+
declare function getScaffoldConfig(nitro: Nitro): ScaffoldConfig;
|
|
45
|
+
/**
|
|
46
|
+
* Check if client utilities should be generated (category-level check)
|
|
47
|
+
*/
|
|
48
|
+
declare function shouldGenerateClientUtils(nitro: Nitro): boolean;
|
|
49
|
+
/**
|
|
50
|
+
* Get client utilities configuration (handles false case)
|
|
51
|
+
*/
|
|
52
|
+
declare function getClientUtilsConfig(nitro: Nitro): ClientUtilsConfig;
|
|
53
|
+
/**
|
|
54
|
+
* Check if SDK files should be generated (category-level check)
|
|
55
|
+
*/
|
|
56
|
+
declare function shouldGenerateSDK(nitro: Nitro): boolean;
|
|
57
|
+
/**
|
|
58
|
+
* Get SDK configuration (handles false case)
|
|
59
|
+
*/
|
|
60
|
+
declare function getSdkConfig(nitro: Nitro): SdkConfig;
|
|
61
|
+
/**
|
|
62
|
+
* Check if type files should be generated (category-level check)
|
|
63
|
+
*/
|
|
64
|
+
declare function shouldGenerateTypes(nitro: Nitro): boolean;
|
|
65
|
+
/**
|
|
66
|
+
* Get types configuration (handles false case)
|
|
67
|
+
*/
|
|
68
|
+
declare function getTypesConfig(nitro: Nitro): TypesConfig;
|
|
69
|
+
//#endregion
|
|
70
|
+
export { PathPlaceholders, getClientUtilsConfig, getDefaultPaths, getScaffoldConfig, getSdkConfig, getTypesConfig, replacePlaceholders, resolveFilePath, shouldGenerateClientUtils, shouldGenerateFile, shouldGenerateSDK, shouldGenerateScaffold, shouldGenerateTypes };
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import { resolve } from "pathe";
|
|
2
|
+
|
|
3
|
+
//#region src/utils/path-resolver.ts
|
|
4
|
+
/**
|
|
5
|
+
* Replace placeholders in a path string
|
|
6
|
+
* Supports: {serviceName}, {buildDir}, {rootDir}, {framework}, {typesDir}, {serverGraphql}, {clientGraphql}
|
|
7
|
+
*/
|
|
8
|
+
function replacePlaceholders(path, placeholders) {
|
|
9
|
+
return path.replace(/\{serviceName\}/g, placeholders.serviceName || "default").replace(/\{buildDir\}/g, placeholders.buildDir).replace(/\{rootDir\}/g, placeholders.rootDir).replace(/\{framework\}/g, placeholders.framework).replace(/\{typesDir\}/g, placeholders.typesDir).replace(/\{serverGraphql\}/g, placeholders.serverGraphql).replace(/\{clientGraphql\}/g, placeholders.clientGraphql);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Get default paths based on framework and user configuration
|
|
13
|
+
*/
|
|
14
|
+
function getDefaultPaths(nitro) {
|
|
15
|
+
const isNuxt = nitro.options.framework?.name === "nuxt";
|
|
16
|
+
const rootDir = nitro.options.rootDir;
|
|
17
|
+
const buildDir = nitro.options.buildDir;
|
|
18
|
+
const pathsConfig = nitro.options.graphql?.paths || {};
|
|
19
|
+
const defaultServerGraphql = pathsConfig.serverGraphql || resolve(rootDir, "server", "graphql");
|
|
20
|
+
const defaultClientGraphql = pathsConfig.clientGraphql || resolve(rootDir, isNuxt ? "app/graphql" : "graphql");
|
|
21
|
+
const defaultBuildDir = pathsConfig.buildDir || buildDir;
|
|
22
|
+
const defaultTypesDir = pathsConfig.typesDir || resolve(defaultBuildDir, "types");
|
|
23
|
+
return {
|
|
24
|
+
serviceName: "default",
|
|
25
|
+
buildDir: defaultBuildDir,
|
|
26
|
+
rootDir,
|
|
27
|
+
framework: isNuxt ? "nuxt" : "nitro",
|
|
28
|
+
typesDir: defaultTypesDir,
|
|
29
|
+
serverGraphql: defaultServerGraphql,
|
|
30
|
+
clientGraphql: defaultClientGraphql
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Check if a file should be generated based on config
|
|
35
|
+
* Returns: true if should generate, false if should skip
|
|
36
|
+
*/
|
|
37
|
+
function shouldGenerateFile(config, categoryEnabled, topLevelEnabled) {
|
|
38
|
+
if (config === false) return false;
|
|
39
|
+
if (config === true || typeof config === "string") return true;
|
|
40
|
+
if (categoryEnabled === false) return false;
|
|
41
|
+
if (categoryEnabled === true) return true;
|
|
42
|
+
return topLevelEnabled;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Resolve the file path based on configuration
|
|
46
|
+
* Returns: resolved absolute path or null if file should not be generated
|
|
47
|
+
*/
|
|
48
|
+
function resolveFilePath(config, categoryEnabled, topLevelEnabled, defaultPath, placeholders) {
|
|
49
|
+
if (!shouldGenerateFile(config, categoryEnabled, topLevelEnabled)) return null;
|
|
50
|
+
if (typeof config === "string") {
|
|
51
|
+
const customPath = replacePlaceholders(config, placeholders);
|
|
52
|
+
return resolve(placeholders.rootDir, customPath);
|
|
53
|
+
}
|
|
54
|
+
const resolvedDefault = replacePlaceholders(defaultPath, placeholders);
|
|
55
|
+
return resolve(placeholders.rootDir, resolvedDefault);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Check if scaffold files should be generated (category-level check)
|
|
59
|
+
*/
|
|
60
|
+
function shouldGenerateScaffold(nitro) {
|
|
61
|
+
const scaffoldConfig = nitro.options.graphql?.scaffold;
|
|
62
|
+
if (scaffoldConfig === false) return false;
|
|
63
|
+
if (scaffoldConfig && scaffoldConfig.enabled === false) return false;
|
|
64
|
+
return true;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Get scaffold configuration (handles false case)
|
|
68
|
+
*/
|
|
69
|
+
function getScaffoldConfig(nitro) {
|
|
70
|
+
const scaffoldConfig = nitro.options.graphql?.scaffold;
|
|
71
|
+
if (scaffoldConfig === false) return { enabled: false };
|
|
72
|
+
return scaffoldConfig || {};
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Check if client utilities should be generated (category-level check)
|
|
76
|
+
*/
|
|
77
|
+
function shouldGenerateClientUtils(nitro) {
|
|
78
|
+
const clientUtilsConfig = nitro.options.graphql?.clientUtils;
|
|
79
|
+
if (clientUtilsConfig === false) return false;
|
|
80
|
+
if (clientUtilsConfig && clientUtilsConfig.enabled === false) return false;
|
|
81
|
+
return nitro.options.framework?.name === "nuxt";
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Get client utilities configuration (handles false case)
|
|
85
|
+
*/
|
|
86
|
+
function getClientUtilsConfig(nitro) {
|
|
87
|
+
const clientUtilsConfig = nitro.options.graphql?.clientUtils;
|
|
88
|
+
if (clientUtilsConfig === false) return { enabled: false };
|
|
89
|
+
return clientUtilsConfig || {};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Check if SDK files should be generated (category-level check)
|
|
93
|
+
*/
|
|
94
|
+
function shouldGenerateSDK(nitro) {
|
|
95
|
+
const sdkConfig = nitro.options.graphql?.sdk;
|
|
96
|
+
if (sdkConfig === false) return false;
|
|
97
|
+
if (sdkConfig && sdkConfig.enabled === false) return false;
|
|
98
|
+
return true;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Get SDK configuration (handles false case)
|
|
102
|
+
*/
|
|
103
|
+
function getSdkConfig(nitro) {
|
|
104
|
+
const sdkConfig = nitro.options.graphql?.sdk;
|
|
105
|
+
if (sdkConfig === false) return { enabled: false };
|
|
106
|
+
return sdkConfig || {};
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Check if type files should be generated (category-level check)
|
|
110
|
+
*/
|
|
111
|
+
function shouldGenerateTypes(nitro) {
|
|
112
|
+
const typesConfig = nitro.options.graphql?.types;
|
|
113
|
+
if (typesConfig === false) return false;
|
|
114
|
+
if (typesConfig && typesConfig.enabled === false) return false;
|
|
115
|
+
return true;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Get types configuration (handles false case)
|
|
119
|
+
*/
|
|
120
|
+
function getTypesConfig(nitro) {
|
|
121
|
+
const typesConfig = nitro.options.graphql?.types;
|
|
122
|
+
if (typesConfig === false) return { enabled: false };
|
|
123
|
+
return typesConfig || {};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
//#endregion
|
|
127
|
+
export { getClientUtilsConfig, getDefaultPaths, getScaffoldConfig, getSdkConfig, getTypesConfig, replacePlaceholders, resolveFilePath, shouldGenerateClientUtils, shouldGenerateFile, shouldGenerateSDK, shouldGenerateScaffold, shouldGenerateTypes };
|
|
@@ -21,7 +21,7 @@ function pluginContent(_schema, _documents, _config, _info) {
|
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
async function generateTypes(selectFremework, schema, config = {}, outputPath) {
|
|
24
|
-
const
|
|
24
|
+
const mergedConfig = defu$1({
|
|
25
25
|
scalars: {
|
|
26
26
|
DateTime: DateTimeResolver.extensions.codegenScalarType,
|
|
27
27
|
DateTimeISO: DateTimeISOResolver.extensions.codegenScalarType,
|
|
@@ -43,8 +43,7 @@ async function generateTypes(selectFremework, schema, config = {}, outputPath) {
|
|
|
43
43
|
declarationKind: "interface",
|
|
44
44
|
enumsAsTypes: true,
|
|
45
45
|
...config.federation?.enabled && { federation: true }
|
|
46
|
-
};
|
|
47
|
-
const mergedConfig = defu$1(defaultConfig, config.codegen?.server);
|
|
46
|
+
}, config.codegen?.server);
|
|
48
47
|
return await codegen({
|
|
49
48
|
filename: outputPath || "types.generated.ts",
|
|
50
49
|
schema: parse(printSchemaWithDirectives(schema)),
|