nitro-graphql 2.0.0-beta.31 → 2.0.0-beta.32
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/codegen/client-types.d.mts +13 -0
- package/dist/codegen/client-types.mjs +176 -0
- package/dist/codegen/external-types.d.mts +12 -0
- package/dist/codegen/external-types.mjs +129 -0
- package/dist/codegen/index.d.mts +25 -0
- package/dist/codegen/index.mjs +38 -0
- package/dist/codegen/server-types.d.mts +13 -0
- package/dist/codegen/server-types.mjs +76 -0
- package/dist/codegen/validation.d.mts +13 -0
- package/dist/codegen/validation.mjs +96 -0
- package/dist/config/defaults.mjs +36 -0
- package/dist/constants.mjs +91 -0
- package/dist/ecosystem/nuxt.mjs +3 -3
- package/dist/rollup.d.mts +7 -7
- package/dist/rollup.mjs +73 -73
- package/dist/routes/apollo-server.d.mts +2 -2
- package/dist/routes/debug.d.mts +2 -2
- package/dist/routes/health.d.mts +2 -2
- package/dist/setup/file-watcher.mjs +80 -0
- package/dist/setup/rollup-integration.mjs +90 -0
- package/dist/setup/scaffold-generator.mjs +109 -0
- package/dist/setup/ts-config.mjs +69 -0
- package/dist/setup.d.mts +2 -2
- package/dist/setup.mjs +127 -274
- package/dist/types/index.d.mts +1 -1
- package/dist/utils/client-codegen.d.mts +1 -1
- package/dist/utils/client-codegen.mjs +5 -2
- package/dist/utils/directive-parser.d.mts +2 -1
- package/dist/utils/directive-parser.mjs +4 -2
- package/dist/utils/file-generator.mjs +1 -1
- package/dist/utils/index.d.mts +2 -2
- package/dist/utils/index.mjs +2 -2
- package/dist/utils/path-resolver.d.mts +2 -2
- package/dist/utils/path-resolver.mjs +2 -2
- package/dist/utils/server-codegen.mjs +4 -1
- package/dist/utils/type-generation.d.mts +6 -12
- package/dist/utils/type-generation.mjs +6 -419
- package/package.json +8 -2
package/dist/types/index.d.mts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { StandardSchemaV1 } from "./standard-schema.mjs";
|
|
2
2
|
import { ESMCodeGenOptions } from "knitwork";
|
|
3
|
-
import { IResolvers } from "@graphql-tools/utils";
|
|
4
3
|
import { TypeScriptPluginConfig } from "@graphql-codegen/typescript";
|
|
5
4
|
import { plugin as plugin$1 } from "@graphql-codegen/typescript-generic-sdk";
|
|
6
5
|
import { TypeScriptDocumentsPluginConfig } from "@graphql-codegen/typescript-operations";
|
|
6
|
+
import { IResolvers } from "@graphql-tools/utils";
|
|
7
7
|
import { TypeScriptResolversPluginConfig } from "@graphql-codegen/typescript-resolvers";
|
|
8
8
|
|
|
9
9
|
//#region src/types/index.d.ts
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CodegenClientConfig, ExternalGraphQLService, GenericSdkConfig } from "../types/index.mjs";
|
|
2
2
|
import { GraphQLSchema } from "graphql";
|
|
3
|
-
import { Source } from "@graphql-tools/utils";
|
|
4
3
|
import { LoadSchemaOptions, UnnormalizedTypeDefPointer } from "@graphql-tools/load";
|
|
4
|
+
import { Source } from "@graphql-tools/utils";
|
|
5
5
|
|
|
6
6
|
//#region src/utils/client-codegen.d.ts
|
|
7
7
|
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { defu as defu$1 } from "defu";
|
|
2
|
-
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
2
|
import { consola as consola$1 } from "consola";
|
|
4
3
|
import { dirname, resolve } from "pathe";
|
|
5
4
|
import { parse } from "graphql";
|
|
6
|
-
import {
|
|
5
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
7
6
|
import { createHash } from "node:crypto";
|
|
8
7
|
import { codegen } from "@graphql-codegen/core";
|
|
9
8
|
import { preset } from "@graphql-codegen/import-types-preset";
|
|
@@ -13,9 +12,13 @@ import { plugin as plugin$2 } from "@graphql-codegen/typescript-operations";
|
|
|
13
12
|
import { GraphQLFileLoader } from "@graphql-tools/graphql-file-loader";
|
|
14
13
|
import { loadDocuments, loadSchemaSync } from "@graphql-tools/load";
|
|
15
14
|
import { UrlLoader } from "@graphql-tools/url-loader";
|
|
15
|
+
import { printSchemaWithDirectives } from "@graphql-tools/utils";
|
|
16
16
|
import { CurrencyResolver, DateTimeISOResolver, DateTimeResolver, JSONObjectResolver, JSONResolver, NonEmptyStringResolver, UUIDResolver } from "graphql-scalars";
|
|
17
17
|
|
|
18
18
|
//#region src/utils/client-codegen.ts
|
|
19
|
+
/**
|
|
20
|
+
* Plugin to add prepend comments to generated files
|
|
21
|
+
*/
|
|
19
22
|
function pluginContent(_schema, _documents, _config, _info) {
|
|
20
23
|
return {
|
|
21
24
|
prepend: [
|
|
@@ -70,8 +70,9 @@ declare class DirectiveParser {
|
|
|
70
70
|
declare function generateDirectiveSchema(directive: ParsedDirective): string;
|
|
71
71
|
/**
|
|
72
72
|
* Generate directive schemas file from scanned directives
|
|
73
|
+
* @returns The path to the generated _directives.graphql file, or null if no directives
|
|
73
74
|
*/
|
|
74
|
-
declare function generateDirectiveSchemas(nitro: any, directives: any[]): Promise<
|
|
75
|
+
declare function generateDirectiveSchemas(nitro: any, directives: any[]): Promise<string | null>;
|
|
75
76
|
/**
|
|
76
77
|
* Singleton instance for reuse
|
|
77
78
|
*/
|
|
@@ -181,9 +181,10 @@ function generateDirectiveSchema(directive) {
|
|
|
181
181
|
}
|
|
182
182
|
/**
|
|
183
183
|
* Generate directive schemas file from scanned directives
|
|
184
|
+
* @returns The path to the generated _directives.graphql file, or null if no directives
|
|
184
185
|
*/
|
|
185
186
|
async function generateDirectiveSchemas(nitro, directives) {
|
|
186
|
-
if (directives.length === 0) return;
|
|
187
|
+
if (directives.length === 0) return null;
|
|
187
188
|
const { existsSync, readFileSync, writeFileSync, mkdirSync } = await import("node:fs");
|
|
188
189
|
const { readFile } = await import("node:fs/promises");
|
|
189
190
|
const { resolve, dirname } = await import("pathe");
|
|
@@ -212,8 +213,9 @@ ${directiveSchemas.join("\n\n")}`;
|
|
|
212
213
|
let shouldWrite = true;
|
|
213
214
|
if (existsSync(directivesPath)) shouldWrite = readFileSync(directivesPath, "utf-8") !== content;
|
|
214
215
|
if (shouldWrite) writeFileSync(directivesPath, content, "utf-8");
|
|
215
|
-
|
|
216
|
+
return directivesPath;
|
|
216
217
|
}
|
|
218
|
+
return null;
|
|
217
219
|
}
|
|
218
220
|
/**
|
|
219
221
|
* Singleton instance for reuse
|
package/dist/utils/index.d.mts
CHANGED
|
@@ -27,7 +27,7 @@ declare function scanGraphql(nitro: Nitro): Promise<string[]>;
|
|
|
27
27
|
declare function scanResolvers(nitro: Nitro): Promise<GenImport[]>;
|
|
28
28
|
declare function scanDirectives(nitro: Nitro): Promise<GenImport[]>;
|
|
29
29
|
declare function scanSchemas(nitro: Nitro): Promise<string[]>;
|
|
30
|
-
declare function
|
|
30
|
+
declare function scanDocuments(nitro: Nitro): Promise<string[]>;
|
|
31
31
|
/**
|
|
32
32
|
* Scan documents for a specific external service
|
|
33
33
|
*/
|
|
@@ -37,4 +37,4 @@ declare function scanExternalServiceDocs(nitro: Nitro, serviceName: string, patt
|
|
|
37
37
|
*/
|
|
38
38
|
declare function validateExternalServices(services: unknown[]): string[];
|
|
39
39
|
//#endregion
|
|
40
|
-
export { GLOB_SCAN_PATTERN, createDefaultMaskError, directiveParser, generateDirectiveSchema, generateDirectiveSchemas, generateLayerIgnorePatterns, getImportId, getLayerAppDirectories, getLayerDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives,
|
|
40
|
+
export { GLOB_SCAN_PATTERN, createDefaultMaskError, directiveParser, generateDirectiveSchema, generateDirectiveSchemas, generateLayerIgnorePatterns, getImportId, getLayerAppDirectories, getLayerDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives, scanDocuments, scanExternalServiceDocs, scanGraphql, scanResolvers, scanSchemas, validateExternalServices };
|
package/dist/utils/index.mjs
CHANGED
|
@@ -200,7 +200,7 @@ async function scanSchemas(nitro) {
|
|
|
200
200
|
return true;
|
|
201
201
|
}).map((f) => f.fullPath);
|
|
202
202
|
}
|
|
203
|
-
async function
|
|
203
|
+
async function scanDocuments(nitro) {
|
|
204
204
|
const files = await scanDir(nitro, nitro.options.rootDir, nitro.graphql.dir.client, "**/*.graphql");
|
|
205
205
|
const layerAppDirs = getLayerAppDirectories(nitro);
|
|
206
206
|
const layerFiles = await Promise.all(layerAppDirs.map((layerAppDir) => scanDir(nitro, layerAppDir, "graphql", "**/*.graphql"))).then((r) => r.flat());
|
|
@@ -287,4 +287,4 @@ async function scanDir(nitro, dir, name, globPattern = GLOB_SCAN_PATTERN) {
|
|
|
287
287
|
}
|
|
288
288
|
|
|
289
289
|
//#endregion
|
|
290
|
-
export { GLOB_SCAN_PATTERN, createDefaultMaskError, directiveParser, generateDirectiveSchema, generateDirectiveSchemas, generateLayerIgnorePatterns, getImportId, getLayerAppDirectories, getLayerDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives,
|
|
290
|
+
export { GLOB_SCAN_PATTERN, createDefaultMaskError, directiveParser, generateDirectiveSchema, generateDirectiveSchemas, generateLayerIgnorePatterns, getImportId, getLayerAppDirectories, getLayerDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives, scanDocuments, scanExternalServiceDocs, scanGraphql, scanResolvers, scanSchemas, validateExternalServices };
|
|
@@ -53,7 +53,7 @@ declare function getClientUtilsConfig(nitro: Nitro): ClientUtilsConfig;
|
|
|
53
53
|
/**
|
|
54
54
|
* Check if SDK files should be generated (category-level check)
|
|
55
55
|
*/
|
|
56
|
-
declare function
|
|
56
|
+
declare function shouldGenerateSdk(nitro: Nitro): boolean;
|
|
57
57
|
/**
|
|
58
58
|
* Get SDK configuration (handles false case)
|
|
59
59
|
*/
|
|
@@ -67,4 +67,4 @@ declare function shouldGenerateTypes(nitro: Nitro): boolean;
|
|
|
67
67
|
*/
|
|
68
68
|
declare function getTypesConfig(nitro: Nitro): TypesConfig;
|
|
69
69
|
//#endregion
|
|
70
|
-
export { PathPlaceholders, getClientUtilsConfig, getDefaultPaths, getScaffoldConfig, getSdkConfig, getTypesConfig, replacePlaceholders, resolveFilePath, shouldGenerateClientUtils, shouldGenerateFile,
|
|
70
|
+
export { PathPlaceholders, getClientUtilsConfig, getDefaultPaths, getScaffoldConfig, getSdkConfig, getTypesConfig, replacePlaceholders, resolveFilePath, shouldGenerateClientUtils, shouldGenerateFile, shouldGenerateScaffold, shouldGenerateSdk, shouldGenerateTypes };
|
|
@@ -91,7 +91,7 @@ function getClientUtilsConfig(nitro) {
|
|
|
91
91
|
/**
|
|
92
92
|
* Check if SDK files should be generated (category-level check)
|
|
93
93
|
*/
|
|
94
|
-
function
|
|
94
|
+
function shouldGenerateSdk(nitro) {
|
|
95
95
|
const sdkConfig = nitro.options.graphql?.sdk;
|
|
96
96
|
if (sdkConfig === false) return false;
|
|
97
97
|
if (sdkConfig && sdkConfig.enabled === false) return false;
|
|
@@ -124,4 +124,4 @@ function getTypesConfig(nitro) {
|
|
|
124
124
|
}
|
|
125
125
|
|
|
126
126
|
//#endregion
|
|
127
|
-
export { getClientUtilsConfig, getDefaultPaths, getScaffoldConfig, getSdkConfig, getTypesConfig, replacePlaceholders, resolveFilePath, shouldGenerateClientUtils, shouldGenerateFile,
|
|
127
|
+
export { getClientUtilsConfig, getDefaultPaths, getScaffoldConfig, getSdkConfig, getTypesConfig, replacePlaceholders, resolveFilePath, shouldGenerateClientUtils, shouldGenerateFile, shouldGenerateScaffold, shouldGenerateSdk, shouldGenerateTypes };
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { defu as defu$1 } from "defu";
|
|
2
2
|
import consola from "consola";
|
|
3
3
|
import { parse } from "graphql";
|
|
4
|
-
import { printSchemaWithDirectives } from "@graphql-tools/utils";
|
|
5
4
|
import { codegen } from "@graphql-codegen/core";
|
|
6
5
|
import * as typescriptPlugin from "@graphql-codegen/typescript";
|
|
6
|
+
import { printSchemaWithDirectives } from "@graphql-tools/utils";
|
|
7
7
|
import { CurrencyResolver, DateTimeISOResolver, DateTimeResolver, JSONObjectResolver, JSONResolver, NonEmptyStringResolver, UUIDResolver } from "graphql-scalars";
|
|
8
8
|
import * as typescriptResolversPlugin from "@graphql-codegen/typescript-resolvers";
|
|
9
9
|
|
|
10
10
|
//#region src/utils/server-codegen.ts
|
|
11
|
+
/**
|
|
12
|
+
* Plugin to add prepend comments to generated files
|
|
13
|
+
*/
|
|
11
14
|
function pluginContent(_schema, _documents, _config, _info) {
|
|
12
15
|
return {
|
|
13
16
|
prepend: [
|
|
@@ -1,12 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
}
|
|
7
|
-
declare function clientTypeGeneration(nitro: Nitro, options?: {
|
|
8
|
-
silent?: boolean;
|
|
9
|
-
isInitial?: boolean;
|
|
10
|
-
}): Promise<void>;
|
|
11
|
-
//#endregion
|
|
12
|
-
export { clientTypeGeneration, serverTypeGeneration };
|
|
1
|
+
import { generateMainClientTypes } from "../codegen/client-types.mjs";
|
|
2
|
+
import { generateExternalServicesTypes } from "../codegen/external-types.mjs";
|
|
3
|
+
import { generateServerTypes } from "../codegen/server-types.mjs";
|
|
4
|
+
import { validateNoDuplicateTypes } from "../codegen/validation.mjs";
|
|
5
|
+
import { clientTypeGeneration, serverTypeGeneration } from "../codegen/index.mjs";
|
|
6
|
+
export { clientTypeGeneration, generateExternalServicesTypes, generateMainClientTypes, generateServerTypes, serverTypeGeneration, validateNoDuplicateTypes };
|
|
@@ -1,420 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import consola from "consola";
|
|
7
|
-
import { basename, dirname, join, resolve } from "pathe";
|
|
8
|
-
import { buildSchema, parse } from "graphql";
|
|
9
|
-
import { loadFilesSync } from "@graphql-tools/load-files";
|
|
10
|
-
import { mergeTypeDefs } from "@graphql-tools/merge";
|
|
11
|
-
import { printSchemaWithDirectives } from "@graphql-tools/utils";
|
|
1
|
+
import { generateMainClientTypes } from "../codegen/client-types.mjs";
|
|
2
|
+
import { generateExternalServicesTypes } from "../codegen/external-types.mjs";
|
|
3
|
+
import { validateNoDuplicateTypes } from "../codegen/validation.mjs";
|
|
4
|
+
import { generateServerTypes } from "../codegen/server-types.mjs";
|
|
5
|
+
import { clientTypeGeneration, serverTypeGeneration } from "../codegen/index.mjs";
|
|
12
6
|
|
|
13
|
-
|
|
14
|
-
const logger = consola.withTag("nitro-graphql");
|
|
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
|
-
function generateGraphQLIndexFile(nitro, clientDir, externalServices = []) {
|
|
26
|
-
if (!shouldGenerateClientUtils(nitro)) return;
|
|
27
|
-
const placeholders = getDefaultPaths(nitro);
|
|
28
|
-
const clientUtilsConfig = getClientUtilsConfig(nitro);
|
|
29
|
-
const indexPath = resolveFilePath(clientUtilsConfig.index, clientUtilsConfig.enabled, true, "{clientGraphql}/index.ts", placeholders);
|
|
30
|
-
if (!indexPath) return;
|
|
31
|
-
if (!existsSync(indexPath)) {
|
|
32
|
-
let indexContent = `// This file is auto-generated once by nitro-graphql for quick start
|
|
33
|
-
// You can modify this file according to your needs
|
|
34
|
-
//
|
|
35
|
-
// Export your main GraphQL service (auto-generated)
|
|
36
|
-
export * from './default/ofetch'
|
|
37
|
-
|
|
38
|
-
// Export external GraphQL services (auto-generated for existing services)
|
|
39
|
-
// When you add new external services, don't forget to add their exports here:
|
|
40
|
-
// export * from './yourServiceName/ofetch'
|
|
41
|
-
`;
|
|
42
|
-
for (const service of externalServices) indexContent += `export * from './${service.name}/ofetch'\n`;
|
|
43
|
-
writeFileIfNotExists(indexPath, indexContent, "client index.ts");
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
function generateNuxtOfetchClient(nitro, clientDir, serviceName = "default") {
|
|
47
|
-
if (!shouldGenerateClientUtils(nitro)) return;
|
|
48
|
-
const placeholders = {
|
|
49
|
-
...getDefaultPaths(nitro),
|
|
50
|
-
serviceName
|
|
51
|
-
};
|
|
52
|
-
const clientUtilsConfig = getClientUtilsConfig(nitro);
|
|
53
|
-
const ofetchPath = resolveFilePath(clientUtilsConfig.ofetch, clientUtilsConfig.enabled, true, "{clientGraphql}/{serviceName}/ofetch.ts", placeholders);
|
|
54
|
-
if (!ofetchPath) return;
|
|
55
|
-
const serviceDir = dirname(ofetchPath);
|
|
56
|
-
if (!existsSync(serviceDir)) mkdirSync(serviceDir, { recursive: true });
|
|
57
|
-
if (existsSync(ofetchPath)) return;
|
|
58
|
-
writeFileIfNotExists(ofetchPath, nitro.options.framework?.name === "nuxt" ? `// This file is auto-generated once by nitro-graphql for quick start
|
|
59
|
-
// You can modify this file according to your needs
|
|
60
|
-
import type { Requester } from './sdk'
|
|
61
|
-
import { getSdk } from './sdk'
|
|
62
|
-
|
|
63
|
-
export function createGraphQLClient(endpoint: string): Requester {
|
|
64
|
-
return async <R>(doc: string, vars?: any): Promise<R> => {
|
|
65
|
-
const headers = import.meta.server ? useRequestHeaders() : undefined
|
|
66
|
-
|
|
67
|
-
const result = await $fetch(endpoint, {
|
|
68
|
-
method: 'POST',
|
|
69
|
-
body: { query: doc, variables: vars },
|
|
70
|
-
headers: {
|
|
71
|
-
'Content-Type': 'application/json',
|
|
72
|
-
...headers,
|
|
73
|
-
},
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
return result as R
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export const $sdk = getSdk(createGraphQLClient('/api/graphql'))` : `// This file is auto-generated once by nitro-graphql for quick start
|
|
81
|
-
// You can modify this file according to your needs
|
|
82
|
-
import type { Requester } from './sdk'
|
|
83
|
-
import { ofetch } from 'ofetch'
|
|
84
|
-
import { getSdk } from './sdk'
|
|
85
|
-
|
|
86
|
-
export function createGraphQLClient(endpoint: string): Requester {
|
|
87
|
-
return async <R>(doc: string, vars?: any): Promise<R> => {
|
|
88
|
-
const result = await ofetch(endpoint, {
|
|
89
|
-
method: 'POST',
|
|
90
|
-
body: { query: doc, variables: vars },
|
|
91
|
-
headers: {
|
|
92
|
-
'Content-Type': 'application/json',
|
|
93
|
-
},
|
|
94
|
-
})
|
|
95
|
-
|
|
96
|
-
return result as R
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
export const $sdk = getSdk(createGraphQLClient('/api/graphql'))`, `${serviceName} ofetch.ts`);
|
|
101
|
-
}
|
|
102
|
-
function generateExternalOfetchClient(nitro, service, endpoint) {
|
|
103
|
-
if (!shouldGenerateClientUtils(nitro)) return;
|
|
104
|
-
const serviceName = service.name;
|
|
105
|
-
const placeholders = {
|
|
106
|
-
...getDefaultPaths(nitro),
|
|
107
|
-
serviceName
|
|
108
|
-
};
|
|
109
|
-
const clientUtilsConfig = getClientUtilsConfig(nitro);
|
|
110
|
-
const ofetchPath = resolveFilePath(service.paths?.ofetch ?? clientUtilsConfig.ofetch, clientUtilsConfig.enabled, true, "{clientGraphql}/{serviceName}/ofetch.ts", placeholders);
|
|
111
|
-
if (!ofetchPath) return;
|
|
112
|
-
const serviceDir = dirname(ofetchPath);
|
|
113
|
-
if (!existsSync(serviceDir)) mkdirSync(serviceDir, { recursive: true });
|
|
114
|
-
if (!existsSync(ofetchPath)) {
|
|
115
|
-
const capitalizedServiceName = serviceName.charAt(0).toUpperCase() + serviceName.slice(1);
|
|
116
|
-
writeFileIfNotExists(ofetchPath, nitro.options.framework?.name === "nuxt" ? `// This file is auto-generated once by nitro-graphql for quick start
|
|
117
|
-
// You can modify this file according to your needs
|
|
118
|
-
import type { Sdk, Requester } from './sdk'
|
|
119
|
-
import { getSdk } from './sdk'
|
|
120
|
-
|
|
121
|
-
export function create${capitalizedServiceName}GraphQLClient(endpoint: string = '${endpoint}'): Requester {
|
|
122
|
-
return async <R>(doc: string, vars?: any): Promise<R> => {
|
|
123
|
-
const headers = import.meta.server ? useRequestHeaders() : undefined
|
|
124
|
-
|
|
125
|
-
const result = await $fetch(endpoint, {
|
|
126
|
-
method: 'POST',
|
|
127
|
-
body: { query: doc, variables: vars },
|
|
128
|
-
headers: {
|
|
129
|
-
'Content-Type': 'application/json',
|
|
130
|
-
...headers,
|
|
131
|
-
},
|
|
132
|
-
})
|
|
133
|
-
|
|
134
|
-
return result as R
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
export const $${serviceName}Sdk: Sdk = getSdk(create${capitalizedServiceName}GraphQLClient())` : `// This file is auto-generated once by nitro-graphql for quick start
|
|
139
|
-
// You can modify this file according to your needs
|
|
140
|
-
import type { Sdk, Requester } from './sdk'
|
|
141
|
-
import { ofetch } from 'ofetch'
|
|
142
|
-
import { getSdk } from './sdk'
|
|
143
|
-
|
|
144
|
-
export function create${capitalizedServiceName}GraphQLClient(endpoint: string = '${endpoint}'): Requester {
|
|
145
|
-
return async <R>(doc: string, vars?: any): Promise<R> => {
|
|
146
|
-
const result = await ofetch(endpoint, {
|
|
147
|
-
method: 'POST',
|
|
148
|
-
body: { query: doc, variables: vars },
|
|
149
|
-
headers: {
|
|
150
|
-
'Content-Type': 'application/json',
|
|
151
|
-
},
|
|
152
|
-
})
|
|
153
|
-
|
|
154
|
-
return result as R
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
export const $${serviceName}Sdk: Sdk = getSdk(create${capitalizedServiceName}GraphQLClient())`, `${serviceName} external ofetch.ts`);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
/**
|
|
162
|
-
* Check for duplicate type definitions using a simpler approach
|
|
163
|
-
* Try to build each schema individually - if that succeeds but merging fails, we have duplicates
|
|
164
|
-
* @returns true if validation passes, false if duplicates found
|
|
165
|
-
*/
|
|
166
|
-
function validateNoDuplicateTypes(schemas, schemaStrings) {
|
|
167
|
-
const individualSchemasByFile = /* @__PURE__ */ new Map();
|
|
168
|
-
schemaStrings.forEach((schemaContent, index) => {
|
|
169
|
-
const schemaPath = schemas[index];
|
|
170
|
-
const fileName = basename(schemaPath);
|
|
171
|
-
try {
|
|
172
|
-
parse(schemaContent);
|
|
173
|
-
individualSchemasByFile.set(fileName, schemaContent);
|
|
174
|
-
} catch (error) {
|
|
175
|
-
consola.warn(`Invalid GraphQL syntax in ${fileName}:`, error);
|
|
176
|
-
throw error;
|
|
177
|
-
}
|
|
178
|
-
});
|
|
179
|
-
try {
|
|
180
|
-
mergeTypeDefs([schemaStrings.join("\n\n")], {
|
|
181
|
-
throwOnConflict: false,
|
|
182
|
-
commentDescriptions: true,
|
|
183
|
-
sort: true
|
|
184
|
-
});
|
|
185
|
-
mergeTypeDefs([schemaStrings.join("\n\n")], {
|
|
186
|
-
throwOnConflict: true,
|
|
187
|
-
commentDescriptions: true,
|
|
188
|
-
sort: true
|
|
189
|
-
});
|
|
190
|
-
} catch (conflictError) {
|
|
191
|
-
if (conflictError?.message?.includes("already defined with a different type")) throw conflictError;
|
|
192
|
-
}
|
|
193
|
-
const typeNames = /* @__PURE__ */ new Set();
|
|
194
|
-
const duplicateTypes = [];
|
|
195
|
-
schemaStrings.forEach((schemaContent, index) => {
|
|
196
|
-
const fileName = basename(schemas[index]);
|
|
197
|
-
try {
|
|
198
|
-
parse(schemaContent).definitions.forEach((def) => {
|
|
199
|
-
if (def.kind === "ObjectTypeDefinition" || def.kind === "InterfaceTypeDefinition" || def.kind === "UnionTypeDefinition" || def.kind === "EnumTypeDefinition" || def.kind === "InputObjectTypeDefinition" || def.kind === "ScalarTypeDefinition") {
|
|
200
|
-
const typeName = def.name.value;
|
|
201
|
-
if ([
|
|
202
|
-
"String",
|
|
203
|
-
"Int",
|
|
204
|
-
"Float",
|
|
205
|
-
"Boolean",
|
|
206
|
-
"ID",
|
|
207
|
-
"DateTime",
|
|
208
|
-
"JSON"
|
|
209
|
-
].includes(typeName)) return;
|
|
210
|
-
if (typeNames.has(typeName)) {
|
|
211
|
-
const existing = duplicateTypes.find((d) => d.type === typeName);
|
|
212
|
-
if (existing) existing.files.push(fileName);
|
|
213
|
-
else {
|
|
214
|
-
const firstFile = schemas.find((_, i) => {
|
|
215
|
-
const content = schemaStrings[i];
|
|
216
|
-
if (!content) return false;
|
|
217
|
-
try {
|
|
218
|
-
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);
|
|
219
|
-
} catch {
|
|
220
|
-
return false;
|
|
221
|
-
}
|
|
222
|
-
});
|
|
223
|
-
duplicateTypes.push({
|
|
224
|
-
type: typeName,
|
|
225
|
-
files: [basename(firstFile || ""), fileName]
|
|
226
|
-
});
|
|
227
|
-
}
|
|
228
|
-
} else typeNames.add(typeName);
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
} catch {}
|
|
232
|
-
});
|
|
233
|
-
if (duplicateTypes.length > 0) {
|
|
234
|
-
let errorMessage = "⚠️ DUPLICATE TYPE DEFINITIONS DETECTED!\n\n";
|
|
235
|
-
duplicateTypes.forEach(({ type, files }) => {
|
|
236
|
-
errorMessage += `❌ Type "${type}" is defined in multiple files:\n`;
|
|
237
|
-
files.forEach((fileName) => {
|
|
238
|
-
const fullPath = schemas.find((path) => basename(path) === fileName) || fileName;
|
|
239
|
-
errorMessage += ` • ${fullPath}\n`;
|
|
240
|
-
});
|
|
241
|
-
errorMessage += "\n";
|
|
242
|
-
});
|
|
243
|
-
errorMessage += "💡 Each GraphQL type should only be defined once.\n";
|
|
244
|
-
errorMessage += " Consider using \"extend type\" syntax instead of duplicate definitions.\n";
|
|
245
|
-
errorMessage += `\n🔍 Found ${duplicateTypes.length} duplicate type(s): ${duplicateTypes.map((d) => d.type).join(", ")}`;
|
|
246
|
-
consola.error(errorMessage);
|
|
247
|
-
return false;
|
|
248
|
-
}
|
|
249
|
-
return true;
|
|
250
|
-
}
|
|
251
|
-
async function serverTypeGeneration(app, options = {}) {
|
|
252
|
-
try {
|
|
253
|
-
if (!shouldGenerateTypes(app)) {
|
|
254
|
-
logger.debug("Server type generation is disabled");
|
|
255
|
-
return;
|
|
256
|
-
}
|
|
257
|
-
const schemas = app.scanSchemas || [];
|
|
258
|
-
if (!schemas.length) {
|
|
259
|
-
if (!options.silent) consola.info("No GraphQL definitions found for server type generation.");
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
const schemaStrings = loadFilesSync(schemas).map((schema$1) => typeof schema$1 === "string" ? schema$1 : schema$1.loc?.source?.body || "").filter(Boolean);
|
|
263
|
-
if (!validateNoDuplicateTypes(schemas, schemaStrings)) return;
|
|
264
|
-
const federationEnabled = app.options.graphql?.federation?.enabled === true;
|
|
265
|
-
const mergedSchemas = mergeTypeDefs([schemaStrings.join("\n\n")], {
|
|
266
|
-
throwOnConflict: true,
|
|
267
|
-
commentDescriptions: true,
|
|
268
|
-
sort: true
|
|
269
|
-
});
|
|
270
|
-
let schema;
|
|
271
|
-
if (federationEnabled) {
|
|
272
|
-
const buildSubgraph = await loadFederationSupport();
|
|
273
|
-
if (!buildSubgraph) throw new Error("Federation is enabled but @apollo/subgraph is not installed. Run: pnpm add @apollo/subgraph");
|
|
274
|
-
schema = buildSubgraph([{ typeDefs: parse(mergedSchemas) }]);
|
|
275
|
-
} else schema = buildSchema(mergedSchemas);
|
|
276
|
-
const data = await generateTypes(app.options.graphql?.framework || "graphql-yoga", schema, app.options.graphql ?? {});
|
|
277
|
-
const printSchema = printSchemaWithDirectives(schema);
|
|
278
|
-
const schemaPath = resolve(app.graphql.buildDir, "schema.graphql");
|
|
279
|
-
mkdirSync(dirname(schemaPath), { recursive: true });
|
|
280
|
-
writeFileSync(schemaPath, printSchema, "utf-8");
|
|
281
|
-
const placeholders = getDefaultPaths(app);
|
|
282
|
-
const typesConfig = getTypesConfig(app);
|
|
283
|
-
const serverTypesPath = resolveFilePath(typesConfig.server, typesConfig.enabled, true, "{typesDir}/nitro-graphql-server.d.ts", placeholders);
|
|
284
|
-
if (serverTypesPath) {
|
|
285
|
-
mkdirSync(dirname(serverTypesPath), { recursive: true });
|
|
286
|
-
writeFileSync(serverTypesPath, data, "utf-8");
|
|
287
|
-
if (!options.silent) logger.success(`Generated server types at: ${serverTypesPath}`);
|
|
288
|
-
}
|
|
289
|
-
} catch (error) {
|
|
290
|
-
logger.error("Server schema generation error:", error);
|
|
291
|
-
}
|
|
292
|
-
}
|
|
293
|
-
async function clientTypeGeneration(nitro, options = {}) {
|
|
294
|
-
try {
|
|
295
|
-
if (nitro.scanSchemas && nitro.scanSchemas.length > 0) await generateMainClientTypes(nitro, options);
|
|
296
|
-
if (nitro.options.graphql?.externalServices?.length) await generateExternalServicesTypes(nitro, options);
|
|
297
|
-
} catch (error) {
|
|
298
|
-
logger.error("Client schema generation error:", error);
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
/**
|
|
302
|
-
* Check for old structure files and warn user about manual migration
|
|
303
|
-
*/
|
|
304
|
-
function checkOldStructure(clientDir) {
|
|
305
|
-
const oldOfetchPath = resolve(clientDir, "ofetch.ts");
|
|
306
|
-
const oldSdkPath = resolve(clientDir, "sdk.ts");
|
|
307
|
-
if (existsSync(oldOfetchPath) || existsSync(oldSdkPath)) {
|
|
308
|
-
const foundFiles = [];
|
|
309
|
-
if (existsSync(oldOfetchPath)) foundFiles.push("app/graphql/ofetch.ts");
|
|
310
|
-
if (existsSync(oldSdkPath)) foundFiles.push("app/graphql/sdk.ts");
|
|
311
|
-
consola.error(`⚠️ OLD GRAPHQL STRUCTURE DETECTED!
|
|
312
|
-
|
|
313
|
-
📁 Found old files in app/graphql/ directory that need to be moved:
|
|
314
|
-
• ${foundFiles.join("\n • ")}
|
|
315
|
-
|
|
316
|
-
🔄 Please manually move these files to the new structure:
|
|
317
|
-
• app/graphql/ofetch.ts → app/graphql/default/ofetch.ts
|
|
318
|
-
• app/graphql/sdk.ts → app/graphql/default/sdk.ts
|
|
319
|
-
|
|
320
|
-
📝 Also update your app/graphql/index.ts to include:
|
|
321
|
-
export * from './default/ofetch'
|
|
322
|
-
|
|
323
|
-
💡 After moving, update your imports to use:
|
|
324
|
-
import { $sdk } from "#graphql/client"
|
|
325
|
-
|
|
326
|
-
🚫 The old files will cause import conflicts until moved!`);
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
async function generateMainClientTypes(nitro, options = {}) {
|
|
330
|
-
checkOldStructure(nitro.graphql.clientDir);
|
|
331
|
-
const docs = nitro.scanDocuments;
|
|
332
|
-
const loadDocs = await loadGraphQLDocuments(docs);
|
|
333
|
-
const schemaFilePath = join(nitro.graphql.buildDir, "schema.graphql");
|
|
334
|
-
if (!existsSync(schemaFilePath)) {
|
|
335
|
-
if (!options.silent) consola.info("Schema file not ready yet for client type generation. Server types need to be generated first.");
|
|
336
|
-
return;
|
|
337
|
-
}
|
|
338
|
-
const graphqlString = readFileSync(schemaFilePath, "utf-8");
|
|
339
|
-
const federationEnabled = nitro.options.graphql?.federation?.enabled === true;
|
|
340
|
-
let schema;
|
|
341
|
-
if (federationEnabled) {
|
|
342
|
-
const buildSubgraph = await loadFederationSupport();
|
|
343
|
-
if (!buildSubgraph) throw new Error("Federation is enabled but @apollo/subgraph is not installed. Run: pnpm add @apollo/subgraph");
|
|
344
|
-
schema = buildSubgraph([{ typeDefs: parse(graphqlString) }]);
|
|
345
|
-
} else schema = buildSchema(graphqlString);
|
|
346
|
-
const types = await generateClientTypes(schema, loadDocs, nitro.options.graphql?.codegen?.client ?? {}, nitro.options.graphql?.codegen?.clientSDK ?? {}, void 0, void 0, void 0, options);
|
|
347
|
-
if (types === false) return;
|
|
348
|
-
const placeholders = getDefaultPaths(nitro);
|
|
349
|
-
const typesConfig = getTypesConfig(nitro);
|
|
350
|
-
const sdkConfig = getSdkConfig(nitro);
|
|
351
|
-
const clientTypesPath = resolveFilePath(typesConfig.client, typesConfig.enabled, true, "{typesDir}/nitro-graphql-client.d.ts", placeholders);
|
|
352
|
-
if (clientTypesPath) {
|
|
353
|
-
mkdirSync(dirname(clientTypesPath), { recursive: true });
|
|
354
|
-
writeFileSync(clientTypesPath, types.types, "utf-8");
|
|
355
|
-
if (!options.silent) logger.success(`Generated client types at: ${clientTypesPath}`);
|
|
356
|
-
}
|
|
357
|
-
const sdkPath = resolveFilePath(sdkConfig.main, sdkConfig.enabled, true, "{clientGraphql}/default/sdk.ts", placeholders);
|
|
358
|
-
if (sdkPath) {
|
|
359
|
-
mkdirSync(dirname(sdkPath), { recursive: true });
|
|
360
|
-
writeFileSync(sdkPath, types.sdk, "utf-8");
|
|
361
|
-
if (!options.silent) logger.success(`Generated SDK at: ${sdkPath}`);
|
|
362
|
-
}
|
|
363
|
-
generateNuxtOfetchClient(nitro, nitro.graphql.clientDir, "default");
|
|
364
|
-
const externalServices = nitro.options.graphql?.externalServices || [];
|
|
365
|
-
if (externalServices.length > 0) generateGraphQLIndexFile(nitro, nitro.graphql.clientDir, externalServices);
|
|
366
|
-
}
|
|
367
|
-
async function generateExternalServicesTypes(nitro, options = {}) {
|
|
368
|
-
const externalServices = nitro.options.graphql?.externalServices || [];
|
|
369
|
-
for (const service of externalServices) try {
|
|
370
|
-
if (!options.silent) consola.info(`[graphql:${service.name}] Processing external service`);
|
|
371
|
-
await downloadAndSaveSchema(service, nitro.options.buildDir);
|
|
372
|
-
const schema = await loadExternalSchema(service, nitro.options.buildDir);
|
|
373
|
-
if (!schema) {
|
|
374
|
-
consola.warn(`[graphql:${service.name}] Failed to load schema, skipping`);
|
|
375
|
-
continue;
|
|
376
|
-
}
|
|
377
|
-
const documentPatterns = service.documents || [];
|
|
378
|
-
let loadDocs = [];
|
|
379
|
-
if (documentPatterns.length > 0) try {
|
|
380
|
-
loadDocs = await loadGraphQLDocuments(documentPatterns);
|
|
381
|
-
if (!loadDocs || loadDocs.length === 0) {
|
|
382
|
-
consola.warn(`[graphql:${service.name}] No GraphQL documents found, skipping service generation`);
|
|
383
|
-
continue;
|
|
384
|
-
}
|
|
385
|
-
} catch (error) {
|
|
386
|
-
consola.warn(`[graphql:${service.name}] No documents found, skipping service generation:`, error);
|
|
387
|
-
continue;
|
|
388
|
-
}
|
|
389
|
-
const types = await generateExternalClientTypes(service, schema, loadDocs);
|
|
390
|
-
if (types === false) {
|
|
391
|
-
consola.warn(`[graphql:${service.name}] Type generation failed`);
|
|
392
|
-
continue;
|
|
393
|
-
}
|
|
394
|
-
const placeholders = {
|
|
395
|
-
...getDefaultPaths(nitro),
|
|
396
|
-
serviceName: service.name
|
|
397
|
-
};
|
|
398
|
-
const typesConfig = getTypesConfig(nitro);
|
|
399
|
-
const sdkConfig = getSdkConfig(nitro);
|
|
400
|
-
const serviceTypesPath = resolveFilePath(service.paths?.types ?? typesConfig.external, typesConfig.enabled, true, "{typesDir}/nitro-graphql-client-{serviceName}.d.ts", placeholders);
|
|
401
|
-
if (serviceTypesPath) {
|
|
402
|
-
mkdirSync(dirname(serviceTypesPath), { recursive: true });
|
|
403
|
-
writeFileSync(serviceTypesPath, types.types, "utf-8");
|
|
404
|
-
if (!options.silent) consola.success(`[graphql:${service.name}] Generated types at: ${serviceTypesPath}`);
|
|
405
|
-
}
|
|
406
|
-
const serviceSdkPath = resolveFilePath(service.paths?.sdk ?? sdkConfig.external, sdkConfig.enabled, true, "{clientGraphql}/{serviceName}/sdk.ts", placeholders);
|
|
407
|
-
if (serviceSdkPath) {
|
|
408
|
-
mkdirSync(dirname(serviceSdkPath), { recursive: true });
|
|
409
|
-
writeFileSync(serviceSdkPath, types.sdk, "utf-8");
|
|
410
|
-
if (!options.silent) consola.success(`[graphql:${service.name}] Generated SDK at: ${serviceSdkPath}`);
|
|
411
|
-
}
|
|
412
|
-
generateExternalOfetchClient(nitro, service, service.endpoint);
|
|
413
|
-
if (!options.silent) consola.success(`[graphql:${service.name}] External service types generated successfully`);
|
|
414
|
-
} catch (error) {
|
|
415
|
-
consola.error(`[graphql:${service.name}] External service generation failed:`, error);
|
|
416
|
-
}
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
//#endregion
|
|
420
|
-
export { clientTypeGeneration, serverTypeGeneration };
|
|
7
|
+
export { clientTypeGeneration, generateExternalServicesTypes, generateMainClientTypes, generateServerTypes, serverTypeGeneration, validateNoDuplicateTypes };
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nitro-graphql",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "2.0.0-beta.
|
|
4
|
+
"version": "2.0.0-beta.32",
|
|
5
5
|
"description": "GraphQL integration for Nitro",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -112,6 +112,7 @@
|
|
|
112
112
|
"@nuxt/schema": "^4.2.1",
|
|
113
113
|
"@types/node": "^24.10.1",
|
|
114
114
|
"@vitejs/devtools": "^0.0.0-alpha.16",
|
|
115
|
+
"@vitest/ui": "^3.0.0",
|
|
115
116
|
"bumpp": "^10.3.1",
|
|
116
117
|
"changelogen": "^0.6.2",
|
|
117
118
|
"crossws": "^0.4.1",
|
|
@@ -122,7 +123,8 @@
|
|
|
122
123
|
"tsdown": "^0.16.6",
|
|
123
124
|
"typescript": "^5.9.3",
|
|
124
125
|
"vite": "npm:rolldown-vite@latest",
|
|
125
|
-
"vitepress-plugin-llms": "^1.9.3"
|
|
126
|
+
"vitepress-plugin-llms": "^1.9.3",
|
|
127
|
+
"vitest": "^3.0.0"
|
|
126
128
|
},
|
|
127
129
|
"resolutions": {
|
|
128
130
|
"nitro-graphql": "link:."
|
|
@@ -140,6 +142,10 @@
|
|
|
140
142
|
"docs:preview": "cd .docs && pnpm preview",
|
|
141
143
|
"lint": "eslint .",
|
|
142
144
|
"lint:fix": "eslint . --fix",
|
|
145
|
+
"test": "vitest",
|
|
146
|
+
"test:ui": "vitest --ui",
|
|
147
|
+
"test:run": "vitest run",
|
|
148
|
+
"test:coverage": "vitest run --coverage",
|
|
143
149
|
"test:types": "tsc --noEmit"
|
|
144
150
|
}
|
|
145
151
|
}
|