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
package/dist/index.js
DELETED
|
@@ -1,264 +0,0 @@
|
|
|
1
|
-
import { generateDirectiveSchemas } from "./utils/directive-parser.js";
|
|
2
|
-
import { generateLayerIgnorePatterns, getLayerAppDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives, scanDocs, scanResolvers, scanSchemas, validateExternalServices } from "./utils/index.js";
|
|
3
|
-
import { clientTypeGeneration, serverTypeGeneration } from "./utils/type-generation.js";
|
|
4
|
-
import { rollupConfig } from "./rollup.js";
|
|
5
|
-
import { existsSync, mkdirSync, writeFileSync } from "node:fs";
|
|
6
|
-
import { fileURLToPath } from "node:url";
|
|
7
|
-
import { watch } from "chokidar";
|
|
8
|
-
import consola from "consola";
|
|
9
|
-
import defu from "defu";
|
|
10
|
-
import { dirname, join, relative, resolve } from "pathe";
|
|
11
|
-
|
|
12
|
-
//#region src/index.ts
|
|
13
|
-
var src_default = defineNitroModule({
|
|
14
|
-
name: "nitro-graphql",
|
|
15
|
-
async setup(nitro) {
|
|
16
|
-
if (!nitro.options.graphql?.framework) consola.warn("No GraphQL framework specified. Please set graphql.framework to \"graphql-yoga\" or \"apollo-server\".");
|
|
17
|
-
if (nitro.options.graphql?.externalServices?.length) {
|
|
18
|
-
const validationErrors = validateExternalServices(nitro.options.graphql.externalServices);
|
|
19
|
-
if (validationErrors.length > 0) {
|
|
20
|
-
consola.error("External services configuration errors:");
|
|
21
|
-
for (const error of validationErrors) consola.error(` - ${error}`);
|
|
22
|
-
throw new Error("Invalid external services configuration");
|
|
23
|
-
}
|
|
24
|
-
consola.info(`Configured ${nitro.options.graphql.externalServices.length} external GraphQL services`);
|
|
25
|
-
}
|
|
26
|
-
nitro.graphql ||= {
|
|
27
|
-
buildDir: "",
|
|
28
|
-
watchDirs: [],
|
|
29
|
-
clientDir: "",
|
|
30
|
-
serverDir: resolve(nitro.options.rootDir, "server", "graphql"),
|
|
31
|
-
dir: {
|
|
32
|
-
build: relative(nitro.options.rootDir, nitro.options.buildDir),
|
|
33
|
-
client: "graphql",
|
|
34
|
-
server: "server"
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
nitro.hooks.hook("rollup:before", (_, rollupConfig$1) => {
|
|
38
|
-
rollupConfig$1.external = rollupConfig$1.external || [];
|
|
39
|
-
const codegenExternals = ["oxc-parser", "@oxc-parser"];
|
|
40
|
-
if (Array.isArray(rollupConfig$1.external)) rollupConfig$1.external.push(...codegenExternals);
|
|
41
|
-
else if (typeof rollupConfig$1.external === "function") {
|
|
42
|
-
const originalExternal = rollupConfig$1.external;
|
|
43
|
-
rollupConfig$1.external = (id, parent, isResolved) => {
|
|
44
|
-
if (codegenExternals.some((external) => id.includes(external))) return true;
|
|
45
|
-
return originalExternal(id, parent, isResolved);
|
|
46
|
-
};
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
nitro.options.runtimeConfig.graphql = defu(nitro.options.runtimeConfig.graphql || {}, {
|
|
50
|
-
endpoint: {
|
|
51
|
-
graphql: "/api/graphql",
|
|
52
|
-
healthCheck: "/api/graphql/health"
|
|
53
|
-
},
|
|
54
|
-
playground: true
|
|
55
|
-
});
|
|
56
|
-
if (nitro.options.graphql?.federation?.enabled) consola.info(`Apollo Federation enabled for service: ${nitro.options.graphql.federation.serviceName || "unnamed"}`);
|
|
57
|
-
const graphqlBuildDir = resolve(nitro.options.buildDir, "graphql");
|
|
58
|
-
nitro.graphql.buildDir = graphqlBuildDir;
|
|
59
|
-
const watchDirs = [];
|
|
60
|
-
switch (nitro.options.framework.name) {
|
|
61
|
-
case "nuxt": {
|
|
62
|
-
if (!nitro.graphql.clientDir) {
|
|
63
|
-
nitro.graphql.clientDir = resolve(nitro.options.rootDir, "app", "graphql");
|
|
64
|
-
nitro.graphql.dir.client = "app/graphql";
|
|
65
|
-
}
|
|
66
|
-
if (!nitro.options.graphql?.serverDir) nitro.graphql.serverDir = resolve(nitro.options.rootDir, "server", "graphql");
|
|
67
|
-
watchDirs.push(nitro.graphql.clientDir);
|
|
68
|
-
const layerServerDirs = getLayerServerDirectories(nitro);
|
|
69
|
-
const layerAppDirs = getLayerAppDirectories(nitro);
|
|
70
|
-
for (const layerServerDir of layerServerDirs) watchDirs.push(join(layerServerDir, "graphql"));
|
|
71
|
-
for (const layerAppDir of layerAppDirs) watchDirs.push(join(layerAppDir, "graphql"));
|
|
72
|
-
break;
|
|
73
|
-
}
|
|
74
|
-
case "nitro":
|
|
75
|
-
if (!nitro.options.graphql?.serverDir) nitro.graphql.serverDir = resolve(nitro.options.rootDir, "server", "graphql");
|
|
76
|
-
nitro.graphql.clientDir = resolve(nitro.options.rootDir, "graphql");
|
|
77
|
-
nitro.graphql.dir.client = "graphql";
|
|
78
|
-
watchDirs.push(nitro.graphql.clientDir);
|
|
79
|
-
watchDirs.push(nitro.graphql.serverDir);
|
|
80
|
-
break;
|
|
81
|
-
default:
|
|
82
|
-
}
|
|
83
|
-
if (nitro.options.graphql?.externalServices?.length) {
|
|
84
|
-
for (const service of nitro.options.graphql.externalServices) if (service.documents?.length) for (const pattern of service.documents) {
|
|
85
|
-
if (!pattern) continue;
|
|
86
|
-
const baseDir = pattern.split("**")[0]?.replace(/\/$/, "") || ".";
|
|
87
|
-
const resolvedDir = resolve(nitro.options.rootDir, baseDir);
|
|
88
|
-
if (!watchDirs.includes(resolvedDir)) watchDirs.push(resolvedDir);
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
const watcher = watch(watchDirs, {
|
|
92
|
-
persistent: true,
|
|
93
|
-
ignoreInitial: true,
|
|
94
|
-
ignored: [...nitro.options.ignore, ...generateLayerIgnorePatterns(nitro)]
|
|
95
|
-
}).on("all", async (_, path) => {
|
|
96
|
-
if (path.endsWith(".graphql") || path.endsWith(".gql")) if (path.includes(nitro.graphql.serverDir) || path.includes("server/graphql") || path.includes("server\\graphql")) {
|
|
97
|
-
await serverTypeGeneration(nitro);
|
|
98
|
-
await clientTypeGeneration(nitro);
|
|
99
|
-
await nitro.hooks.callHook("dev:reload");
|
|
100
|
-
} else await clientTypeGeneration(nitro);
|
|
101
|
-
});
|
|
102
|
-
nitro.hooks.hook("close", () => {
|
|
103
|
-
watcher.close();
|
|
104
|
-
});
|
|
105
|
-
const tsconfigDir = dirname(resolve(nitro.options.buildDir, nitro.options.typescript.tsconfigPath));
|
|
106
|
-
const typesDir = resolve(nitro.options.buildDir, "types");
|
|
107
|
-
nitro.scanSchemas = await scanSchemas(nitro);
|
|
108
|
-
nitro.scanDocuments = await scanDocs(nitro);
|
|
109
|
-
nitro.scanResolvers = await scanResolvers(nitro);
|
|
110
|
-
const directives = await scanDirectives(nitro);
|
|
111
|
-
nitro.scanDirectives = directives;
|
|
112
|
-
await generateDirectiveSchemas(nitro, directives);
|
|
113
|
-
nitro.hooks.hook("dev:start", async () => {
|
|
114
|
-
nitro.scanSchemas = await scanSchemas(nitro);
|
|
115
|
-
nitro.scanResolvers = await scanResolvers(nitro);
|
|
116
|
-
const directives$1 = await scanDirectives(nitro);
|
|
117
|
-
nitro.scanDirectives = directives$1;
|
|
118
|
-
await generateDirectiveSchemas(nitro, directives$1);
|
|
119
|
-
nitro.scanDocuments = await scanDocs(nitro);
|
|
120
|
-
});
|
|
121
|
-
await rollupConfig(nitro);
|
|
122
|
-
await serverTypeGeneration(nitro);
|
|
123
|
-
await clientTypeGeneration(nitro);
|
|
124
|
-
nitro.hooks.hook("close", async () => {
|
|
125
|
-
await serverTypeGeneration(nitro);
|
|
126
|
-
await clientTypeGeneration(nitro);
|
|
127
|
-
});
|
|
128
|
-
const runtime = fileURLToPath(new URL("routes", import.meta.url));
|
|
129
|
-
const methods = [
|
|
130
|
-
"GET",
|
|
131
|
-
"POST",
|
|
132
|
-
"OPTIONS"
|
|
133
|
-
];
|
|
134
|
-
if (nitro.options.graphql?.framework === "graphql-yoga") for (const method of methods) nitro.options.handlers.push({
|
|
135
|
-
route: nitro.options.runtimeConfig.graphql?.endpoint?.graphql || "/api/graphql",
|
|
136
|
-
handler: join(runtime, "graphql-yoga"),
|
|
137
|
-
method
|
|
138
|
-
});
|
|
139
|
-
if (nitro.options.graphql?.framework === "apollo-server") for (const method of methods) nitro.options.handlers.push({
|
|
140
|
-
route: nitro.options.runtimeConfig.graphql?.endpoint?.graphql || "/api/graphql",
|
|
141
|
-
handler: join(runtime, "apollo-server"),
|
|
142
|
-
method
|
|
143
|
-
});
|
|
144
|
-
nitro.options.handlers.push({
|
|
145
|
-
route: nitro.options.runtimeConfig.graphql?.endpoint?.healthCheck || "/api/graphql/health",
|
|
146
|
-
handler: join(runtime, "health"),
|
|
147
|
-
method: "GET"
|
|
148
|
-
});
|
|
149
|
-
if (nitro.options.imports) {
|
|
150
|
-
nitro.options.imports.presets ??= [];
|
|
151
|
-
nitro.options.imports.presets.push({
|
|
152
|
-
from: fileURLToPath(new URL("utils/define", import.meta.url)),
|
|
153
|
-
imports: [
|
|
154
|
-
"defineResolver",
|
|
155
|
-
"defineMutation",
|
|
156
|
-
"defineQuery",
|
|
157
|
-
"defineSubscription",
|
|
158
|
-
"defineType",
|
|
159
|
-
"defineGraphQLConfig",
|
|
160
|
-
"defineSchema",
|
|
161
|
-
"defineDirective"
|
|
162
|
-
]
|
|
163
|
-
});
|
|
164
|
-
}
|
|
165
|
-
nitro.hooks.hook("rollup:before", (_, rollupConfig$1) => {
|
|
166
|
-
const manualChunks = rollupConfig$1.output?.manualChunks;
|
|
167
|
-
const chunkFiles = rollupConfig$1.output?.chunkFileNames;
|
|
168
|
-
if (!rollupConfig$1.output.inlineDynamicImports) rollupConfig$1.output.manualChunks = (id, meta) => {
|
|
169
|
-
if (id.endsWith(".graphql") || id.endsWith(".gql")) return "schemas";
|
|
170
|
-
if (id.endsWith(".resolver.ts")) return "resolvers";
|
|
171
|
-
if (typeof manualChunks === "function") return manualChunks(id, meta);
|
|
172
|
-
};
|
|
173
|
-
rollupConfig$1.output.chunkFileNames = (chunkInfo) => {
|
|
174
|
-
if (chunkInfo.moduleIds && chunkInfo.moduleIds.some((id) => id.endsWith(".graphql") || id.endsWith(".resolver.ts") || id.endsWith(".gql"))) return `chunks/graphql/[name].mjs`;
|
|
175
|
-
if (typeof chunkFiles === "function") return chunkFiles(chunkInfo);
|
|
176
|
-
return `chunks/_/[name].mjs`;
|
|
177
|
-
};
|
|
178
|
-
});
|
|
179
|
-
nitro.options.typescript.strict = true;
|
|
180
|
-
nitro.hooks.hook("types:extend", (types) => {
|
|
181
|
-
types.tsConfig ||= {};
|
|
182
|
-
types.tsConfig.compilerOptions ??= {};
|
|
183
|
-
types.tsConfig.compilerOptions.paths ??= {};
|
|
184
|
-
types.tsConfig.compilerOptions.paths["#graphql/server"] = [relativeWithDot(tsconfigDir, join(typesDir, "nitro-graphql-server.d.ts"))];
|
|
185
|
-
types.tsConfig.compilerOptions.paths["#graphql/client"] = [relativeWithDot(tsconfigDir, join(typesDir, "nitro-graphql-client.d.ts"))];
|
|
186
|
-
types.tsConfig.compilerOptions.paths["#graphql/schema"] = [relativeWithDot(tsconfigDir, join(nitro.graphql.serverDir, "schema.ts"))];
|
|
187
|
-
if (nitro.options.graphql?.externalServices?.length) for (const service of nitro.options.graphql.externalServices) types.tsConfig.compilerOptions.paths[`#graphql/client/${service.name}`] = [relativeWithDot(tsconfigDir, join(typesDir, `nitro-graphql-client-${service.name}.d.ts`))];
|
|
188
|
-
types.tsConfig.include = types.tsConfig.include || [];
|
|
189
|
-
types.tsConfig.include.push(relativeWithDot(tsconfigDir, join(typesDir, "nitro-graphql-server.d.ts")), relativeWithDot(tsconfigDir, join(typesDir, "nitro-graphql-client.d.ts")), relativeWithDot(tsconfigDir, join(typesDir, "graphql.d.ts")));
|
|
190
|
-
if (nitro.options.graphql?.externalServices?.length) for (const service of nitro.options.graphql.externalServices) types.tsConfig.include.push(relativeWithDot(tsconfigDir, join(typesDir, `nitro-graphql-client-${service.name}.d.ts`)));
|
|
191
|
-
});
|
|
192
|
-
if (nitro.options.framework?.name === "nuxt" && nitro.options.graphql?.externalServices?.length) nitro.hooks.hook("build:before", () => {
|
|
193
|
-
const nuxtOptions = nitro._nuxt?.options;
|
|
194
|
-
if (nuxtOptions) nuxtOptions.nitroGraphqlExternalServices = nitro.options.graphql?.externalServices || [];
|
|
195
|
-
});
|
|
196
|
-
if (!existsSync(join(nitro.options.rootDir, "graphql.config.ts"))) {
|
|
197
|
-
const schemaPath = relativeWithDot(nitro.options.rootDir, resolve(nitro.graphql.buildDir, "schema.graphql"));
|
|
198
|
-
const documentsPath = relativeWithDot(nitro.options.rootDir, resolve(nitro.graphql.clientDir, "**/*.{graphql,js,ts,jsx,tsx}"));
|
|
199
|
-
writeFileSync(join(nitro.options.rootDir, "graphql.config.ts"), `
|
|
200
|
-
import type { IGraphQLConfig } from 'graphql-config'
|
|
201
|
-
|
|
202
|
-
export default <IGraphQLConfig> {
|
|
203
|
-
projects: {
|
|
204
|
-
default: {
|
|
205
|
-
schema: [
|
|
206
|
-
'${schemaPath}',
|
|
207
|
-
],
|
|
208
|
-
documents: [
|
|
209
|
-
'${documentsPath}',
|
|
210
|
-
],
|
|
211
|
-
},
|
|
212
|
-
},
|
|
213
|
-
}`, "utf-8");
|
|
214
|
-
}
|
|
215
|
-
if (!existsSync(nitro.graphql.serverDir)) mkdirSync(nitro.graphql.serverDir, { recursive: true });
|
|
216
|
-
if (!existsSync(join(nitro.graphql.serverDir, "schema.ts"))) writeFileSync(join(nitro.graphql.serverDir, "schema.ts"), `
|
|
217
|
-
import { defineSchema } from 'nitro-graphql/utils/define'
|
|
218
|
-
|
|
219
|
-
export default defineSchema({
|
|
220
|
-
|
|
221
|
-
})
|
|
222
|
-
`, "utf-8");
|
|
223
|
-
if (!existsSync(join(nitro.graphql.serverDir, "config.ts"))) writeFileSync(join(nitro.graphql.serverDir, "config.ts"), `// Example GraphQL config file please change it to your needs
|
|
224
|
-
// import * as tables from '../drizzle/schema/index'
|
|
225
|
-
// import { useDatabase } from '../utils/useDb'
|
|
226
|
-
import { defineGraphQLConfig } from 'nitro-graphql/utils/define'
|
|
227
|
-
|
|
228
|
-
export default defineGraphQLConfig({
|
|
229
|
-
// graphql-yoga example config
|
|
230
|
-
// context: () => {
|
|
231
|
-
// return {
|
|
232
|
-
// context: {
|
|
233
|
-
// useDatabase,
|
|
234
|
-
// tables,
|
|
235
|
-
// },
|
|
236
|
-
// }
|
|
237
|
-
// },
|
|
238
|
-
})
|
|
239
|
-
`, "utf-8");
|
|
240
|
-
if (!existsSync(join(nitro.graphql.serverDir, "context.ts"))) writeFileSync(join(nitro.graphql.serverDir, "context.ts"), `// Example context definition - please change it to your needs
|
|
241
|
-
// import type { Database } from '../utils/useDb'
|
|
242
|
-
|
|
243
|
-
declare module 'h3' {
|
|
244
|
-
interface H3EventContext {
|
|
245
|
-
// Add your custom context properties here
|
|
246
|
-
// useDatabase: () => Database
|
|
247
|
-
// tables: typeof import('../drizzle/schema')
|
|
248
|
-
// auth?: {
|
|
249
|
-
// user?: {
|
|
250
|
-
// id: string
|
|
251
|
-
// role: 'admin' | 'user'
|
|
252
|
-
// }
|
|
253
|
-
// }
|
|
254
|
-
}
|
|
255
|
-
}`, "utf-8");
|
|
256
|
-
if (existsSync(join(nitro.graphql.serverDir, "context.d.ts"))) {
|
|
257
|
-
consola.warn("nitro-graphql: Found context.d.ts file. Please rename it to context.ts for the new structure.");
|
|
258
|
-
consola.info("The context file should now be context.ts instead of context.d.ts");
|
|
259
|
-
}
|
|
260
|
-
}
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
//#endregion
|
|
264
|
-
export { src_default as default };
|
package/dist/rollup.js
DELETED
|
@@ -1,119 +0,0 @@
|
|
|
1
|
-
import { getImportId, scanGraphql } from "./utils/index.js";
|
|
2
|
-
import { clientTypeGeneration, serverTypeGeneration } from "./utils/type-generation.js";
|
|
3
|
-
import { resolve } from "pathe";
|
|
4
|
-
import { readFile } from "node:fs/promises";
|
|
5
|
-
import { parse } from "graphql";
|
|
6
|
-
import { genImport } from "knitwork";
|
|
7
|
-
|
|
8
|
-
//#region src/rollup.ts
|
|
9
|
-
async function rollupConfig(app) {
|
|
10
|
-
virtualSchemas(app);
|
|
11
|
-
virtualResolvers(app);
|
|
12
|
-
virtualDirectives(app);
|
|
13
|
-
getGraphQLConfig(app);
|
|
14
|
-
virtualModuleConfig(app);
|
|
15
|
-
app.hooks.hook("rollup:before", (nitro, rollupConfig$1) => {
|
|
16
|
-
rollupConfig$1.plugins = rollupConfig$1.plugins || [];
|
|
17
|
-
const { include = /\.(graphql|gql)$/i, exclude, validate = false } = app.options.graphql?.loader || {};
|
|
18
|
-
if (Array.isArray(rollupConfig$1.plugins)) {
|
|
19
|
-
rollupConfig$1.plugins.push({
|
|
20
|
-
name: "nitro-graphql",
|
|
21
|
-
enforce: "pre",
|
|
22
|
-
resolveId(id) {
|
|
23
|
-
if (/\.(graphql|gql)$/i.test(id)) return null;
|
|
24
|
-
},
|
|
25
|
-
async load(id) {
|
|
26
|
-
if (exclude?.test?.(id)) return null;
|
|
27
|
-
if (!include.test(id)) return null;
|
|
28
|
-
try {
|
|
29
|
-
const content = await readFile(id, "utf-8");
|
|
30
|
-
if (validate) parse(content);
|
|
31
|
-
return `export default ${JSON.stringify(content)}`;
|
|
32
|
-
} catch (error) {
|
|
33
|
-
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return null;
|
|
34
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
35
|
-
this.error(`Failed to read GraphQL file ${id}: ${message}`);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
rollupConfig$1.plugins.push({
|
|
40
|
-
name: "nitro-graphql-watcher",
|
|
41
|
-
enforce: "pre",
|
|
42
|
-
async buildStart() {
|
|
43
|
-
const graphqlFiles = await scanGraphql(nitro);
|
|
44
|
-
for (const file of graphqlFiles) this.addWatchFile(file);
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
app.hooks.hook("dev:reload", async () => {
|
|
50
|
-
await serverTypeGeneration(app);
|
|
51
|
-
await clientTypeGeneration(app);
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
function virtualSchemas(app) {
|
|
55
|
-
const getSchemas = () => [...app.scanSchemas, ...app.options.graphql?.typedefs ?? []];
|
|
56
|
-
app.options.virtual ??= {};
|
|
57
|
-
app.options.virtual["#nitro-internal-virtual/server-schemas"] = () => {
|
|
58
|
-
const imports = getSchemas();
|
|
59
|
-
return `
|
|
60
|
-
${imports.map((handler) => `import ${getImportId(handler)} from '${handler}';`).join("\n")}
|
|
61
|
-
|
|
62
|
-
export const schemas = [
|
|
63
|
-
${imports.map((h) => `{ def: ${getImportId(h)} }`).join(",\n")}
|
|
64
|
-
];
|
|
65
|
-
`;
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
function virtualResolvers(app) {
|
|
69
|
-
const getResolvers = () => [...app.scanResolvers];
|
|
70
|
-
app.options.virtual ??= {};
|
|
71
|
-
app.options.virtual["#nitro-internal-virtual/server-resolvers"] = () => {
|
|
72
|
-
const imports = getResolvers();
|
|
73
|
-
const importsContent = imports.map(({ specifier, imports: imports$1, options }) => genImport(specifier, imports$1, options));
|
|
74
|
-
const data = imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ resolver: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n");
|
|
75
|
-
return [
|
|
76
|
-
...importsContent,
|
|
77
|
-
"export const resolvers = [",
|
|
78
|
-
data,
|
|
79
|
-
"]",
|
|
80
|
-
""
|
|
81
|
-
].join("\n");
|
|
82
|
-
};
|
|
83
|
-
}
|
|
84
|
-
function virtualDirectives(app) {
|
|
85
|
-
const getDirectives = () => app.scanDirectives || [];
|
|
86
|
-
app.options.virtual ??= {};
|
|
87
|
-
app.options.virtual["#nitro-internal-virtual/server-directives"] = () => {
|
|
88
|
-
const imports = getDirectives();
|
|
89
|
-
const importsContent = imports.map(({ specifier, imports: imports$1, options }) => genImport(specifier, imports$1, options));
|
|
90
|
-
const data = imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ directive: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n");
|
|
91
|
-
return [
|
|
92
|
-
...importsContent,
|
|
93
|
-
"export const directives = [",
|
|
94
|
-
data,
|
|
95
|
-
"]",
|
|
96
|
-
""
|
|
97
|
-
].join("\n");
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
function getGraphQLConfig(app) {
|
|
101
|
-
const configPath = resolve(app.graphql.serverDir, "config.ts");
|
|
102
|
-
app.options.virtual ??= {};
|
|
103
|
-
app.options.virtual["#nitro-internal-virtual/graphql-config"] = () => {
|
|
104
|
-
return `import config from '${configPath}'
|
|
105
|
-
const importedConfig = config
|
|
106
|
-
export { importedConfig }
|
|
107
|
-
`;
|
|
108
|
-
};
|
|
109
|
-
}
|
|
110
|
-
function virtualModuleConfig(app) {
|
|
111
|
-
app.options.virtual ??= {};
|
|
112
|
-
app.options.virtual["#nitro-internal-virtual/module-config"] = () => {
|
|
113
|
-
const moduleConfig = app.options.graphql || {};
|
|
114
|
-
return `export const moduleConfig = ${JSON.stringify(moduleConfig, null, 2)};`;
|
|
115
|
-
};
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
//#endregion
|
|
119
|
-
export { rollupConfig };
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import { consola as consola$1 } from "consola";
|
|
2
|
-
import defu from "defu";
|
|
3
|
-
import { parse } from "graphql";
|
|
4
|
-
import { mergeResolvers, mergeTypeDefs } from "@graphql-tools/merge";
|
|
5
|
-
import { importedConfig } from "#nitro-internal-virtual/graphql-config";
|
|
6
|
-
import { moduleConfig } from "#nitro-internal-virtual/module-config";
|
|
7
|
-
import { directives } from "#nitro-internal-virtual/server-directives";
|
|
8
|
-
import { resolvers } from "#nitro-internal-virtual/server-resolvers";
|
|
9
|
-
import { schemas } from "#nitro-internal-virtual/server-schemas";
|
|
10
|
-
import { ApolloServer } from "@apollo/server";
|
|
11
|
-
import { ApolloServerPluginLandingPageLocalDefault } from "@apollo/server/plugin/landingPage/default";
|
|
12
|
-
import { makeExecutableSchema } from "@graphql-tools/schema";
|
|
13
|
-
import { defineEventHandler } from "h3";
|
|
14
|
-
import { startServerAndCreateH3Handler } from "nitro-graphql/utils/apollo";
|
|
15
|
-
|
|
16
|
-
//#region src/routes/apollo-server.ts
|
|
17
|
-
let buildSubgraphSchema = null;
|
|
18
|
-
async function loadFederationSupport() {
|
|
19
|
-
if (buildSubgraphSchema !== null) return buildSubgraphSchema;
|
|
20
|
-
try {
|
|
21
|
-
buildSubgraphSchema = (await import("@apollo/subgraph")).buildSubgraphSchema;
|
|
22
|
-
} catch {
|
|
23
|
-
buildSubgraphSchema = false;
|
|
24
|
-
}
|
|
25
|
-
return buildSubgraphSchema;
|
|
26
|
-
}
|
|
27
|
-
async function createMergedSchema() {
|
|
28
|
-
try {
|
|
29
|
-
const typeDefs = mergeTypeDefs([schemas.map((schema$1) => schema$1.def).join("\n\n")], {
|
|
30
|
-
throwOnConflict: true,
|
|
31
|
-
commentDescriptions: true,
|
|
32
|
-
sort: true
|
|
33
|
-
});
|
|
34
|
-
const mergedResolvers = mergeResolvers(resolvers.map((r) => r.resolver));
|
|
35
|
-
const federationEnabled = moduleConfig.federation?.enabled;
|
|
36
|
-
let schema;
|
|
37
|
-
if (federationEnabled) {
|
|
38
|
-
const buildSubgraph = await loadFederationSupport();
|
|
39
|
-
if (buildSubgraph) schema = buildSubgraph({
|
|
40
|
-
typeDefs: typeof typeDefs === "string" ? parse(typeDefs) : typeDefs,
|
|
41
|
-
resolvers: mergedResolvers
|
|
42
|
-
});
|
|
43
|
-
else {
|
|
44
|
-
console.warn("Federation enabled but @apollo/subgraph not available, falling back to regular schema");
|
|
45
|
-
schema = makeExecutableSchema({
|
|
46
|
-
typeDefs,
|
|
47
|
-
resolvers: mergedResolvers
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
} else schema = makeExecutableSchema({
|
|
51
|
-
typeDefs,
|
|
52
|
-
resolvers: mergedResolvers
|
|
53
|
-
});
|
|
54
|
-
if (directives && directives.length > 0) {
|
|
55
|
-
for (const { directive } of directives) if (directive.transformer) schema = directive.transformer(schema);
|
|
56
|
-
}
|
|
57
|
-
return schema;
|
|
58
|
-
} catch (error) {
|
|
59
|
-
consola$1.error("Schema merge error:", error);
|
|
60
|
-
throw error;
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
let apolloServer = null;
|
|
64
|
-
let serverStarted = false;
|
|
65
|
-
async function createApolloServer() {
|
|
66
|
-
if (!apolloServer) {
|
|
67
|
-
apolloServer = new ApolloServer(defu({
|
|
68
|
-
schema: await createMergedSchema(),
|
|
69
|
-
introspection: true,
|
|
70
|
-
plugins: [ApolloServerPluginLandingPageLocalDefault({ embed: true })]
|
|
71
|
-
}, importedConfig));
|
|
72
|
-
if (!serverStarted) {
|
|
73
|
-
await apolloServer.start();
|
|
74
|
-
serverStarted = true;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
return apolloServer;
|
|
78
|
-
}
|
|
79
|
-
let serverPromise = null;
|
|
80
|
-
var apollo_server_default = defineEventHandler(async (event) => {
|
|
81
|
-
if (!serverPromise) serverPromise = createApolloServer();
|
|
82
|
-
return startServerAndCreateH3Handler(await serverPromise, {
|
|
83
|
-
context: async () => ({ event }),
|
|
84
|
-
serverAlreadyStarted: true
|
|
85
|
-
})(event);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
//#endregion
|
|
89
|
-
export { apollo_server_default as default };
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { consola as consola$1 } from "consola";
|
|
2
|
-
import defu from "defu";
|
|
3
|
-
import { parse } from "graphql";
|
|
4
|
-
import { mergeResolvers, mergeTypeDefs } from "@graphql-tools/merge";
|
|
5
|
-
import { importedConfig } from "#nitro-internal-virtual/graphql-config";
|
|
6
|
-
import { moduleConfig } from "#nitro-internal-virtual/module-config";
|
|
7
|
-
import { directives } from "#nitro-internal-virtual/server-directives";
|
|
8
|
-
import { resolvers } from "#nitro-internal-virtual/server-resolvers";
|
|
9
|
-
import { schemas } from "#nitro-internal-virtual/server-schemas";
|
|
10
|
-
import { makeExecutableSchema } from "@graphql-tools/schema";
|
|
11
|
-
import { defineEventHandler } from "h3";
|
|
12
|
-
import { createYoga } from "graphql-yoga";
|
|
13
|
-
|
|
14
|
-
//#region src/routes/graphql-yoga.ts
|
|
15
|
-
let buildSubgraphSchema = null;
|
|
16
|
-
async function loadFederationSupport() {
|
|
17
|
-
if (buildSubgraphSchema !== null) return buildSubgraphSchema;
|
|
18
|
-
try {
|
|
19
|
-
buildSubgraphSchema = (await import("@apollo/subgraph")).buildSubgraphSchema;
|
|
20
|
-
} catch {
|
|
21
|
-
buildSubgraphSchema = false;
|
|
22
|
-
}
|
|
23
|
-
return buildSubgraphSchema;
|
|
24
|
-
}
|
|
25
|
-
const apolloSandboxHtml = `<!DOCTYPE html>
|
|
26
|
-
<html lang="en">
|
|
27
|
-
<body style="margin: 0; overflow-x: hidden; overflow-y: hidden">
|
|
28
|
-
<div id="sandbox" style="height:100vh; width:100vw;"></div>
|
|
29
|
-
<script src="https://embeddable-sandbox.cdn.apollographql.com/02e2da0fccbe0240ef03d2396d6c98559bab5b06/embeddable-sandbox.umd.production.min.js"><\/script>
|
|
30
|
-
<script>
|
|
31
|
-
new window.EmbeddedSandbox({
|
|
32
|
-
target: "#sandbox",
|
|
33
|
-
initialEndpoint: window.location.href,
|
|
34
|
-
hideCookieToggle: false,
|
|
35
|
-
initialState: {
|
|
36
|
-
includeCookies: true
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
<\/script>
|
|
40
|
-
</body>
|
|
41
|
-
</html>`;
|
|
42
|
-
async function createMergedSchema() {
|
|
43
|
-
try {
|
|
44
|
-
const typeDefs = mergeTypeDefs([schemas.map((schema$1) => schema$1.def).join("\n\n")], {
|
|
45
|
-
throwOnConflict: true,
|
|
46
|
-
commentDescriptions: true,
|
|
47
|
-
sort: true
|
|
48
|
-
});
|
|
49
|
-
const mergedResolvers = mergeResolvers(resolvers.map((r) => r.resolver));
|
|
50
|
-
const federationEnabled = moduleConfig.federation?.enabled;
|
|
51
|
-
let schema;
|
|
52
|
-
if (federationEnabled) {
|
|
53
|
-
const buildSubgraph = await loadFederationSupport();
|
|
54
|
-
if (buildSubgraph) schema = buildSubgraph({
|
|
55
|
-
typeDefs: typeof typeDefs === "string" ? parse(typeDefs) : typeDefs,
|
|
56
|
-
resolvers: mergedResolvers
|
|
57
|
-
});
|
|
58
|
-
else {
|
|
59
|
-
console.warn("Federation enabled but @apollo/subgraph not available, falling back to regular schema");
|
|
60
|
-
schema = makeExecutableSchema({
|
|
61
|
-
typeDefs,
|
|
62
|
-
resolvers: mergedResolvers
|
|
63
|
-
});
|
|
64
|
-
}
|
|
65
|
-
} else schema = makeExecutableSchema({
|
|
66
|
-
typeDefs,
|
|
67
|
-
resolvers: mergedResolvers
|
|
68
|
-
});
|
|
69
|
-
if (directives && directives.length > 0) {
|
|
70
|
-
for (const { directive } of directives) if (directive.transformer) schema = directive.transformer(schema);
|
|
71
|
-
}
|
|
72
|
-
return schema;
|
|
73
|
-
} catch (error) {
|
|
74
|
-
consola$1.error("Schema merge error:", error);
|
|
75
|
-
throw error;
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
let yoga;
|
|
79
|
-
var graphql_yoga_default = defineEventHandler(async (event) => {
|
|
80
|
-
if (!yoga) yoga = createYoga(defu({
|
|
81
|
-
schema: await createMergedSchema(),
|
|
82
|
-
graphqlEndpoint: "/api/graphql",
|
|
83
|
-
landingPage: false,
|
|
84
|
-
renderGraphiQL: () => apolloSandboxHtml
|
|
85
|
-
}, importedConfig));
|
|
86
|
-
const response = await yoga.handleRequest(event.req, event);
|
|
87
|
-
return new Response(response.body, response);
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
//#endregion
|
|
91
|
-
export { graphql_yoga_default as default };
|