nitro-graphql 2.0.0-beta.4 → 2.0.0-beta.41
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 +438 -27
- 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 +46 -0
- package/dist/cli/commands/init.mjs +195 -0
- package/dist/cli/commands/validate.d.mts +10 -0
- package/dist/cli/commands/validate.mjs +69 -0
- package/dist/cli/completions.d.mts +7 -0
- package/dist/cli/completions.mjs +34 -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 +253 -0
- package/dist/config.d.mts +2 -0
- package/dist/config.mjs +3 -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/core/codegen/plugin.d.mts +20 -0
- package/dist/core/codegen/plugin.mjs +30 -0
- 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/core/codegen/validation.d.mts +13 -0
- package/dist/core/codegen/validation.mjs +96 -0
- 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 +33 -0
- package/dist/core/index.mjs +27 -0
- package/dist/core/manifest.d.mts +46 -0
- package/dist/core/manifest.mjs +76 -0
- package/dist/core/scanning/ast-scanner.d.mts +28 -0
- package/dist/core/scanning/ast-scanner.mjs +122 -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 +15 -0
- package/dist/core/scanning/resolvers.mjs +59 -0
- package/dist/core/scanning/schemas.d.mts +14 -0
- package/dist/core/scanning/schemas.mjs +64 -0
- package/dist/core/schema/builder.d.mts +53 -0
- package/dist/core/schema/builder.mjs +70 -0
- package/dist/core/schema/federation.d.mts +34 -0
- package/dist/core/schema/federation.mjs +40 -0
- 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 +210 -0
- package/dist/core/types/config.mjs +1 -0
- package/dist/{utils/define.d.ts → core/types/define.d.mts} +3 -30
- package/dist/core/types/define.mjs +1 -0
- package/dist/core/types/index.d.mts +5 -0
- package/dist/core/types/index.mjs +1 -0
- package/dist/core/types/scanning.d.mts +69 -0
- package/dist/core/types/scanning.mjs +1 -0
- package/dist/{utils/directive-parser.d.ts → core/utils/directive-parser.d.mts} +21 -4
- package/dist/{utils/directive-parser.js → core/utils/directive-parser.mjs} +25 -34
- package/dist/core/utils/errors.d.mts +77 -0
- package/dist/core/utils/errors.mjs +93 -0
- package/dist/core/utils/file-io.d.mts +24 -0
- package/dist/core/utils/file-io.mjs +47 -0
- package/dist/core/utils/imports.d.mts +15 -0
- package/dist/core/utils/imports.mjs +25 -0
- 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/core/utils/ofetch-templates.d.mts +30 -0
- package/dist/core/utils/ofetch-templates.mjs +135 -0
- package/dist/core/validation/external-services.d.mts +11 -0
- package/dist/core/validation/external-services.mjs +34 -0
- package/dist/core/validation/index.d.mts +2 -0
- package/dist/core/validation/index.mjs +3 -0
- package/dist/define.d.mts +294 -0
- package/dist/define.mjs +323 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.mjs +6 -0
- package/dist/nitro/adapter.d.mts +30 -0
- package/dist/nitro/adapter.mjs +97 -0
- package/dist/{utils/apollo.d.ts → nitro/apollo.d.mts} +3 -3
- package/dist/nitro/apollo.mjs +59 -0
- package/dist/nitro/codegen.d.mts +19 -0
- package/dist/nitro/codegen.mjs +141 -0
- package/dist/nitro/config.d.mts +51 -0
- package/dist/nitro/config.mjs +57 -0
- package/dist/nitro/index.d.mts +46 -0
- package/dist/nitro/index.mjs +65 -0
- package/dist/nitro/paths.d.mts +54 -0
- package/dist/nitro/paths.mjs +92 -0
- package/dist/nitro/rollup.d.mts +6 -0
- package/dist/nitro/rollup.mjs +95 -0
- package/dist/nitro/routes/apollo-server.d.mts +6 -0
- package/dist/nitro/routes/apollo-server.mjs +71 -0
- package/dist/nitro/routes/debug-template.d.mts +15 -0
- package/dist/nitro/routes/debug-template.mjs +385 -0
- package/dist/nitro/routes/debug.d.mts +55 -0
- package/dist/nitro/routes/debug.mjs +102 -0
- package/dist/nitro/routes/graphql-yoga.d.mts +6 -0
- package/dist/nitro/routes/graphql-yoga.mjs +62 -0
- package/dist/nitro/routes/health.d.mts +10 -0
- package/dist/{routes/health.js → nitro/routes/health.mjs} +4 -3
- package/dist/nitro/setup/extend-loader.d.mts +19 -0
- package/dist/nitro/setup/extend-loader.mjs +129 -0
- package/dist/nitro/setup/file-watcher.d.mts +16 -0
- package/dist/nitro/setup/file-watcher.mjs +98 -0
- 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/nitro/setup/rollup-integration.mjs +90 -0
- 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/nitro/setup/ts-config.mjs +69 -0
- package/dist/nitro/setup.d.mts +12 -0
- package/dist/nitro/setup.mjs +234 -0
- package/dist/nitro/types.d.mts +374 -0
- package/dist/nitro/types.mjs +1 -0
- package/dist/nitro/virtual/generators.d.mts +31 -0
- package/dist/nitro/virtual/generators.mjs +113 -0
- package/dist/nitro/virtual/stubs.d.mts +20 -0
- package/dist/nitro/virtual/stubs.mjs +31 -0
- package/dist/{ecosystem/nuxt.d.ts → nuxt.d.mts} +1 -1
- package/dist/nuxt.mjs +109 -0
- package/dist/{graphql/server.d.ts → stubs/index.d.mts} +5 -1
- package/dist/stubs/index.mjs +1 -0
- package/package.json +102 -77
- package/dist/ecosystem/nuxt.js +0 -67
- package/dist/graphql/index.d.ts +0 -5
- package/dist/index.d.ts +0 -8
- package/dist/index.js +0 -264
- package/dist/rollup.js +0 -119
- package/dist/routes/apollo-server.d.ts +0 -6
- package/dist/routes/apollo-server.js +0 -89
- package/dist/routes/graphql-yoga.d.ts +0 -6
- package/dist/routes/graphql-yoga.js +0 -91
- package/dist/routes/health.d.ts +0 -6
- package/dist/types/index.d.ts +0 -128
- package/dist/types/standard-schema.d.ts +0 -59
- package/dist/utils/apollo.js +0 -61
- package/dist/utils/client-codegen.d.ts +0 -38
- package/dist/utils/client-codegen.js +0 -290
- package/dist/utils/define.js +0 -57
- package/dist/utils/index.d.ts +0 -39
- package/dist/utils/index.js +0 -250
- package/dist/utils/server-codegen.d.ts +0 -7
- package/dist/utils/server-codegen.js +0 -136
- package/dist/utils/type-generation.d.ts +0 -7
- package/dist/utils/type-generation.js +0 -287
- package/dist/vite.d.ts +0 -25
- package/dist/vite.js +0 -40
- /package/dist/{graphql/index.js → core/types/adapter.mjs} +0 -0
- /package/dist/{graphql/server.js → core/types/codegen.mjs} +0 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import { CHUNK_NAME_RESOLVERS, CHUNK_NAME_SCHEMAS, CHUNK_PATH_GRAPHQL, CHUNK_PATH_UNKNOWN, GRAPHQL_EXTENSIONS, RESOLVER_EXTENSIONS } from "../../core/constants.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/nitro/setup/rollup-integration.ts
|
|
4
|
+
/**
|
|
5
|
+
* Setup Rollup/Rolldown chunking configuration for GraphQL files
|
|
6
|
+
* Creates separate chunks for schemas and resolvers to optimize bundle size
|
|
7
|
+
*/
|
|
8
|
+
function setupRollupChunking(nitro) {
|
|
9
|
+
nitro.hooks.hook("rollup:before", (_, rollupConfig) => {
|
|
10
|
+
const manualChunks = rollupConfig.output?.manualChunks;
|
|
11
|
+
const chunkFiles = rollupConfig.output?.chunkFileNames;
|
|
12
|
+
if (!rollupConfig.output.inlineDynamicImports) {
|
|
13
|
+
rollupConfig.output.manualChunks = (id, _meta) => {
|
|
14
|
+
if (isGraphQLFile(id)) return getSchemaChunkName(id);
|
|
15
|
+
if (isResolverFile(id)) return getResolverChunkName(id);
|
|
16
|
+
if (typeof manualChunks === "function") return manualChunks(id, meta);
|
|
17
|
+
};
|
|
18
|
+
rollupConfig.output.advancedChunks = {
|
|
19
|
+
groups: [{
|
|
20
|
+
name: CHUNK_NAME_SCHEMAS,
|
|
21
|
+
test: /* @__PURE__ */ new RegExp(`\\.(${GRAPHQL_EXTENSIONS.map((e) => e.slice(1)).join("|")})$`)
|
|
22
|
+
}, {
|
|
23
|
+
name: CHUNK_NAME_RESOLVERS,
|
|
24
|
+
test: /\.resolver\.(ts|js)$/
|
|
25
|
+
}],
|
|
26
|
+
minSize: 0,
|
|
27
|
+
minShareCount: 1
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
rollupConfig.output.chunkFileNames = (chunkInfo) => {
|
|
31
|
+
if (chunkInfo.moduleIds && chunkInfo.moduleIds.some((id) => isGraphQLFile(id) || isResolverFile(id))) return CHUNK_PATH_GRAPHQL;
|
|
32
|
+
if (typeof chunkFiles === "function") return chunkFiles(chunkInfo);
|
|
33
|
+
return CHUNK_PATH_UNKNOWN;
|
|
34
|
+
};
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Check if a file is a GraphQL schema file
|
|
39
|
+
*/
|
|
40
|
+
function isGraphQLFile(id) {
|
|
41
|
+
return GRAPHQL_EXTENSIONS.some((ext) => id.endsWith(ext));
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Check if a file is a resolver file
|
|
45
|
+
*/
|
|
46
|
+
function isResolverFile(id) {
|
|
47
|
+
return RESOLVER_EXTENSIONS.some((ext) => id.endsWith(ext));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Get chunk name for GraphQL schema files
|
|
51
|
+
* All schemas are bundled into a single chunk since GraphQL server
|
|
52
|
+
* merges them at runtime anyway - no benefit from separate chunks
|
|
53
|
+
*/
|
|
54
|
+
function getSchemaChunkName(_id) {
|
|
55
|
+
return CHUNK_NAME_SCHEMAS;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Get chunk name for resolver files
|
|
59
|
+
* All resolvers are bundled into a single chunk since GraphQL server
|
|
60
|
+
* requires all resolvers to be registered at startup - no runtime lazy loading
|
|
61
|
+
*/
|
|
62
|
+
function getResolverChunkName(_id) {
|
|
63
|
+
return CHUNK_NAME_RESOLVERS;
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Configure external dependencies for Rollup
|
|
67
|
+
* Marks codegen and federation packages as external
|
|
68
|
+
*/
|
|
69
|
+
function setupRollupExternals(nitro) {
|
|
70
|
+
nitro.hooks.hook("rollup:before", (_, rollupConfig) => {
|
|
71
|
+
rollupConfig.external = rollupConfig.external || [];
|
|
72
|
+
const allExternals = [...["oxc-parser", "@oxc-parser"]];
|
|
73
|
+
if (!nitro.options.graphql?.federation?.enabled) allExternals.push(...[
|
|
74
|
+
"@apollo/subgraph",
|
|
75
|
+
"@apollo/federation-internals",
|
|
76
|
+
"@apollo/cache-control-types"
|
|
77
|
+
]);
|
|
78
|
+
if (Array.isArray(rollupConfig.external)) rollupConfig.external.push(...allExternals);
|
|
79
|
+
else if (typeof rollupConfig.external === "function") {
|
|
80
|
+
const originalExternal = rollupConfig.external;
|
|
81
|
+
rollupConfig.external = (id, parent, isResolved) => {
|
|
82
|
+
if (allExternals.some((external) => id.includes(external))) return true;
|
|
83
|
+
return originalExternal(id, parent, isResolved);
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
//#endregion
|
|
90
|
+
export { setupRollupChunking, setupRollupExternals };
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ENDPOINT_DEBUG, GRAPHQL_HTTP_METHODS } from "../../core/constants.mjs";
|
|
2
|
+
import { join } from "pathe";
|
|
3
|
+
import { fileURLToPath } from "node:url";
|
|
4
|
+
|
|
5
|
+
//#region src/nitro/setup/routes.ts
|
|
6
|
+
/**
|
|
7
|
+
* Register GraphQL route handlers
|
|
8
|
+
*/
|
|
9
|
+
function registerRouteHandlers(nitro) {
|
|
10
|
+
const runtime = fileURLToPath(new URL("../routes", import.meta.url));
|
|
11
|
+
const framework = nitro.options.graphql?.framework;
|
|
12
|
+
if (framework === "graphql-yoga") for (const method of GRAPHQL_HTTP_METHODS) nitro.options.handlers.push({
|
|
13
|
+
route: nitro.options.runtimeConfig.graphql?.endpoint?.graphql || "/api/graphql",
|
|
14
|
+
handler: join(runtime, "graphql-yoga"),
|
|
15
|
+
method
|
|
16
|
+
});
|
|
17
|
+
if (framework === "apollo-server") for (const method of GRAPHQL_HTTP_METHODS) nitro.options.handlers.push({
|
|
18
|
+
route: nitro.options.runtimeConfig.graphql?.endpoint?.graphql || "/api/graphql",
|
|
19
|
+
handler: join(runtime, "apollo-server"),
|
|
20
|
+
method
|
|
21
|
+
});
|
|
22
|
+
nitro.options.handlers.push({
|
|
23
|
+
route: nitro.options.runtimeConfig.graphql?.endpoint?.healthCheck || "/api/graphql/health",
|
|
24
|
+
handler: join(runtime, "health"),
|
|
25
|
+
method: "GET"
|
|
26
|
+
});
|
|
27
|
+
if (nitro.options.dev) nitro.options.handlers.push({
|
|
28
|
+
route: ENDPOINT_DEBUG,
|
|
29
|
+
handler: join(runtime, "debug"),
|
|
30
|
+
method: "GET"
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
//#endregion
|
|
35
|
+
export { registerRouteHandlers };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { Nitro, NitroTypes } from "nitro/types";
|
|
2
|
+
|
|
3
|
+
//#region src/nitro/setup/ts-config.d.ts
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Setup TypeScript path aliases for GraphQL types
|
|
7
|
+
* Called via nitro:config hook to extend tsconfig.json
|
|
8
|
+
*/
|
|
9
|
+
declare function setupTypeScriptPaths(nitro: Nitro, types: NitroTypes): void;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { setupTypeScriptPaths };
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { relativeWithDot } from "../../core/utils/imports.mjs";
|
|
2
|
+
import { getDefaultPaths, getTypesConfig, resolveFilePath } from "../paths.mjs";
|
|
3
|
+
import { dirname, join, resolve } from "pathe";
|
|
4
|
+
|
|
5
|
+
//#region src/nitro/setup/ts-config.ts
|
|
6
|
+
/**
|
|
7
|
+
* Setup TypeScript path aliases for GraphQL types
|
|
8
|
+
* Called via nitro:config hook to extend tsconfig.json
|
|
9
|
+
*/
|
|
10
|
+
function setupTypeScriptPaths(nitro, types) {
|
|
11
|
+
const tsconfigDir = dirname(resolve(nitro.options.buildDir, nitro.options.typescript.tsconfigPath));
|
|
12
|
+
types.tsConfig ||= {};
|
|
13
|
+
types.tsConfig.compilerOptions ??= {};
|
|
14
|
+
types.tsConfig.compilerOptions.paths ??= {};
|
|
15
|
+
types.tsConfig.include = types.tsConfig.include || [];
|
|
16
|
+
const placeholders = getDefaultPaths(nitro);
|
|
17
|
+
const typesConfig = getTypesConfig(nitro);
|
|
18
|
+
setupServerTypesPath(nitro, types, tsconfigDir, placeholders, typesConfig);
|
|
19
|
+
setupClientTypesPath(nitro, types, tsconfigDir, placeholders, typesConfig);
|
|
20
|
+
setupSchemaPath(nitro, types, tsconfigDir);
|
|
21
|
+
setupExternalServicePaths(nitro, types, tsconfigDir, placeholders, typesConfig);
|
|
22
|
+
types.tsConfig.include.push(relativeWithDot(tsconfigDir, join(placeholders.typesDir, "graphql.d.ts")));
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Setup #graphql/server path alias
|
|
26
|
+
*/
|
|
27
|
+
function setupServerTypesPath(nitro, types, tsconfigDir, placeholders, typesConfig) {
|
|
28
|
+
const serverTypesPath = resolveFilePath(typesConfig.server, typesConfig.enabled, true, "{typesDir}/nitro-graphql-server.d.ts", placeholders);
|
|
29
|
+
if (serverTypesPath) {
|
|
30
|
+
types.tsConfig.compilerOptions.paths["#graphql/server"] = [relativeWithDot(tsconfigDir, serverTypesPath)];
|
|
31
|
+
types.tsConfig.include.push(relativeWithDot(tsconfigDir, serverTypesPath));
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Setup #graphql/client path alias
|
|
36
|
+
*/
|
|
37
|
+
function setupClientTypesPath(nitro, types, tsconfigDir, placeholders, typesConfig) {
|
|
38
|
+
const clientTypesPath = resolveFilePath(typesConfig.client, typesConfig.enabled, true, "{typesDir}/nitro-graphql-client.d.ts", placeholders);
|
|
39
|
+
if (clientTypesPath) {
|
|
40
|
+
types.tsConfig.compilerOptions.paths["#graphql/client"] = [relativeWithDot(tsconfigDir, clientTypesPath)];
|
|
41
|
+
types.tsConfig.include.push(relativeWithDot(tsconfigDir, clientTypesPath));
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Setup #graphql/schema path alias
|
|
46
|
+
*/
|
|
47
|
+
function setupSchemaPath(nitro, types, tsconfigDir) {
|
|
48
|
+
types.tsConfig.compilerOptions.paths["#graphql/schema"] = [relativeWithDot(tsconfigDir, join(nitro.graphql.serverDir, "schema.ts"))];
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Setup #graphql/client/{serviceName} path aliases for external services
|
|
52
|
+
*/
|
|
53
|
+
function setupExternalServicePaths(nitro, types, tsconfigDir, placeholders, typesConfig) {
|
|
54
|
+
if (!nitro.options.graphql?.externalServices?.length) return;
|
|
55
|
+
for (const service of nitro.options.graphql.externalServices) {
|
|
56
|
+
const servicePlaceholders = {
|
|
57
|
+
...placeholders,
|
|
58
|
+
serviceName: service.name
|
|
59
|
+
};
|
|
60
|
+
const externalTypesPath = resolveFilePath(service.paths?.types ?? typesConfig.external, typesConfig.enabled, true, "{typesDir}/nitro-graphql-client-{serviceName}.d.ts", servicePlaceholders);
|
|
61
|
+
if (externalTypesPath) {
|
|
62
|
+
types.tsConfig.compilerOptions.paths[`#graphql/client/${service.name}`] = [relativeWithDot(tsconfigDir, externalTypesPath)];
|
|
63
|
+
types.tsConfig.include.push(relativeWithDot(tsconfigDir, externalTypesPath));
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
//#endregion
|
|
69
|
+
export { setupTypeScriptPaths };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { resolveSecurityConfig } from "./setup/logging.mjs";
|
|
2
|
+
import { Nitro } from "nitro/types";
|
|
3
|
+
|
|
4
|
+
//#region src/nitro/setup.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Main setup function for nitro-graphql
|
|
8
|
+
* Coordinates all initialization steps for the module
|
|
9
|
+
*/
|
|
10
|
+
declare function setupNitroGraphQL(nitro: Nitro): Promise<void>;
|
|
11
|
+
//#endregion
|
|
12
|
+
export { resolveSecurityConfig, setupNitroGraphQL };
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
import { FRAMEWORK_NITRO, FRAMEWORK_NUXT, LOG_TAG } from "../core/constants.mjs";
|
|
2
|
+
import { generateDirectiveSchemas } from "../core/utils/directive-parser.mjs";
|
|
3
|
+
import { validateExternalServices } from "../core/validation/external-services.mjs";
|
|
4
|
+
import { NitroAdapter } from "./adapter.mjs";
|
|
5
|
+
import { getDefaultPaths } from "./paths.mjs";
|
|
6
|
+
import { generateClientTypes, generateServerTypes } from "./codegen.mjs";
|
|
7
|
+
import { DEFAULT_RUNTIME_CONFIG, DEFAULT_TYPESCRIPT_STRICT, DEFAULT_TYPES_CONFIG } from "./config.mjs";
|
|
8
|
+
import { rollupConfig } from "./rollup.mjs";
|
|
9
|
+
import { resolveExtendConfig, resolveExtendDirs } from "./setup/extend-loader.mjs";
|
|
10
|
+
import { getWatchDirectories, setupFileWatcher } from "./setup/file-watcher.mjs";
|
|
11
|
+
import { logStartupInfo, resolveSecurityConfig } from "./setup/logging.mjs";
|
|
12
|
+
import { setupRollupChunking, setupRollupExternals } from "./setup/rollup-integration.mjs";
|
|
13
|
+
import { registerRouteHandlers } from "./setup/routes.mjs";
|
|
14
|
+
import { setupTypeScriptPaths } from "./setup/ts-config.mjs";
|
|
15
|
+
import defu from "defu";
|
|
16
|
+
import consola from "consola";
|
|
17
|
+
import { relative, resolve } from "pathe";
|
|
18
|
+
|
|
19
|
+
//#region src/nitro/setup.ts
|
|
20
|
+
const logger = consola.withTag(LOG_TAG);
|
|
21
|
+
/**
|
|
22
|
+
* Scan all GraphQL files and update Nitro state
|
|
23
|
+
*/
|
|
24
|
+
async function scanAllGraphQLFiles(nitro) {
|
|
25
|
+
const directivesResult = await NitroAdapter.scanDirectives(nitro);
|
|
26
|
+
nitro.scanDirectives = directivesResult.items;
|
|
27
|
+
if (!nitro.scanSchemas) nitro.scanSchemas = [];
|
|
28
|
+
const directivesPath = await generateDirectiveSchemas(nitro, directivesResult.items);
|
|
29
|
+
const schemas = (await NitroAdapter.scanSchemas(nitro)).items;
|
|
30
|
+
if (directivesPath && !schemas.includes(directivesPath)) schemas.push(directivesPath);
|
|
31
|
+
nitro.scanSchemas = schemas;
|
|
32
|
+
nitro.scanDocuments = (await NitroAdapter.scanDocuments(nitro)).items;
|
|
33
|
+
nitro.scanResolvers = (await NitroAdapter.scanResolvers(nitro)).items;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Main setup function for nitro-graphql
|
|
37
|
+
* Coordinates all initialization steps for the module
|
|
38
|
+
*/
|
|
39
|
+
async function setupNitroGraphQL(nitro) {
|
|
40
|
+
const serverEnabled = nitro.options.graphql?.server !== false;
|
|
41
|
+
initializeConfiguration(nitro, serverEnabled);
|
|
42
|
+
validateConfiguration(nitro);
|
|
43
|
+
setupBuildDirectories(nitro);
|
|
44
|
+
if (serverEnabled) {
|
|
45
|
+
setupRollupExternals(nitro);
|
|
46
|
+
setupRollupChunking(nitro);
|
|
47
|
+
}
|
|
48
|
+
initializeRuntimeConfig(nitro);
|
|
49
|
+
setupFileWatching(nitro, serverEnabled, await resolveExtendDirs(nitro));
|
|
50
|
+
await scanGraphQLFiles(nitro, serverEnabled);
|
|
51
|
+
await resolveExtendConfig(nitro);
|
|
52
|
+
if (serverEnabled) setupDevHooks(nitro);
|
|
53
|
+
if (serverEnabled) await rollupConfig(nitro);
|
|
54
|
+
await generateTypes(nitro, serverEnabled);
|
|
55
|
+
setupCloseHooks(nitro, serverEnabled);
|
|
56
|
+
if (serverEnabled) registerRouteHandlers(nitro);
|
|
57
|
+
setupTypeScriptConfiguration(nitro);
|
|
58
|
+
setupNuxtIntegration(nitro);
|
|
59
|
+
logStartupInfo(nitro, serverEnabled);
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Initialize default configuration values
|
|
63
|
+
*/
|
|
64
|
+
function initializeConfiguration(nitro, serverEnabled) {
|
|
65
|
+
nitro.options.graphql ||= {};
|
|
66
|
+
nitro.options.graphql.types = defu(nitro.options.graphql.types, DEFAULT_TYPES_CONFIG);
|
|
67
|
+
if (serverEnabled && !nitro.options.graphql?.framework) logger.warn("No GraphQL framework specified. Please set graphql.framework to \"graphql-yoga\" or \"apollo-server\".");
|
|
68
|
+
const defaultPaths = getDefaultPaths(nitro);
|
|
69
|
+
nitro.graphql ||= {
|
|
70
|
+
buildDir: "",
|
|
71
|
+
watchDirs: [],
|
|
72
|
+
clientDir: defaultPaths.clientDir,
|
|
73
|
+
serverDir: defaultPaths.serverDir,
|
|
74
|
+
dir: {
|
|
75
|
+
build: relative(nitro.options.rootDir, nitro.options.buildDir),
|
|
76
|
+
client: "graphql",
|
|
77
|
+
server: "server"
|
|
78
|
+
}
|
|
79
|
+
};
|
|
80
|
+
nitro.scanSchemas ||= [];
|
|
81
|
+
nitro.scanResolvers ||= [];
|
|
82
|
+
nitro.scanDirectives ||= [];
|
|
83
|
+
nitro.scanDocuments ||= [];
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Validate external services configuration
|
|
87
|
+
*/
|
|
88
|
+
function validateConfiguration(nitro) {
|
|
89
|
+
if (nitro.options.graphql?.externalServices?.length) {
|
|
90
|
+
const validationErrors = validateExternalServices(nitro.options.graphql.externalServices);
|
|
91
|
+
if (validationErrors.length > 0) {
|
|
92
|
+
logger.error("External services configuration errors:");
|
|
93
|
+
for (const error of validationErrors) logger.error(` - ${error}`);
|
|
94
|
+
throw new Error("Invalid external services configuration");
|
|
95
|
+
}
|
|
96
|
+
logger.info(`Configured ${nitro.options.graphql.externalServices.length} external GraphQL services`);
|
|
97
|
+
}
|
|
98
|
+
if (nitro.options.graphql?.federation?.enabled) logger.info(`Apollo Federation enabled for service: ${nitro.options.graphql.federation.serviceName || "unnamed"}`);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Setup build directories
|
|
102
|
+
*/
|
|
103
|
+
function setupBuildDirectories(nitro) {
|
|
104
|
+
const graphqlBuildDir = resolve(nitro.options.buildDir, "graphql");
|
|
105
|
+
nitro.graphql.buildDir = graphqlBuildDir;
|
|
106
|
+
switch (nitro.options.framework.name) {
|
|
107
|
+
case FRAMEWORK_NUXT:
|
|
108
|
+
nitro.graphql.dir.client = relative(nitro.options.rootDir, nitro.graphql.clientDir);
|
|
109
|
+
nitro.graphql.dir.server = relative(nitro.options.rootDir, nitro.graphql.serverDir);
|
|
110
|
+
break;
|
|
111
|
+
case FRAMEWORK_NITRO:
|
|
112
|
+
nitro.graphql.dir.client = relative(nitro.options.rootDir, nitro.graphql.clientDir);
|
|
113
|
+
nitro.graphql.dir.server = relative(nitro.options.rootDir, nitro.graphql.serverDir);
|
|
114
|
+
break;
|
|
115
|
+
default: break;
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Initialize runtime configuration
|
|
120
|
+
*/
|
|
121
|
+
function initializeRuntimeConfig(nitro) {
|
|
122
|
+
const securityConfig = resolveSecurityConfig(nitro.options.graphql?.security);
|
|
123
|
+
nitro.options.runtimeConfig.graphql = defu(nitro.options.runtimeConfig.graphql || {}, {
|
|
124
|
+
...DEFAULT_RUNTIME_CONFIG,
|
|
125
|
+
security: securityConfig
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
/**
|
|
129
|
+
* Setup file watching for development mode
|
|
130
|
+
*/
|
|
131
|
+
function setupFileWatching(nitro, serverEnabled, extendDirs = []) {
|
|
132
|
+
const watchDirs = serverEnabled ? getWatchDirectories(nitro, extendDirs) : [nitro.graphql.clientDir].filter(Boolean);
|
|
133
|
+
nitro.graphql.watchDirs = watchDirs;
|
|
134
|
+
const watcher = setupFileWatcher(nitro, watchDirs);
|
|
135
|
+
nitro.hooks.hook("close", () => {
|
|
136
|
+
watcher.close();
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Scan all GraphQL files (schemas, resolvers, directives, documents)
|
|
141
|
+
*/
|
|
142
|
+
async function scanGraphQLFiles(nitro, serverEnabled) {
|
|
143
|
+
const skipLocalScan = nitro.options.graphql?.skipLocalScan === true;
|
|
144
|
+
const extendSources = nitro.options.graphql?.extend;
|
|
145
|
+
if (skipLocalScan && extendSources?.length) {
|
|
146
|
+
logger.info(`Using ${extendSources.length} extend source(s), skipping local scanning`);
|
|
147
|
+
nitro.scanSchemas = [];
|
|
148
|
+
nitro.scanResolvers = [];
|
|
149
|
+
nitro.scanDirectives = [];
|
|
150
|
+
nitro.scanDocuments = (await NitroAdapter.scanDocuments(nitro)).items;
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
if (serverEnabled) await scanAllGraphQLFiles(nitro);
|
|
154
|
+
else nitro.scanDocuments = (await NitroAdapter.scanDocuments(nitro)).items;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Setup dev mode hooks for rescanning files
|
|
158
|
+
*/
|
|
159
|
+
function setupDevHooks(nitro) {
|
|
160
|
+
let hasShownInitialLogs = false;
|
|
161
|
+
nitro.hooks.hook("dev:start", async () => {
|
|
162
|
+
await scanAllGraphQLFiles(nitro);
|
|
163
|
+
await resolveExtendConfig(nitro);
|
|
164
|
+
if (nitro.options.dev && !hasShownInitialLogs) {
|
|
165
|
+
hasShownInitialLogs = true;
|
|
166
|
+
logResolverDiagnostics(nitro);
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
/**
|
|
171
|
+
* Log resolver diagnostics for development
|
|
172
|
+
*/
|
|
173
|
+
function logResolverDiagnostics(nitro) {
|
|
174
|
+
const resolvers = nitro.scanResolvers || [];
|
|
175
|
+
if (resolvers.length > 0) {
|
|
176
|
+
const totalExports = resolvers.reduce((sum, r) => sum + r.imports.length, 0);
|
|
177
|
+
const typeCount = {
|
|
178
|
+
query: 0,
|
|
179
|
+
mutation: 0,
|
|
180
|
+
resolver: 0,
|
|
181
|
+
type: 0,
|
|
182
|
+
subscription: 0,
|
|
183
|
+
directive: 0
|
|
184
|
+
};
|
|
185
|
+
for (const resolver of resolvers) for (const imp of resolver.imports) if (imp.type in typeCount) typeCount[imp.type]++;
|
|
186
|
+
const breakdown = [];
|
|
187
|
+
if (typeCount.query > 0) breakdown.push(`${typeCount.query} query`);
|
|
188
|
+
if (typeCount.mutation > 0) breakdown.push(`${typeCount.mutation} mutation`);
|
|
189
|
+
if (typeCount.resolver > 0) breakdown.push(`${typeCount.resolver} resolver`);
|
|
190
|
+
if (typeCount.type > 0) breakdown.push(`${typeCount.type} type`);
|
|
191
|
+
if (typeCount.subscription > 0) breakdown.push(`${typeCount.subscription} subscription`);
|
|
192
|
+
if (typeCount.directive > 0) breakdown.push(`${typeCount.directive} directive`);
|
|
193
|
+
if (breakdown.length > 0) logger.success(`${totalExports} resolver export(s): ${breakdown.join(", ")}`);
|
|
194
|
+
} else logger.warn("No resolvers found. Check /_nitro/graphql/debug for details.");
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* Generate server and client types
|
|
198
|
+
*/
|
|
199
|
+
async function generateTypes(nitro, serverEnabled) {
|
|
200
|
+
if (serverEnabled) {
|
|
201
|
+
await generateServerTypes(nitro);
|
|
202
|
+
await generateClientTypes(nitro, { isInitial: true });
|
|
203
|
+
} else await generateClientTypes(nitro, { isInitial: true });
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Setup close hooks for final type generation
|
|
207
|
+
*/
|
|
208
|
+
function setupCloseHooks(nitro, serverEnabled) {
|
|
209
|
+
nitro.hooks.hook("close", async () => {
|
|
210
|
+
if (serverEnabled) await generateServerTypes(nitro, { silent: true });
|
|
211
|
+
await generateClientTypes(nitro, { silent: true });
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Setup TypeScript configuration and path aliases
|
|
216
|
+
*/
|
|
217
|
+
function setupTypeScriptConfiguration(nitro) {
|
|
218
|
+
nitro.options.typescript.strict = DEFAULT_TYPESCRIPT_STRICT;
|
|
219
|
+
nitro.hooks.hook("types:extend", (types) => {
|
|
220
|
+
setupTypeScriptPaths(nitro, types);
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* Setup Nuxt-specific integration
|
|
225
|
+
*/
|
|
226
|
+
function setupNuxtIntegration(nitro) {
|
|
227
|
+
if (nitro.options.framework?.name === FRAMEWORK_NUXT && nitro.options.graphql?.externalServices?.length) nitro.hooks.hook("build:before", () => {
|
|
228
|
+
const nuxtOptions = nitro._nuxt?.options;
|
|
229
|
+
if (nuxtOptions) nuxtOptions.nitroGraphqlExternalServices = nitro.options.graphql?.externalServices || [];
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
//#endregion
|
|
234
|
+
export { resolveSecurityConfig, setupNitroGraphQL };
|