nitro-graphql 2.0.0-beta.4 → 2.0.0-beta.40
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 +441 -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/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 +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/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 +212 -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 +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/nitro/paths.d.mts +54 -0
- package/dist/nitro/paths.mjs +93 -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/file-watcher.d.mts +16 -0
- package/dist/nitro/setup/file-watcher.mjs +80 -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 +231 -0
- 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.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 +97 -78
- 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
|
@@ -1,287 +0,0 @@
|
|
|
1
|
-
import { downloadAndSaveSchema, generateClientTypes, generateExternalClientTypes, loadExternalSchema, loadGraphQLDocuments } from "./client-codegen.js";
|
|
2
|
-
import { generateTypes } from "./server-codegen.js";
|
|
3
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
4
|
-
import consola from "consola";
|
|
5
|
-
import { basename, dirname, join, resolve } from "pathe";
|
|
6
|
-
import { buildSchema, parse } from "graphql";
|
|
7
|
-
import { buildSubgraphSchema } from "@apollo/subgraph";
|
|
8
|
-
import { loadFilesSync } from "@graphql-tools/load-files";
|
|
9
|
-
import { mergeTypeDefs } from "@graphql-tools/merge";
|
|
10
|
-
import { printSchemaWithDirectives } from "@graphql-tools/utils";
|
|
11
|
-
|
|
12
|
-
//#region src/utils/type-generation.ts
|
|
13
|
-
function generateGraphQLIndexFile(clientDir, externalServices = []) {
|
|
14
|
-
const indexPath = resolve(clientDir, "index.ts");
|
|
15
|
-
if (!existsSync(indexPath)) {
|
|
16
|
-
let indexContent = `// This file is auto-generated once by nitro-graphql for quick start
|
|
17
|
-
// You can modify this file according to your needs
|
|
18
|
-
//
|
|
19
|
-
// Export your main GraphQL service (auto-generated)
|
|
20
|
-
export * from './default/ofetch'
|
|
21
|
-
|
|
22
|
-
// Export external GraphQL services (auto-generated for existing services)
|
|
23
|
-
// When you add new external services, don't forget to add their exports here:
|
|
24
|
-
// export * from './yourServiceName/ofetch'
|
|
25
|
-
`;
|
|
26
|
-
for (const service of externalServices) indexContent += `export * from './${service.name}/ofetch'\n`;
|
|
27
|
-
writeFileSync(indexPath, indexContent, "utf-8");
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
function generateOfetchClient(clientDir, serviceName, endpoint, isDefault = false) {
|
|
31
|
-
const serviceDir = resolve(clientDir, serviceName);
|
|
32
|
-
const ofetchPath = resolve(serviceDir, "ofetch.ts");
|
|
33
|
-
if (!existsSync(serviceDir)) mkdirSync(serviceDir, { recursive: true });
|
|
34
|
-
if (existsSync(ofetchPath)) return;
|
|
35
|
-
const capitalizedServiceName = serviceName.charAt(0).toUpperCase() + serviceName.slice(1);
|
|
36
|
-
const functionName = isDefault ? "createGraphQLClient" : `create${capitalizedServiceName}GraphQLClient`;
|
|
37
|
-
const exportName = isDefault ? "$sdk" : `$${serviceName}Sdk`;
|
|
38
|
-
writeFileSync(ofetchPath, `// This file is auto-generated once by nitro-graphql for quick start
|
|
39
|
-
// You can modify this file according to your needs
|
|
40
|
-
import type { ${isDefault ? "Requester" : "Sdk, Requester"} } from './sdk'
|
|
41
|
-
import { getSdk } from './sdk'
|
|
42
|
-
|
|
43
|
-
export function ${functionName}(endpoint: string${isDefault ? "" : ` = '${endpoint}'`}): Requester {
|
|
44
|
-
return async <R>(doc: string, vars?: any): Promise<R> => {
|
|
45
|
-
const headers = import.meta.server ? useRequestHeaders() : undefined
|
|
46
|
-
|
|
47
|
-
const result = await $fetch(endpoint, {
|
|
48
|
-
method: 'POST',
|
|
49
|
-
body: { query: doc, variables: vars },
|
|
50
|
-
headers: {
|
|
51
|
-
'Content-Type': 'application/json',
|
|
52
|
-
...headers,
|
|
53
|
-
},
|
|
54
|
-
})
|
|
55
|
-
|
|
56
|
-
return result as R
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
export const ${exportName}${isDefault ? "" : ": Sdk"} = getSdk(${functionName}(${isDefault ? "'/api/graphql'" : ""}))`, "utf-8");
|
|
61
|
-
}
|
|
62
|
-
/**
|
|
63
|
-
* Check for duplicate type definitions using a simpler approach
|
|
64
|
-
* Try to build each schema individually - if that succeeds but merging fails, we have duplicates
|
|
65
|
-
* @returns true if validation passes, false if duplicates found
|
|
66
|
-
*/
|
|
67
|
-
function validateNoDuplicateTypes(schemas, schemaStrings) {
|
|
68
|
-
const individualSchemasByFile = /* @__PURE__ */ new Map();
|
|
69
|
-
schemaStrings.forEach((schemaContent, index) => {
|
|
70
|
-
const schemaPath = schemas[index];
|
|
71
|
-
const fileName = basename(schemaPath);
|
|
72
|
-
try {
|
|
73
|
-
parse(schemaContent);
|
|
74
|
-
individualSchemasByFile.set(fileName, schemaContent);
|
|
75
|
-
} catch (error) {
|
|
76
|
-
consola.warn(`Invalid GraphQL syntax in ${fileName}:`, error);
|
|
77
|
-
throw error;
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
try {
|
|
81
|
-
mergeTypeDefs([schemaStrings.join("\n\n")], {
|
|
82
|
-
throwOnConflict: false,
|
|
83
|
-
commentDescriptions: true,
|
|
84
|
-
sort: true
|
|
85
|
-
});
|
|
86
|
-
mergeTypeDefs([schemaStrings.join("\n\n")], {
|
|
87
|
-
throwOnConflict: true,
|
|
88
|
-
commentDescriptions: true,
|
|
89
|
-
sort: true
|
|
90
|
-
});
|
|
91
|
-
} catch (conflictError) {
|
|
92
|
-
if (conflictError?.message?.includes("already defined with a different type")) throw conflictError;
|
|
93
|
-
}
|
|
94
|
-
const typeNames = /* @__PURE__ */ new Set();
|
|
95
|
-
const duplicateTypes = [];
|
|
96
|
-
schemaStrings.forEach((schemaContent, index) => {
|
|
97
|
-
const fileName = basename(schemas[index]);
|
|
98
|
-
try {
|
|
99
|
-
parse(schemaContent).definitions.forEach((def) => {
|
|
100
|
-
if (def.kind === "ObjectTypeDefinition" || def.kind === "InterfaceTypeDefinition" || def.kind === "UnionTypeDefinition" || def.kind === "EnumTypeDefinition" || def.kind === "InputObjectTypeDefinition" || def.kind === "ScalarTypeDefinition") {
|
|
101
|
-
const typeName = def.name.value;
|
|
102
|
-
if ([
|
|
103
|
-
"String",
|
|
104
|
-
"Int",
|
|
105
|
-
"Float",
|
|
106
|
-
"Boolean",
|
|
107
|
-
"ID",
|
|
108
|
-
"DateTime",
|
|
109
|
-
"JSON"
|
|
110
|
-
].includes(typeName)) return;
|
|
111
|
-
if (typeNames.has(typeName)) {
|
|
112
|
-
const existing = duplicateTypes.find((d) => d.type === typeName);
|
|
113
|
-
if (existing) existing.files.push(fileName);
|
|
114
|
-
else {
|
|
115
|
-
const firstFile = schemas.find((_, i) => {
|
|
116
|
-
const content = schemaStrings[i];
|
|
117
|
-
if (!content) return false;
|
|
118
|
-
try {
|
|
119
|
-
return parse(content).definitions.some((d) => (d.kind === "ObjectTypeDefinition" || d.kind === "InterfaceTypeDefinition" || d.kind === "UnionTypeDefinition" || d.kind === "EnumTypeDefinition" || d.kind === "InputObjectTypeDefinition" || d.kind === "ScalarTypeDefinition") && d.name.value === typeName);
|
|
120
|
-
} catch {
|
|
121
|
-
return false;
|
|
122
|
-
}
|
|
123
|
-
});
|
|
124
|
-
duplicateTypes.push({
|
|
125
|
-
type: typeName,
|
|
126
|
-
files: [basename(firstFile || ""), fileName]
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
} else typeNames.add(typeName);
|
|
130
|
-
}
|
|
131
|
-
});
|
|
132
|
-
} catch {}
|
|
133
|
-
});
|
|
134
|
-
if (duplicateTypes.length > 0) {
|
|
135
|
-
let errorMessage = "⚠️ DUPLICATE TYPE DEFINITIONS DETECTED!\n\n";
|
|
136
|
-
duplicateTypes.forEach(({ type, files }) => {
|
|
137
|
-
errorMessage += `❌ Type "${type}" is defined in multiple files:\n`;
|
|
138
|
-
files.forEach((fileName) => {
|
|
139
|
-
const fullPath = schemas.find((path) => basename(path) === fileName) || fileName;
|
|
140
|
-
errorMessage += ` • ${fullPath}\n`;
|
|
141
|
-
});
|
|
142
|
-
errorMessage += "\n";
|
|
143
|
-
});
|
|
144
|
-
errorMessage += "💡 Each GraphQL type should only be defined once.\n";
|
|
145
|
-
errorMessage += " Consider using \"extend type\" syntax instead of duplicate definitions.\n";
|
|
146
|
-
errorMessage += `\n🔍 Found ${duplicateTypes.length} duplicate type(s): ${duplicateTypes.map((d) => d.type).join(", ")}`;
|
|
147
|
-
consola.error(errorMessage);
|
|
148
|
-
return false;
|
|
149
|
-
}
|
|
150
|
-
return true;
|
|
151
|
-
}
|
|
152
|
-
async function serverTypeGeneration(app) {
|
|
153
|
-
try {
|
|
154
|
-
const schemas = app.scanSchemas || [];
|
|
155
|
-
if (!schemas.length) {
|
|
156
|
-
consola.info("No GraphQL definitions found for server type generation.");
|
|
157
|
-
return;
|
|
158
|
-
}
|
|
159
|
-
const schemaStrings = loadFilesSync(schemas).map((schema$1) => typeof schema$1 === "string" ? schema$1 : schema$1.loc?.source?.body || "").filter(Boolean);
|
|
160
|
-
if (!validateNoDuplicateTypes(schemas, schemaStrings)) return;
|
|
161
|
-
const federationEnabled = app.options.graphql?.federation?.enabled === true;
|
|
162
|
-
const mergedSchemas = mergeTypeDefs([schemaStrings.join("\n\n")], {
|
|
163
|
-
throwOnConflict: true,
|
|
164
|
-
commentDescriptions: true,
|
|
165
|
-
sort: true
|
|
166
|
-
});
|
|
167
|
-
const schema = federationEnabled ? buildSubgraphSchema([{ typeDefs: parse(mergedSchemas) }]) : buildSchema(mergedSchemas);
|
|
168
|
-
const data = await generateTypes(app.options.graphql?.framework || "graphql-yoga", schema, app.options.graphql ?? {});
|
|
169
|
-
const printSchema = printSchemaWithDirectives(schema);
|
|
170
|
-
const schemaPath = resolve(app.graphql.buildDir, "schema.graphql");
|
|
171
|
-
mkdirSync(dirname(schemaPath), { recursive: true });
|
|
172
|
-
writeFileSync(schemaPath, printSchema, "utf-8");
|
|
173
|
-
const serverTypesPath = resolve(app.options.buildDir, "types", "nitro-graphql-server.d.ts");
|
|
174
|
-
mkdirSync(dirname(serverTypesPath), { recursive: true });
|
|
175
|
-
writeFileSync(serverTypesPath, data, "utf-8");
|
|
176
|
-
} catch (error) {
|
|
177
|
-
consola.error("Server schema generation error:", error);
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
async function clientTypeGeneration(nitro) {
|
|
181
|
-
try {
|
|
182
|
-
if (nitro.scanSchemas && nitro.scanSchemas.length > 0) await generateMainClientTypes(nitro);
|
|
183
|
-
if (nitro.options.graphql?.externalServices?.length) await generateExternalServicesTypes(nitro);
|
|
184
|
-
} catch (error) {
|
|
185
|
-
consola.error("Client schema generation error:", error);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
/**
|
|
189
|
-
* Check for old structure files and warn user about manual migration
|
|
190
|
-
*/
|
|
191
|
-
function checkOldStructure(clientDir) {
|
|
192
|
-
const oldOfetchPath = resolve(clientDir, "ofetch.ts");
|
|
193
|
-
const oldSdkPath = resolve(clientDir, "sdk.ts");
|
|
194
|
-
if (existsSync(oldOfetchPath) || existsSync(oldSdkPath)) {
|
|
195
|
-
const foundFiles = [];
|
|
196
|
-
if (existsSync(oldOfetchPath)) foundFiles.push("app/graphql/ofetch.ts");
|
|
197
|
-
if (existsSync(oldSdkPath)) foundFiles.push("app/graphql/sdk.ts");
|
|
198
|
-
consola.error(`⚠️ OLD GRAPHQL STRUCTURE DETECTED!
|
|
199
|
-
|
|
200
|
-
📁 Found old files in app/graphql/ directory that need to be moved:
|
|
201
|
-
• ${foundFiles.join("\n • ")}
|
|
202
|
-
|
|
203
|
-
🔄 Please manually move these files to the new structure:
|
|
204
|
-
• app/graphql/ofetch.ts → app/graphql/default/ofetch.ts
|
|
205
|
-
• app/graphql/sdk.ts → app/graphql/default/sdk.ts
|
|
206
|
-
|
|
207
|
-
📝 Also update your app/graphql/index.ts to include:
|
|
208
|
-
export * from './default/ofetch'
|
|
209
|
-
|
|
210
|
-
💡 After moving, update your imports to use:
|
|
211
|
-
import { $sdk } from "#graphql/client"
|
|
212
|
-
|
|
213
|
-
🚫 The old files will cause import conflicts until moved!`);
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
async function generateMainClientTypes(nitro) {
|
|
217
|
-
checkOldStructure(nitro.graphql.clientDir);
|
|
218
|
-
const docs = nitro.scanDocuments;
|
|
219
|
-
const loadDocs = await loadGraphQLDocuments(docs);
|
|
220
|
-
const schemaFilePath = join(nitro.graphql.buildDir, "schema.graphql");
|
|
221
|
-
if (!existsSync(schemaFilePath)) {
|
|
222
|
-
consola.info("Schema file not ready yet for client type generation. Server types need to be generated first.");
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
const graphqlString = readFileSync(schemaFilePath, "utf-8");
|
|
226
|
-
const types = await generateClientTypes(nitro.options.graphql?.federation?.enabled === true ? buildSubgraphSchema([{ typeDefs: parse(graphqlString) }]) : buildSchema(graphqlString), loadDocs, nitro.options.graphql?.codegen?.client ?? {}, nitro.options.graphql?.codegen?.clientSDK ?? {});
|
|
227
|
-
if (types === false) return;
|
|
228
|
-
const clientTypesPath = resolve(nitro.options.buildDir, "types", "nitro-graphql-client.d.ts");
|
|
229
|
-
const defaultServiceDir = resolve(nitro.graphql.clientDir, "default");
|
|
230
|
-
const sdkTypesPath = resolve(defaultServiceDir, "sdk.ts");
|
|
231
|
-
mkdirSync(dirname(clientTypesPath), { recursive: true });
|
|
232
|
-
writeFileSync(clientTypesPath, types.types, "utf-8");
|
|
233
|
-
mkdirSync(defaultServiceDir, { recursive: true });
|
|
234
|
-
let shouldWriteSdk = true;
|
|
235
|
-
if (existsSync(sdkTypesPath)) shouldWriteSdk = readFileSync(sdkTypesPath, "utf-8") !== types.sdk;
|
|
236
|
-
if (shouldWriteSdk) writeFileSync(sdkTypesPath, types.sdk, "utf-8");
|
|
237
|
-
if (nitro.options.framework?.name === "nuxt") {
|
|
238
|
-
generateOfetchClient(nitro.graphql.clientDir, "default", "/api/graphql", true);
|
|
239
|
-
const externalServices = nitro.options.graphql?.externalServices || [];
|
|
240
|
-
generateGraphQLIndexFile(nitro.graphql.clientDir, externalServices);
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
async function generateExternalServicesTypes(nitro) {
|
|
244
|
-
const externalServices = nitro.options.graphql?.externalServices || [];
|
|
245
|
-
for (const service of externalServices) try {
|
|
246
|
-
consola.info(`[graphql:${service.name}] Processing external service`);
|
|
247
|
-
await downloadAndSaveSchema(service, nitro.options.buildDir);
|
|
248
|
-
const schema = await loadExternalSchema(service, nitro.options.buildDir);
|
|
249
|
-
if (!schema) {
|
|
250
|
-
consola.warn(`[graphql:${service.name}] Failed to load schema, skipping`);
|
|
251
|
-
continue;
|
|
252
|
-
}
|
|
253
|
-
const documentPatterns = service.documents || [];
|
|
254
|
-
let loadDocs = [];
|
|
255
|
-
if (documentPatterns.length > 0) try {
|
|
256
|
-
loadDocs = await loadGraphQLDocuments(documentPatterns);
|
|
257
|
-
if (!loadDocs || loadDocs.length === 0) {
|
|
258
|
-
consola.warn(`[graphql:${service.name}] No GraphQL documents found, skipping service generation`);
|
|
259
|
-
continue;
|
|
260
|
-
}
|
|
261
|
-
} catch (error) {
|
|
262
|
-
consola.warn(`[graphql:${service.name}] No documents found, skipping service generation:`, error);
|
|
263
|
-
continue;
|
|
264
|
-
}
|
|
265
|
-
const types = await generateExternalClientTypes(service, schema, loadDocs);
|
|
266
|
-
if (types === false) {
|
|
267
|
-
consola.warn(`[graphql:${service.name}] Type generation failed`);
|
|
268
|
-
continue;
|
|
269
|
-
}
|
|
270
|
-
const serviceTypesPath = resolve(nitro.options.buildDir, "types", `nitro-graphql-client-${service.name}.d.ts`);
|
|
271
|
-
const serviceDir = resolve(nitro.graphql.clientDir, service.name);
|
|
272
|
-
const serviceSdkPath = resolve(serviceDir, "sdk.ts");
|
|
273
|
-
mkdirSync(dirname(serviceTypesPath), { recursive: true });
|
|
274
|
-
writeFileSync(serviceTypesPath, types.types, "utf-8");
|
|
275
|
-
mkdirSync(serviceDir, { recursive: true });
|
|
276
|
-
let shouldWriteServiceSdk = true;
|
|
277
|
-
if (existsSync(serviceSdkPath)) shouldWriteServiceSdk = readFileSync(serviceSdkPath, "utf-8") !== types.sdk;
|
|
278
|
-
if (shouldWriteServiceSdk) writeFileSync(serviceSdkPath, types.sdk, "utf-8");
|
|
279
|
-
if (nitro.options.framework?.name === "nuxt") generateOfetchClient(nitro.graphql.clientDir, service.name, service.endpoint, false);
|
|
280
|
-
consola.success(`[graphql:${service.name}] External service types generated successfully`);
|
|
281
|
-
} catch (error) {
|
|
282
|
-
consola.error(`[graphql:${service.name}] External service generation failed:`, error);
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
//#endregion
|
|
287
|
-
export { clientTypeGeneration, serverTypeGeneration };
|
package/dist/vite.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import { Plugin } from "vite";
|
|
2
|
-
|
|
3
|
-
//#region src/vite.d.ts
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Vite plugin to load GraphQL files as strings
|
|
7
|
-
* This prevents Vite from trying to parse .graphql/.gql files as JavaScript
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```ts
|
|
11
|
-
* import { defineConfig } from 'vite'
|
|
12
|
-
* import { nitro } from 'nitro/vite'
|
|
13
|
-
* import { graphql } from 'nitro-graphql/vite'
|
|
14
|
-
*
|
|
15
|
-
* export default defineConfig({
|
|
16
|
-
* plugins: [
|
|
17
|
-
* graphql(), // Must be before nitro()
|
|
18
|
-
* nitro()
|
|
19
|
-
* ]
|
|
20
|
-
* })
|
|
21
|
-
* ```
|
|
22
|
-
*/
|
|
23
|
-
declare function graphql(): Plugin;
|
|
24
|
-
//#endregion
|
|
25
|
-
export { graphql };
|
package/dist/vite.js
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
1
|
-
import { readFile } from "node:fs/promises";
|
|
2
|
-
|
|
3
|
-
//#region src/vite.ts
|
|
4
|
-
/**
|
|
5
|
-
* Vite plugin to load GraphQL files as strings
|
|
6
|
-
* This prevents Vite from trying to parse .graphql/.gql files as JavaScript
|
|
7
|
-
*
|
|
8
|
-
* @example
|
|
9
|
-
* ```ts
|
|
10
|
-
* import { defineConfig } from 'vite'
|
|
11
|
-
* import { nitro } from 'nitro/vite'
|
|
12
|
-
* import { graphql } from 'nitro-graphql/vite'
|
|
13
|
-
*
|
|
14
|
-
* export default defineConfig({
|
|
15
|
-
* plugins: [
|
|
16
|
-
* graphql(), // Must be before nitro()
|
|
17
|
-
* nitro()
|
|
18
|
-
* ]
|
|
19
|
-
* })
|
|
20
|
-
* ```
|
|
21
|
-
*/
|
|
22
|
-
function graphql() {
|
|
23
|
-
return {
|
|
24
|
-
name: "nitro-graphql:vite",
|
|
25
|
-
enforce: "pre",
|
|
26
|
-
async load(id) {
|
|
27
|
-
if (!/\.(?:graphql|gql)$/i.test(id)) return null;
|
|
28
|
-
try {
|
|
29
|
-
const content = await readFile(id, "utf-8");
|
|
30
|
-
return `export default ${JSON.stringify(content)}`;
|
|
31
|
-
} catch (error) {
|
|
32
|
-
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") return null;
|
|
33
|
-
throw error;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
//#endregion
|
|
40
|
-
export { graphql };
|
|
File without changes
|
|
File without changes
|