nitro-graphql 2.0.0-beta.36 → 2.0.0-beta.38
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/cli/commands/generate.d.mts +26 -0
- package/dist/cli/commands/generate.mjs +196 -0
- package/dist/cli/commands/index.d.mts +4 -0
- package/dist/cli/commands/index.mjs +5 -0
- package/dist/cli/commands/init.d.mts +12 -0
- package/dist/cli/commands/init.mjs +100 -0
- package/dist/cli/commands/validate.d.mts +10 -0
- package/dist/cli/commands/validate.mjs +69 -0
- package/dist/cli/config.d.mts +75 -0
- package/dist/cli/config.mjs +20 -0
- package/dist/cli/index.d.mts +24 -0
- package/dist/cli/index.mjs +219 -0
- package/dist/core/codegen/client.d.mts +23 -0
- package/dist/core/codegen/client.mjs +150 -0
- package/dist/core/codegen/document-loader.d.mts +10 -0
- package/dist/core/codegen/document-loader.mjs +18 -0
- package/dist/core/codegen/index.d.mts +8 -0
- package/dist/core/codegen/index.mjs +9 -0
- package/dist/{utils/codegen-plugin.d.mts → core/codegen/plugin.d.mts} +2 -2
- package/dist/{utils/codegen-plugin.mjs → core/codegen/plugin.mjs} +1 -1
- package/dist/core/codegen/runtime.d.mts +20 -0
- package/dist/core/codegen/runtime.mjs +60 -0
- package/dist/core/codegen/schema-loader.d.mts +28 -0
- package/dist/core/codegen/schema-loader.mjs +128 -0
- package/dist/core/codegen/server.d.mts +28 -0
- package/dist/core/codegen/server.mjs +143 -0
- package/dist/{codegen → core/codegen}/validation.d.mts +1 -1
- package/dist/{codegen → core/codegen}/validation.mjs +1 -1
- package/dist/core/config.d.mts +50 -0
- package/dist/core/config.mjs +82 -0
- package/dist/core/constants.d.mts +188 -0
- package/dist/core/constants.mjs +210 -0
- package/dist/core/index.d.mts +32 -0
- package/dist/core/index.mjs +26 -0
- package/dist/core/scanning/ast-scanner.d.mts +23 -0
- package/dist/core/scanning/ast-scanner.mjs +103 -0
- package/dist/core/scanning/common.d.mts +37 -0
- package/dist/core/scanning/common.mjs +60 -0
- package/dist/core/scanning/directives.d.mts +10 -0
- package/dist/core/scanning/directives.mjs +29 -0
- package/dist/core/scanning/documents.d.mts +21 -0
- package/dist/core/scanning/documents.mjs +43 -0
- package/dist/core/scanning/index.d.mts +7 -0
- package/dist/core/scanning/index.mjs +8 -0
- package/dist/core/scanning/resolvers.d.mts +10 -0
- package/dist/core/scanning/resolvers.mjs +58 -0
- package/dist/core/scanning/schemas.d.mts +14 -0
- package/dist/core/scanning/schemas.mjs +64 -0
- package/dist/{utils/schema-builder.d.mts → core/schema/builder.d.mts} +7 -2
- package/dist/{utils/schema-builder.mjs → core/schema/builder.mjs} +22 -3
- package/dist/{utils → core/schema}/federation.d.mts +10 -5
- package/dist/{utils → core/schema}/federation.mjs +1 -1
- package/dist/core/schema/index.d.mts +3 -0
- package/dist/core/schema/index.mjs +4 -0
- package/dist/core/types/adapter.d.mts +58 -0
- package/dist/core/types/codegen.d.mts +133 -0
- package/dist/core/types/config.d.mts +212 -0
- package/dist/{types → core/types}/define.d.mts +3 -7
- package/dist/core/types/index.d.mts +5 -0
- package/dist/core/types/scanning.d.mts +69 -0
- package/dist/core/types/scanning.mjs +1 -0
- package/dist/{utils → core/utils}/directive-parser.d.mts +21 -5
- package/dist/{utils → core/utils}/directive-parser.mjs +25 -36
- package/dist/{utils → core/utils}/errors.d.mts +8 -4
- package/dist/{utils → core/utils}/errors.mjs +8 -4
- package/dist/core/utils/file-io.d.mts +24 -0
- package/dist/core/utils/file-io.mjs +47 -0
- package/dist/{utils → core/utils}/imports.d.mts +1 -1
- package/dist/{utils → core/utils}/imports.mjs +1 -1
- package/dist/core/utils/index.d.mts +7 -0
- package/dist/core/utils/index.mjs +8 -0
- package/dist/core/utils/logger.d.mts +19 -0
- package/dist/core/utils/logger.mjs +38 -0
- package/dist/{utils → core/utils}/ofetch-templates.d.mts +1 -1
- package/dist/{utils → core/utils}/ofetch-templates.mjs +1 -1
- package/dist/core/validation/external-services.d.mts +11 -0
- package/dist/{utils/validation.mjs → core/validation/external-services.mjs} +3 -3
- package/dist/core/validation/index.d.mts +2 -0
- package/dist/core/validation/index.mjs +3 -0
- package/dist/define.d.mts +1 -3
- package/dist/index.d.mts +6 -43
- package/dist/index.mjs +5 -62
- package/dist/nitro/adapter.d.mts +30 -0
- package/dist/nitro/adapter.mjs +97 -0
- package/dist/{utils → nitro}/apollo.d.mts +1 -1
- package/dist/{utils → nitro}/apollo.mjs +1 -1
- package/dist/nitro/codegen.d.mts +19 -0
- package/dist/nitro/codegen.mjs +141 -0
- package/dist/nitro/config.d.mts +52 -0
- package/dist/nitro/config.mjs +58 -0
- package/dist/nitro/index.d.mts +46 -0
- package/dist/nitro/index.mjs +65 -0
- package/dist/{utils/path-resolver.d.mts → nitro/paths.d.mts} +3 -19
- package/dist/{utils/path-resolver.mjs → nitro/paths.mjs} +2 -36
- package/dist/{rollup.d.mts → nitro/rollup.d.mts} +1 -1
- package/dist/{rollup.mjs → nitro/rollup.mjs} +11 -23
- package/dist/{routes → nitro/routes}/apollo-server.d.mts +1 -1
- package/dist/nitro/routes/apollo-server.mjs +71 -0
- package/dist/{routes → nitro/routes}/debug-template.d.mts +1 -1
- package/dist/{routes → nitro/routes}/debug-template.mjs +1 -1
- package/dist/{routes → nitro/routes}/debug.d.mts +10 -16
- package/dist/{routes → nitro/routes}/debug.mjs +2 -2
- package/dist/nitro/routes/graphql-yoga.d.mts +6 -0
- package/dist/{routes → nitro/routes}/graphql-yoga.mjs +21 -10
- package/dist/nitro/routes/health.d.mts +10 -0
- package/dist/{routes → nitro/routes}/health.mjs +1 -1
- package/dist/nitro/setup/file-watcher.d.mts +16 -0
- package/dist/{setup → nitro/setup}/file-watcher.mjs +14 -18
- package/dist/nitro/setup/logging.d.mts +17 -0
- package/dist/nitro/setup/logging.mjs +66 -0
- package/dist/nitro/setup/rollup-integration.d.mts +16 -0
- package/dist/{setup → nitro/setup}/rollup-integration.mjs +2 -2
- package/dist/nitro/setup/routes.d.mts +10 -0
- package/dist/nitro/setup/routes.mjs +35 -0
- package/dist/nitro/setup/ts-config.d.mts +11 -0
- package/dist/{setup → nitro/setup}/ts-config.mjs +3 -3
- package/dist/{setup.d.mts → nitro/setup.d.mts} +3 -2
- package/dist/{setup.mjs → nitro/setup.mjs} +73 -62
- package/dist/nitro/types.d.mts +378 -0
- package/dist/nitro/types.mjs +1 -0
- package/dist/nitro/virtual/generators.d.mts +31 -0
- package/dist/nitro/virtual/generators.mjs +193 -0
- package/dist/nitro/virtual/stubs.d.mts +20 -0
- package/dist/nitro/virtual/stubs.mjs +31 -0
- package/dist/{ecosystem/nuxt.d.mts → nuxt.d.mts} +1 -1
- package/dist/{ecosystem/nuxt.mjs → nuxt.mjs} +2 -2
- package/dist/{graphql/server.d.mts → stubs/index.d.mts} +5 -1
- package/dist/stubs/index.mjs +1 -0
- package/package.json +45 -43
- package/dist/codegen/client-types.d.mts +0 -13
- package/dist/codegen/client-types.mjs +0 -131
- package/dist/codegen/external-types.d.mts +0 -12
- package/dist/codegen/external-types.mjs +0 -88
- package/dist/codegen/index.d.mts +0 -18
- package/dist/codegen/index.mjs +0 -24
- package/dist/codegen/server-types.d.mts +0 -13
- package/dist/codegen/server-types.mjs +0 -64
- package/dist/config/defaults.mjs +0 -36
- package/dist/constants/scalars.mjs +0 -27
- package/dist/constants.mjs +0 -106
- package/dist/graphql/index.d.mts +0 -5
- package/dist/routes/apollo-server.mjs +0 -45
- package/dist/routes/graphql-yoga.d.mts +0 -6
- package/dist/routes/health.d.mts +0 -10
- package/dist/setup/graphql-scanner.mjs +0 -25
- package/dist/setup/scaffold-generator.mjs +0 -109
- package/dist/types/index.d.mts +0 -246
- package/dist/types/standard-schema.d.mts +0 -59
- package/dist/utils/client-codegen.d.mts +0 -41
- package/dist/utils/client-codegen.mjs +0 -267
- package/dist/utils/file-generator.d.mts +0 -37
- package/dist/utils/file-generator.mjs +0 -72
- package/dist/utils/file-writer.d.mts +0 -35
- package/dist/utils/file-writer.mjs +0 -32
- package/dist/utils/index.d.mts +0 -13
- package/dist/utils/index.mjs +0 -13
- package/dist/utils/layers.d.mts +0 -22
- package/dist/utils/layers.mjs +0 -28
- package/dist/utils/scanning/common.d.mts +0 -23
- package/dist/utils/scanning/common.mjs +0 -39
- package/dist/utils/scanning/directives.d.mts +0 -11
- package/dist/utils/scanning/directives.mjs +0 -43
- package/dist/utils/scanning/documents.d.mts +0 -15
- package/dist/utils/scanning/documents.mjs +0 -46
- package/dist/utils/scanning/index.d.mts +0 -6
- package/dist/utils/scanning/index.mjs +0 -7
- package/dist/utils/scanning/resolvers.d.mts +0 -11
- package/dist/utils/scanning/resolvers.mjs +0 -100
- package/dist/utils/scanning/schemas.d.mts +0 -15
- package/dist/utils/scanning/schemas.mjs +0 -29
- package/dist/utils/server-codegen.d.mts +0 -7
- package/dist/utils/server-codegen.mjs +0 -113
- package/dist/utils/type-generation.d.mts +0 -6
- package/dist/utils/type-generation.mjs +0 -7
- package/dist/utils/validation.d.mts +0 -11
- package/dist/virtual/debug-info.d.mts +0 -9
- package/dist/virtual/debug-info.mjs +0 -26
- package/dist/virtual/generators/config.d.mts +0 -22
- package/dist/virtual/generators/config.mjs +0 -36
- package/dist/virtual/generators/debug.d.mts +0 -14
- package/dist/virtual/generators/debug.mjs +0 -53
- package/dist/virtual/generators/directives.d.mts +0 -14
- package/dist/virtual/generators/directives.mjs +0 -52
- package/dist/virtual/generators/index.d.mts +0 -6
- package/dist/virtual/generators/index.mjs +0 -7
- package/dist/virtual/generators/resolvers.d.mts +0 -14
- package/dist/virtual/generators/resolvers.mjs +0 -55
- package/dist/virtual/generators/schemas.d.mts +0 -14
- package/dist/virtual/generators/schemas.mjs +0 -43
- package/dist/virtual/graphql-config.d.mts +0 -9
- package/dist/virtual/graphql-config.mjs +0 -10
- package/dist/virtual/module-config.d.mts +0 -9
- package/dist/virtual/module-config.mjs +0 -10
- package/dist/virtual/server-directives.d.mts +0 -11
- package/dist/virtual/server-directives.mjs +0 -10
- package/dist/virtual/server-resolvers.d.mts +0 -11
- package/dist/virtual/server-resolvers.mjs +0 -10
- package/dist/virtual/server-schemas.d.mts +0 -11
- package/dist/virtual/server-schemas.mjs +0 -10
- /package/dist/{graphql/index.mjs → core/types/adapter.mjs} +0 -0
- /package/dist/{graphql/server.mjs → core/types/codegen.mjs} +0 -0
- /package/dist/{types/define.mjs → core/types/config.mjs} +0 -0
- /package/dist/{types/index.mjs → core/types/define.mjs} +0 -0
- /package/dist/{types/standard-schema.mjs → core/types/index.mjs} +0 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { CLIContext } from "../index.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/cli/commands/generate.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Generate all types (server + client + optional runtime)
|
|
7
|
+
*/
|
|
8
|
+
declare function generateAll(ctx: CLIContext, options?: {
|
|
9
|
+
silent?: boolean;
|
|
10
|
+
watch?: boolean;
|
|
11
|
+
runtime?: boolean;
|
|
12
|
+
}): Promise<void>;
|
|
13
|
+
/**
|
|
14
|
+
* Generate server types
|
|
15
|
+
*/
|
|
16
|
+
declare function generateServer(ctx: CLIContext, options?: {
|
|
17
|
+
silent?: boolean;
|
|
18
|
+
}): Promise<void>;
|
|
19
|
+
/**
|
|
20
|
+
* Generate client types
|
|
21
|
+
*/
|
|
22
|
+
declare function generateClient(ctx: CLIContext, options?: {
|
|
23
|
+
silent?: boolean;
|
|
24
|
+
}): Promise<void>;
|
|
25
|
+
//#endregion
|
|
26
|
+
export { generateAll, generateClient, generateServer };
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { LOG_TAG } from "../../core/constants.mjs";
|
|
2
|
+
import { loadGraphQLDocuments } from "../../core/codegen/document-loader.mjs";
|
|
3
|
+
import { generateClientTypesCore } from "../../core/codegen/client.mjs";
|
|
4
|
+
import { generateResolverModule, generateRuntimeIndex, generateSchemaModule } from "../../core/codegen/runtime.mjs";
|
|
5
|
+
import { generateServerTypesCore } from "../../core/codegen/server.mjs";
|
|
6
|
+
import { scanDocumentsCore } from "../../core/scanning/documents.mjs";
|
|
7
|
+
import { scanResolversCore } from "../../core/scanning/resolvers.mjs";
|
|
8
|
+
import { scanSchemasCore } from "../../core/scanning/schemas.mjs";
|
|
9
|
+
import { buildGraphQLSchema } from "../../core/schema/builder.mjs";
|
|
10
|
+
import consola from "consola";
|
|
11
|
+
import { dirname, join, relative } from "pathe";
|
|
12
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
13
|
+
|
|
14
|
+
//#region src/cli/commands/generate.ts
|
|
15
|
+
const logger = consola.withTag(LOG_TAG);
|
|
16
|
+
/**
|
|
17
|
+
* Create ScanContext from CLI context
|
|
18
|
+
*/
|
|
19
|
+
function createScanContext(ctx) {
|
|
20
|
+
return {
|
|
21
|
+
rootDir: ctx.config.rootDir,
|
|
22
|
+
serverDir: ctx.config.serverDir,
|
|
23
|
+
clientDir: ctx.config.clientDir,
|
|
24
|
+
ignorePatterns: ctx.config.ignore,
|
|
25
|
+
isDev: false,
|
|
26
|
+
logger: {
|
|
27
|
+
info: (msg, ...args) => logger.info(msg, ...args),
|
|
28
|
+
warn: (msg, ...args) => logger.warn(msg, ...args),
|
|
29
|
+
error: (msg, ...args) => logger.error(msg, ...args),
|
|
30
|
+
success: (msg, ...args) => logger.success(msg, ...args),
|
|
31
|
+
debug: (msg, ...args) => logger.debug(msg, ...args)
|
|
32
|
+
},
|
|
33
|
+
layerServerDirs: [],
|
|
34
|
+
layerAppDirs: []
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Generate all types (server + client + optional runtime)
|
|
39
|
+
*/
|
|
40
|
+
async function generateAll(ctx, options = {}) {
|
|
41
|
+
await generateServer(ctx, options);
|
|
42
|
+
await generateClient(ctx, options);
|
|
43
|
+
if (options.runtime) await generateRuntimeFiles(ctx, options);
|
|
44
|
+
if (options.watch) await watchAndRegenerate(ctx, options);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Generate server types
|
|
48
|
+
*/
|
|
49
|
+
async function generateServer(ctx, options = {}) {
|
|
50
|
+
const schemaResult = await scanSchemasCore(createScanContext(ctx));
|
|
51
|
+
if (schemaResult.errors.length > 0) for (const error of schemaResult.errors) logger.error(error);
|
|
52
|
+
if (schemaResult.items.length === 0) {
|
|
53
|
+
if (!options.silent) logger.info("No GraphQL schemas found in server directory");
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
const schema = await buildGraphQLSchema(schemaResult.items);
|
|
57
|
+
if (!schema) {
|
|
58
|
+
logger.error("Failed to build GraphQL schema");
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
const result = await generateServerTypesCore({
|
|
62
|
+
framework: ctx.config.framework,
|
|
63
|
+
schema,
|
|
64
|
+
config: ctx.config.codegen?.server,
|
|
65
|
+
federationEnabled: ctx.config.federation?.enabled
|
|
66
|
+
});
|
|
67
|
+
const schemaPath = join(ctx.config.buildDir, "schema.graphql");
|
|
68
|
+
mkdirSync(dirname(schemaPath), { recursive: true });
|
|
69
|
+
writeFileSync(schemaPath, result.schemaString, "utf-8");
|
|
70
|
+
const typesPath = join(ctx.config.typesDir, "nitro-graphql-server.d.ts");
|
|
71
|
+
mkdirSync(dirname(typesPath), { recursive: true });
|
|
72
|
+
writeFileSync(typesPath, result.types, "utf-8");
|
|
73
|
+
if (!options.silent) logger.success(`Generated server types: ${relative(ctx.config.rootDir, typesPath)}`);
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Generate client types
|
|
77
|
+
*/
|
|
78
|
+
async function generateClient(ctx, options = {}) {
|
|
79
|
+
const scanCtx = createScanContext(ctx);
|
|
80
|
+
const schemaPath = join(ctx.config.buildDir, "schema.graphql");
|
|
81
|
+
if (!existsSync(schemaPath)) {
|
|
82
|
+
if (!options.silent) logger.info("Server schema not found. Generate server types first.");
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
const docsResult = await scanDocumentsCore(scanCtx, { externalServices: ctx.config.externalServices });
|
|
86
|
+
if (docsResult.errors.length > 0) for (const error of docsResult.errors) logger.error(error);
|
|
87
|
+
if (docsResult.items.length === 0) {
|
|
88
|
+
if (!options.silent) logger.info("No GraphQL documents found in client directory");
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
const documents = await loadGraphQLDocuments(docsResult.items);
|
|
92
|
+
const { buildSchema } = await import("graphql");
|
|
93
|
+
const { readFileSync: readFileSync$1 } = await import("node:fs");
|
|
94
|
+
const result = await generateClientTypesCore({
|
|
95
|
+
schema: buildSchema(readFileSync$1(schemaPath, "utf-8")),
|
|
96
|
+
documents,
|
|
97
|
+
config: ctx.config.codegen?.client,
|
|
98
|
+
sdkConfig: ctx.config.codegen?.clientSDK,
|
|
99
|
+
options: { silent: options.silent }
|
|
100
|
+
});
|
|
101
|
+
if (result === false) {
|
|
102
|
+
if (!options.silent) logger.warn("Client type generation skipped");
|
|
103
|
+
return;
|
|
104
|
+
}
|
|
105
|
+
const typesPath = join(ctx.config.typesDir, "nitro-graphql-client.d.ts");
|
|
106
|
+
mkdirSync(dirname(typesPath), { recursive: true });
|
|
107
|
+
writeFileSync(typesPath, result.types, "utf-8");
|
|
108
|
+
const sdkPath = join(ctx.config.clientDir, "default", "sdk.ts");
|
|
109
|
+
mkdirSync(dirname(sdkPath), { recursive: true });
|
|
110
|
+
writeFileSync(sdkPath, result.sdk, "utf-8");
|
|
111
|
+
if (!options.silent) logger.success(`Generated client types: ${relative(ctx.config.rootDir, typesPath)}`);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Generate runtime files (resolvers.ts, schema.ts, index.ts)
|
|
115
|
+
*/
|
|
116
|
+
async function generateRuntimeFiles(ctx, options = {}) {
|
|
117
|
+
const scanCtx = createScanContext(ctx);
|
|
118
|
+
const runtimeConfig = ctx.config.runtime;
|
|
119
|
+
const runtimeDir = typeof runtimeConfig === "object" && runtimeConfig.outDir ? join(ctx.config.rootDir, runtimeConfig.outDir) : join(ctx.config.buildDir, "runtime");
|
|
120
|
+
mkdirSync(runtimeDir, { recursive: true });
|
|
121
|
+
const include = typeof runtimeConfig === "object" && runtimeConfig.include ? runtimeConfig.include : {
|
|
122
|
+
resolvers: true,
|
|
123
|
+
schema: true,
|
|
124
|
+
index: true
|
|
125
|
+
};
|
|
126
|
+
if (include.resolvers !== false) {
|
|
127
|
+
const resolversResult = await scanResolversCore(scanCtx);
|
|
128
|
+
if (resolversResult.items.length > 0) {
|
|
129
|
+
const resolverCode = generateResolverModule(resolversResult.items, runtimeDir);
|
|
130
|
+
writeFileSync(join(runtimeDir, "resolvers.ts"), resolverCode, "utf-8");
|
|
131
|
+
if (!options.silent) logger.success(`Generated runtime: ${relative(ctx.config.rootDir, join(runtimeDir, "resolvers.ts"))}`);
|
|
132
|
+
} else if (!options.silent) logger.info("No resolvers found for runtime generation");
|
|
133
|
+
}
|
|
134
|
+
if (include.schema !== false) {
|
|
135
|
+
const schemaPath = join(ctx.config.buildDir, "schema.graphql");
|
|
136
|
+
if (existsSync(schemaPath)) {
|
|
137
|
+
const { readFileSync: readFileSync$1 } = await import("node:fs");
|
|
138
|
+
const schemaCode = generateSchemaModule(readFileSync$1(schemaPath, "utf-8"));
|
|
139
|
+
writeFileSync(join(runtimeDir, "schema.ts"), schemaCode, "utf-8");
|
|
140
|
+
if (!options.silent) logger.success(`Generated runtime: ${relative(ctx.config.rootDir, join(runtimeDir, "schema.ts"))}`);
|
|
141
|
+
} else if (!options.silent) logger.info("Schema not found for runtime generation. Run generate first.");
|
|
142
|
+
}
|
|
143
|
+
if (include.index !== false) {
|
|
144
|
+
const indexCode = generateRuntimeIndex();
|
|
145
|
+
writeFileSync(join(runtimeDir, "index.ts"), indexCode, "utf-8");
|
|
146
|
+
if (!options.silent) logger.success(`Generated runtime: ${relative(ctx.config.rootDir, join(runtimeDir, "index.ts"))}`);
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Watch mode - regenerate on file changes
|
|
151
|
+
*/
|
|
152
|
+
async function watchAndRegenerate(ctx, options = {}) {
|
|
153
|
+
const { watch } = await import("chokidar");
|
|
154
|
+
const watchDirs = [ctx.config.serverDir, ctx.config.clientDir];
|
|
155
|
+
const watcher = watch(watchDirs, {
|
|
156
|
+
ignoreInitial: true,
|
|
157
|
+
ignored: [
|
|
158
|
+
...ctx.config.ignore || [],
|
|
159
|
+
/node_modules/,
|
|
160
|
+
/\.git/
|
|
161
|
+
],
|
|
162
|
+
persistent: true
|
|
163
|
+
});
|
|
164
|
+
await new Promise((resolve$1) => {
|
|
165
|
+
watcher.on("ready", resolve$1);
|
|
166
|
+
watcher.on("error", (error) => logger.error("Watcher error:", error));
|
|
167
|
+
});
|
|
168
|
+
const relPath = (p) => relative(ctx.config.rootDir, p) || ".";
|
|
169
|
+
logger.info(`Watching: ${watchDirs.map(relPath).join(", ")}`);
|
|
170
|
+
let debounceTimer = null;
|
|
171
|
+
const debounceMs = ctx.config.watch?.debounce ?? 300;
|
|
172
|
+
watcher.on("all", (event, filePath) => {
|
|
173
|
+
if (!filePath.endsWith(".graphql") && !filePath.endsWith(".resolver.ts")) return;
|
|
174
|
+
if (debounceTimer) clearTimeout(debounceTimer);
|
|
175
|
+
debounceTimer = setTimeout(async () => {
|
|
176
|
+
try {
|
|
177
|
+
await generateAll(ctx, {
|
|
178
|
+
silent: options.silent ?? true,
|
|
179
|
+
watch: false
|
|
180
|
+
});
|
|
181
|
+
logger.success(`Types regenerated (${relPath(filePath)})`);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
logger.error("Regeneration failed:", error);
|
|
184
|
+
}
|
|
185
|
+
}, debounceMs);
|
|
186
|
+
});
|
|
187
|
+
await new Promise((resolve$1) => {
|
|
188
|
+
process.on("SIGINT", () => {
|
|
189
|
+
watcher.close();
|
|
190
|
+
resolve$1();
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
//#endregion
|
|
196
|
+
export { generateAll, generateClient, generateServer };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { CLIContext } from "../index.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/cli/commands/init.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Initialize project structure
|
|
7
|
+
*/
|
|
8
|
+
declare function init(ctx: CLIContext, options?: {
|
|
9
|
+
force?: boolean;
|
|
10
|
+
}): Promise<void>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { init };
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { LOG_TAG } from "../../core/constants.mjs";
|
|
2
|
+
import consola from "consola";
|
|
3
|
+
import { join, relative } from "pathe";
|
|
4
|
+
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
5
|
+
|
|
6
|
+
//#region src/cli/commands/init.ts
|
|
7
|
+
const logger = consola.withTag(LOG_TAG);
|
|
8
|
+
/**
|
|
9
|
+
* Initialize project structure
|
|
10
|
+
*/
|
|
11
|
+
async function init(ctx, options = {}) {
|
|
12
|
+
const { force } = options;
|
|
13
|
+
const dirs = [ctx.config.serverDir, ctx.config.clientDir];
|
|
14
|
+
for (const dir of dirs) if (!existsSync(dir)) {
|
|
15
|
+
mkdirSync(dir, { recursive: true });
|
|
16
|
+
logger.info(`Created directory: ${dir}`);
|
|
17
|
+
}
|
|
18
|
+
const configPath = join(ctx.config.rootDir, "nitro-graphql.config.ts");
|
|
19
|
+
if (force || !existsSync(configPath)) {
|
|
20
|
+
const relativeServerDir = relative(ctx.config.rootDir, ctx.config.serverDir) || "server/graphql";
|
|
21
|
+
const relativeClientDir = relative(ctx.config.rootDir, ctx.config.clientDir) || "graphql";
|
|
22
|
+
writeFileSync(configPath, `import { defineConfig } from 'nitro-graphql/cli'
|
|
23
|
+
|
|
24
|
+
export default defineConfig({
|
|
25
|
+
framework: '${ctx.config.framework}',
|
|
26
|
+
serverDir: './${relativeServerDir}',
|
|
27
|
+
clientDir: './${relativeClientDir}',
|
|
28
|
+
})
|
|
29
|
+
`, "utf-8");
|
|
30
|
+
logger.success(`Created config file: ${configPath}`);
|
|
31
|
+
} else logger.info(`Config file already exists: ${configPath}`);
|
|
32
|
+
const tsconfigPath = join(ctx.config.rootDir, "tsconfig.json");
|
|
33
|
+
if (force || !existsSync(tsconfigPath)) {
|
|
34
|
+
const relativeBuildDir = relative(ctx.config.rootDir, ctx.config.buildDir) || ".graphql";
|
|
35
|
+
writeFileSync(tsconfigPath, `{
|
|
36
|
+
"compilerOptions": {
|
|
37
|
+
"target": "ESNext",
|
|
38
|
+
"module": "ESNext",
|
|
39
|
+
"moduleResolution": "Bundler",
|
|
40
|
+
"resolveJsonModule": true,
|
|
41
|
+
"allowSyntheticDefaultImports": true,
|
|
42
|
+
"strict": true,
|
|
43
|
+
"noEmit": true,
|
|
44
|
+
"skipLibCheck": true,
|
|
45
|
+
"forceConsistentCasingInFileNames": true,
|
|
46
|
+
"paths": {
|
|
47
|
+
"#graphql/server": ["./${relativeBuildDir}/types/nitro-graphql-server.d.ts"],
|
|
48
|
+
"#graphql/client": ["./${relativeBuildDir}/types/nitro-graphql-client.d.ts"],
|
|
49
|
+
"#graphql/schema": ["./${relative(ctx.config.rootDir, ctx.config.serverDir) || "server/graphql"}/schema.ts"]
|
|
50
|
+
}
|
|
51
|
+
},
|
|
52
|
+
"include": ["**/*.ts"],
|
|
53
|
+
"exclude": ["node_modules", "${relativeBuildDir}"]
|
|
54
|
+
}
|
|
55
|
+
`, "utf-8");
|
|
56
|
+
logger.success(`Created tsconfig.json: ${tsconfigPath}`);
|
|
57
|
+
} else logger.info(`tsconfig.json already exists: ${tsconfigPath}`);
|
|
58
|
+
const schemaPath = join(ctx.config.serverDir, "schema.graphql");
|
|
59
|
+
if (force || !existsSync(schemaPath)) {
|
|
60
|
+
writeFileSync(schemaPath, `# GraphQL Schema
|
|
61
|
+
# Add your type definitions here
|
|
62
|
+
|
|
63
|
+
type Query {
|
|
64
|
+
hello: String!
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
type Mutation {
|
|
68
|
+
# Add your mutations here
|
|
69
|
+
_empty: String
|
|
70
|
+
}
|
|
71
|
+
`, "utf-8");
|
|
72
|
+
logger.success(`Created example schema: ${schemaPath}`);
|
|
73
|
+
} else logger.info(`Schema file already exists: ${schemaPath}`);
|
|
74
|
+
const resolverPath = join(ctx.config.serverDir, "hello.resolver.ts");
|
|
75
|
+
if (force || !existsSync(resolverPath)) {
|
|
76
|
+
writeFileSync(resolverPath, `import { defineQuery } from 'nitro-graphql/define'
|
|
77
|
+
|
|
78
|
+
export const helloQueries = defineQuery({
|
|
79
|
+
hello: () => 'Hello, world!',
|
|
80
|
+
})
|
|
81
|
+
`, "utf-8");
|
|
82
|
+
logger.success(`Created example resolver: ${resolverPath}`);
|
|
83
|
+
} else logger.info(`Resolver file already exists: ${resolverPath}`);
|
|
84
|
+
const queryPath = join(ctx.config.clientDir, "hello.graphql");
|
|
85
|
+
if (force || !existsSync(queryPath)) {
|
|
86
|
+
writeFileSync(queryPath, `query Hello {
|
|
87
|
+
hello
|
|
88
|
+
}
|
|
89
|
+
`, "utf-8");
|
|
90
|
+
logger.success(`Created example query: ${queryPath}`);
|
|
91
|
+
} else logger.info(`Query file already exists: ${queryPath}`);
|
|
92
|
+
logger.info("");
|
|
93
|
+
logger.info("Next steps:");
|
|
94
|
+
logger.info(" 1. Run \"nitro-graphql generate\" to generate types");
|
|
95
|
+
logger.info(" 2. Add more schemas to your server directory");
|
|
96
|
+
logger.info(" 3. Add more queries/mutations to your client directory");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
//#endregion
|
|
100
|
+
export { init };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { LOG_TAG } from "../../core/constants.mjs";
|
|
2
|
+
import { validateNoDuplicateTypes } from "../../core/codegen/validation.mjs";
|
|
3
|
+
import { scanSchemasCore } from "../../core/scanning/schemas.mjs";
|
|
4
|
+
import consola from "consola";
|
|
5
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
6
|
+
|
|
7
|
+
//#region src/cli/commands/validate.ts
|
|
8
|
+
const logger = consola.withTag(LOG_TAG);
|
|
9
|
+
/**
|
|
10
|
+
* Validate GraphQL schemas
|
|
11
|
+
*/
|
|
12
|
+
async function validate(ctx) {
|
|
13
|
+
const schemaResult = await scanSchemasCore({
|
|
14
|
+
rootDir: ctx.config.rootDir,
|
|
15
|
+
serverDir: ctx.config.serverDir,
|
|
16
|
+
clientDir: ctx.config.clientDir,
|
|
17
|
+
ignorePatterns: ctx.config.ignore,
|
|
18
|
+
isDev: false,
|
|
19
|
+
logger: {
|
|
20
|
+
info: (msg, ...args) => logger.info(msg, ...args),
|
|
21
|
+
warn: (msg, ...args) => logger.warn(msg, ...args),
|
|
22
|
+
error: (msg, ...args) => logger.error(msg, ...args),
|
|
23
|
+
success: (msg, ...args) => logger.success(msg, ...args),
|
|
24
|
+
debug: (msg, ...args) => logger.debug(msg, ...args)
|
|
25
|
+
},
|
|
26
|
+
layerServerDirs: [],
|
|
27
|
+
layerAppDirs: []
|
|
28
|
+
});
|
|
29
|
+
if (schemaResult.errors.length > 0) {
|
|
30
|
+
for (const error of schemaResult.errors) logger.error(error);
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
if (schemaResult.items.length === 0) {
|
|
34
|
+
logger.warn("No GraphQL schemas found to validate");
|
|
35
|
+
return true;
|
|
36
|
+
}
|
|
37
|
+
const schemaContents = [];
|
|
38
|
+
for (const schemaPath of schemaResult.items) {
|
|
39
|
+
if (!existsSync(schemaPath)) {
|
|
40
|
+
logger.error(`Schema file not found: ${schemaPath}`);
|
|
41
|
+
return false;
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
const content = readFileSync(schemaPath, "utf-8");
|
|
45
|
+
schemaContents.push(content);
|
|
46
|
+
} catch (error) {
|
|
47
|
+
logger.error(`Failed to read schema file ${schemaPath}:`, error);
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (!validateNoDuplicateTypes(schemaResult.items, schemaContents)) {
|
|
52
|
+
logger.error("Schema validation failed");
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
const { buildSchema, print } = await import("graphql");
|
|
57
|
+
const { mergeTypeDefs } = await import("@graphql-tools/merge");
|
|
58
|
+
const mergedTypeDefs = mergeTypeDefs(schemaContents);
|
|
59
|
+
buildSchema(typeof mergedTypeDefs === "string" ? mergedTypeDefs : print(mergedTypeDefs));
|
|
60
|
+
logger.info(`Validated ${schemaResult.items.length} schema file(s)`);
|
|
61
|
+
return true;
|
|
62
|
+
} catch (error) {
|
|
63
|
+
logger.error("Schema syntax error:", error);
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
export { validate };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { GraphQLFramework } from "../core/constants.mjs";
|
|
2
|
+
import { CoreClientUtilsConfig, CoreCodegenConfig, CoreExternalService, CoreFederationConfig, CorePathsConfig, CoreScaffoldConfig, CoreSdkConfig, CoreSecurityConfig, CoreTypesConfig } from "../core/types/config.mjs";
|
|
3
|
+
import "../core/types/index.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/cli/config.d.ts
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* CLI configuration options
|
|
9
|
+
* Used when running nitro-graphql as a standalone CLI tool
|
|
10
|
+
*/
|
|
11
|
+
interface CLIConfig {
|
|
12
|
+
/** Root directory (defaults to current working directory) */
|
|
13
|
+
rootDir?: string;
|
|
14
|
+
/** Build output directory */
|
|
15
|
+
buildDir?: string;
|
|
16
|
+
/** Server GraphQL directory (defaults to 'server/graphql') */
|
|
17
|
+
serverDir?: string;
|
|
18
|
+
/** Client GraphQL directory (defaults to 'graphql') */
|
|
19
|
+
clientDir?: string;
|
|
20
|
+
/** Types output directory */
|
|
21
|
+
typesDir?: string;
|
|
22
|
+
/** GraphQL framework (defaults to 'graphql-yoga') */
|
|
23
|
+
framework?: GraphQLFramework;
|
|
24
|
+
/** Codegen configuration */
|
|
25
|
+
codegen?: CoreCodegenConfig;
|
|
26
|
+
/** Security configuration */
|
|
27
|
+
security?: CoreSecurityConfig;
|
|
28
|
+
/** External GraphQL services */
|
|
29
|
+
externalServices?: CoreExternalService[];
|
|
30
|
+
/** Apollo Federation configuration */
|
|
31
|
+
federation?: CoreFederationConfig;
|
|
32
|
+
/** Path configuration */
|
|
33
|
+
paths?: CorePathsConfig;
|
|
34
|
+
/** Scaffold file configuration */
|
|
35
|
+
scaffold?: false | CoreScaffoldConfig;
|
|
36
|
+
/** Type generation configuration */
|
|
37
|
+
types?: false | CoreTypesConfig;
|
|
38
|
+
/** SDK generation configuration */
|
|
39
|
+
sdk?: false | CoreSdkConfig;
|
|
40
|
+
/** Client utilities configuration */
|
|
41
|
+
clientUtils?: false | CoreClientUtilsConfig;
|
|
42
|
+
/** Patterns to ignore during scanning */
|
|
43
|
+
ignore?: string[];
|
|
44
|
+
/** Watch mode configuration */
|
|
45
|
+
watch?: {
|
|
46
|
+
/** Enable watch mode */
|
|
47
|
+
enabled?: boolean;
|
|
48
|
+
/** Debounce time in ms */
|
|
49
|
+
debounce?: number;
|
|
50
|
+
};
|
|
51
|
+
/**
|
|
52
|
+
* Runtime file generation
|
|
53
|
+
* Generates resolvers.ts, schema.ts for standalone server usage
|
|
54
|
+
*/
|
|
55
|
+
runtime?: boolean | {
|
|
56
|
+
/** Output directory for runtime files (defaults to '{buildDir}/runtime') */
|
|
57
|
+
outDir?: string;
|
|
58
|
+
/** What to include in generation */
|
|
59
|
+
include?: {
|
|
60
|
+
resolvers?: boolean;
|
|
61
|
+
schema?: boolean;
|
|
62
|
+
index?: boolean;
|
|
63
|
+
};
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Define CLI configuration with type safety
|
|
68
|
+
*/
|
|
69
|
+
declare function defineConfig(config: CLIConfig): CLIConfig;
|
|
70
|
+
/**
|
|
71
|
+
* Default CLI configuration values
|
|
72
|
+
*/
|
|
73
|
+
declare const DEFAULT_CLI_CONFIG: CLIConfig;
|
|
74
|
+
//#endregion
|
|
75
|
+
export { CLIConfig, DEFAULT_CLI_CONFIG, defineConfig };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
//#region src/cli/config.ts
|
|
2
|
+
/**
|
|
3
|
+
* Define CLI configuration with type safety
|
|
4
|
+
*/
|
|
5
|
+
function defineConfig(config) {
|
|
6
|
+
return config;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Default CLI configuration values
|
|
10
|
+
*/
|
|
11
|
+
const DEFAULT_CLI_CONFIG = {
|
|
12
|
+
framework: "graphql-yoga",
|
|
13
|
+
serverDir: "server/graphql",
|
|
14
|
+
clientDir: "graphql",
|
|
15
|
+
buildDir: ".graphql",
|
|
16
|
+
ignore: ["**/node_modules/**", "**/dist/**"]
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
//#endregion
|
|
20
|
+
export { DEFAULT_CLI_CONFIG, defineConfig };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { CLIConfig, defineConfig } from "./config.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/cli/index.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* CLI context with resolved configuration
|
|
8
|
+
*/
|
|
9
|
+
interface CLIContext {
|
|
10
|
+
config: Required<Pick<CLIConfig, 'rootDir' | 'buildDir' | 'serverDir' | 'clientDir' | 'typesDir' | 'framework' | 'ignore'>> & CLIConfig;
|
|
11
|
+
cwd: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Load CLI configuration from file or defaults
|
|
15
|
+
*/
|
|
16
|
+
declare function loadConfig(cwd?: string): Promise<CLIConfig>;
|
|
17
|
+
/**
|
|
18
|
+
* Create CLI context from configuration
|
|
19
|
+
*/
|
|
20
|
+
declare function createCLIContext(options?: {
|
|
21
|
+
cwd?: string;
|
|
22
|
+
}): Promise<CLIContext>;
|
|
23
|
+
//#endregion
|
|
24
|
+
export { type CLIConfig, CLIContext, createCLIContext, defineConfig, loadConfig };
|