nitro-graphql 2.0.0-beta.1 → 2.0.0-beta.3
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 +1 -1
- package/dist/index.js +14 -3
- package/dist/rollup.js +15 -26
- package/dist/routes/apollo-server.d.ts +2 -2
- package/dist/routes/graphql-yoga.d.ts +2 -2
- package/dist/routes/health.d.ts +2 -2
- package/dist/utils/directive-parser.js +3 -7
- package/dist/utils/index.d.ts +2 -3
- package/dist/utils/index.js +47 -26
- package/dist/utils/type-generation.js +17 -40
- package/package.json +13 -13
package/README.md
CHANGED
|
@@ -52,7 +52,7 @@ pnpm add nitro-graphql @apollo/server @apollo/utils.withrequired @as-integration
|
|
|
52
52
|
|
|
53
53
|
```ts
|
|
54
54
|
// nitro.config.ts
|
|
55
|
-
import { defineNitroConfig } from '
|
|
55
|
+
import { defineNitroConfig } from 'nitro/config'
|
|
56
56
|
|
|
57
57
|
export default defineNitroConfig({
|
|
58
58
|
modules: ['nitro-graphql'],
|
package/dist/index.js
CHANGED
|
@@ -27,7 +27,7 @@ var src_default = defineNitroModule({
|
|
|
27
27
|
buildDir: "",
|
|
28
28
|
watchDirs: [],
|
|
29
29
|
clientDir: "",
|
|
30
|
-
serverDir: resolve(nitro.options.
|
|
30
|
+
serverDir: resolve(nitro.options.rootDir, "server", "graphql"),
|
|
31
31
|
dir: {
|
|
32
32
|
build: relative(nitro.options.rootDir, nitro.options.buildDir),
|
|
33
33
|
client: "graphql",
|
|
@@ -72,8 +72,11 @@ var src_default = defineNitroModule({
|
|
|
72
72
|
break;
|
|
73
73
|
}
|
|
74
74
|
case "nitro":
|
|
75
|
+
if (!nitro.options.graphql?.serverDir) nitro.graphql.serverDir = resolve(nitro.options.rootDir, "server", "graphql");
|
|
75
76
|
nitro.graphql.clientDir = resolve(nitro.options.rootDir, "graphql");
|
|
76
77
|
nitro.graphql.dir.client = "graphql";
|
|
78
|
+
watchDirs.push(nitro.graphql.clientDir);
|
|
79
|
+
watchDirs.push(nitro.graphql.serverDir);
|
|
77
80
|
break;
|
|
78
81
|
default:
|
|
79
82
|
}
|
|
@@ -90,7 +93,11 @@ var src_default = defineNitroModule({
|
|
|
90
93
|
ignoreInitial: true,
|
|
91
94
|
ignored: [...nitro.options.ignore, ...generateLayerIgnorePatterns(nitro)]
|
|
92
95
|
}).on("all", async (_, path) => {
|
|
93
|
-
if (path.endsWith(".graphql") || path.endsWith(".gql"))
|
|
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);
|
|
94
101
|
});
|
|
95
102
|
nitro.hooks.hook("close", () => {
|
|
96
103
|
watcher.close();
|
|
@@ -206,13 +213,17 @@ export default <IGraphQLConfig> {
|
|
|
206
213
|
}`, "utf-8");
|
|
207
214
|
}
|
|
208
215
|
if (!existsSync(nitro.graphql.serverDir)) mkdirSync(nitro.graphql.serverDir, { recursive: true });
|
|
209
|
-
if (!existsSync(join(nitro.graphql.serverDir, "schema.ts"))) writeFileSync(join(nitro.graphql.serverDir, "schema.ts"), `
|
|
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({
|
|
210
220
|
|
|
211
221
|
})
|
|
212
222
|
`, "utf-8");
|
|
213
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
|
|
214
224
|
// import * as tables from '../drizzle/schema/index'
|
|
215
225
|
// import { useDatabase } from '../utils/useDb'
|
|
226
|
+
import { defineGraphQLConfig } from 'nitro-graphql/utils/define'
|
|
216
227
|
|
|
217
228
|
export default defineGraphQLConfig({
|
|
218
229
|
// graphql-yoga example config
|
package/dist/rollup.js
CHANGED
|
@@ -37,7 +37,6 @@ async function rollupConfig(app) {
|
|
|
37
37
|
async buildStart() {
|
|
38
38
|
const graphqlFiles = await scanGraphql(nitro);
|
|
39
39
|
for (const file of graphqlFiles) this.addWatchFile(file);
|
|
40
|
-
this.addWatchFile(app.graphql.serverDir);
|
|
41
40
|
}
|
|
42
41
|
});
|
|
43
42
|
}
|
|
@@ -48,9 +47,7 @@ async function rollupConfig(app) {
|
|
|
48
47
|
});
|
|
49
48
|
}
|
|
50
49
|
function virtualSchemas(app) {
|
|
51
|
-
const getSchemas = () =>
|
|
52
|
-
return [...app.scanSchemas, ...app.options.graphql?.typedefs ?? []];
|
|
53
|
-
};
|
|
50
|
+
const getSchemas = () => [...app.scanSchemas, ...app.options.graphql?.typedefs ?? []];
|
|
54
51
|
app.options.virtual ??= {};
|
|
55
52
|
app.options.virtual["#nitro-internal-virtual/server-schemas"] = () => {
|
|
56
53
|
const imports = getSchemas();
|
|
@@ -64,43 +61,35 @@ ${imports.map((h) => `{ def: ${getImportId(h)} }`).join(",\n")}
|
|
|
64
61
|
};
|
|
65
62
|
}
|
|
66
63
|
function virtualResolvers(app) {
|
|
67
|
-
const getResolvers = () =>
|
|
68
|
-
return [...app.scanResolvers];
|
|
69
|
-
};
|
|
64
|
+
const getResolvers = () => [...app.scanResolvers];
|
|
70
65
|
app.options.virtual ??= {};
|
|
71
66
|
app.options.virtual["#nitro-internal-virtual/server-resolvers"] = () => {
|
|
72
67
|
const imports = getResolvers();
|
|
73
|
-
const importsContent =
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
68
|
+
const importsContent = imports.map(({ specifier, imports: imports$1, options }) => genImport(specifier, imports$1, options));
|
|
69
|
+
const data = imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ resolver: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n");
|
|
70
|
+
return [
|
|
71
|
+
...importsContent,
|
|
77
72
|
"export const resolvers = [",
|
|
78
|
-
|
|
73
|
+
data,
|
|
79
74
|
"]",
|
|
80
75
|
""
|
|
81
|
-
];
|
|
82
|
-
content.unshift(...importsContent);
|
|
83
|
-
return content.join("\n");
|
|
76
|
+
].join("\n");
|
|
84
77
|
};
|
|
85
78
|
}
|
|
86
79
|
function virtualDirectives(app) {
|
|
87
|
-
const getDirectives = () =>
|
|
88
|
-
return [...app.scanDirectives || []];
|
|
89
|
-
};
|
|
80
|
+
const getDirectives = () => app.scanDirectives || [];
|
|
90
81
|
app.options.virtual ??= {};
|
|
91
82
|
app.options.virtual["#nitro-internal-virtual/server-directives"] = () => {
|
|
92
83
|
const imports = getDirectives();
|
|
93
|
-
const importsContent =
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
84
|
+
const importsContent = imports.map(({ specifier, imports: imports$1, options }) => genImport(specifier, imports$1, options));
|
|
85
|
+
const data = imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ directive: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n");
|
|
86
|
+
return [
|
|
87
|
+
...importsContent,
|
|
97
88
|
"export const directives = [",
|
|
98
|
-
|
|
89
|
+
data,
|
|
99
90
|
"]",
|
|
100
91
|
""
|
|
101
|
-
];
|
|
102
|
-
content.unshift(...importsContent);
|
|
103
|
-
return content.join("\n");
|
|
92
|
+
].join("\n");
|
|
104
93
|
};
|
|
105
94
|
}
|
|
106
95
|
function getGraphQLConfig(app) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as h30 from "h3";
|
|
2
2
|
|
|
3
3
|
//#region src/routes/apollo-server.d.ts
|
|
4
|
-
declare const _default:
|
|
4
|
+
declare const _default: h30.EventHandler<h30.EventHandlerRequest, unknown>;
|
|
5
5
|
//#endregion
|
|
6
6
|
export { _default as default };
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as h33 from "h3";
|
|
2
2
|
|
|
3
3
|
//#region src/routes/graphql-yoga.d.ts
|
|
4
|
-
declare const _default:
|
|
4
|
+
declare const _default: h33.EventHandler<h33.EventHandlerRequest, unknown>;
|
|
5
5
|
//#endregion
|
|
6
6
|
export { _default as default };
|
package/dist/routes/health.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as h31 from "h3";
|
|
2
2
|
|
|
3
3
|
//#region src/routes/health.d.ts
|
|
4
|
-
declare const _default:
|
|
4
|
+
declare const _default: h31.EventHandler<h31.EventHandlerRequest, unknown>;
|
|
5
5
|
//#endregion
|
|
6
6
|
export { _default as default };
|
|
@@ -18,13 +18,9 @@ var DirectiveParser = class {
|
|
|
18
18
|
sourceType: "module",
|
|
19
19
|
astType: "ts"
|
|
20
20
|
});
|
|
21
|
-
if (result.errors.length > 0)
|
|
22
|
-
console.warn(`Parse errors in ${filePath}:`, result.errors.map((e) => e.message));
|
|
23
|
-
return [];
|
|
24
|
-
}
|
|
21
|
+
if (result.errors.length > 0) return [];
|
|
25
22
|
return this.extractDirectiveDefinitions(result.program);
|
|
26
|
-
} catch
|
|
27
|
-
console.warn(`Failed to parse ${filePath} with oxc:`, error);
|
|
23
|
+
} catch {
|
|
28
24
|
return [];
|
|
29
25
|
}
|
|
30
26
|
}
|
|
@@ -205,7 +201,7 @@ async function generateDirectiveSchemas(nitro, directives) {
|
|
|
205
201
|
}
|
|
206
202
|
}
|
|
207
203
|
if (directiveSchemas.length > 0) {
|
|
208
|
-
const directivesPath = resolve(nitro.graphql.
|
|
204
|
+
const directivesPath = resolve(nitro.graphql.buildDir, "_directives.graphql");
|
|
209
205
|
const content = `# WARNING: This file is auto-generated by nitro-graphql
|
|
210
206
|
# Do not modify this file directly. It will be overwritten.
|
|
211
207
|
# To define custom directives, create .directive.ts files using defineDirective()
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -19,13 +19,12 @@ declare function getLayerAppDirectories(nitro: Nitro): string[];
|
|
|
19
19
|
/**
|
|
20
20
|
* Generate layer-aware ignore patterns for auto-generated files
|
|
21
21
|
*/
|
|
22
|
-
declare function generateLayerIgnorePatterns(
|
|
22
|
+
declare function generateLayerIgnorePatterns(): string[];
|
|
23
23
|
declare function getImportId(p: string, lazy?: boolean): string;
|
|
24
24
|
declare function relativeWithDot(from: string, to: string): string;
|
|
25
25
|
declare function scanGraphql(nitro: Nitro): Promise<string[]>;
|
|
26
26
|
declare function scanResolvers(nitro: Nitro): Promise<GenImport[]>;
|
|
27
27
|
declare function scanDirectives(nitro: Nitro): Promise<GenImport[]>;
|
|
28
|
-
declare function scanTypeDefs(nitro: Nitro): Promise<string[]>;
|
|
29
28
|
declare function scanSchemas(nitro: Nitro): Promise<string[]>;
|
|
30
29
|
declare function scanDocs(nitro: Nitro): Promise<string[]>;
|
|
31
30
|
/**
|
|
@@ -37,4 +36,4 @@ declare function scanExternalServiceDocs(nitro: Nitro, serviceName: string, patt
|
|
|
37
36
|
*/
|
|
38
37
|
declare function validateExternalServices(services: unknown[]): string[];
|
|
39
38
|
//#endregion
|
|
40
|
-
export { GLOB_SCAN_PATTERN, directiveParser, generateDirectiveSchema, generateDirectiveSchemas, generateLayerIgnorePatterns, getImportId, getLayerAppDirectories, getLayerDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives, scanDocs, scanExternalServiceDocs, scanGraphql, scanResolvers, scanSchemas,
|
|
39
|
+
export { GLOB_SCAN_PATTERN, directiveParser, generateDirectiveSchema, generateDirectiveSchemas, generateLayerIgnorePatterns, getImportId, getLayerAppDirectories, getLayerDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives, scanDocs, scanExternalServiceDocs, scanGraphql, scanResolvers, scanSchemas, validateExternalServices };
|
package/dist/utils/index.js
CHANGED
|
@@ -28,12 +28,8 @@ function getLayerAppDirectories(nitro) {
|
|
|
28
28
|
/**
|
|
29
29
|
* Generate layer-aware ignore patterns for auto-generated files
|
|
30
30
|
*/
|
|
31
|
-
function generateLayerIgnorePatterns(
|
|
32
|
-
|
|
33
|
-
patterns.push(`${nitro.graphql.serverDir}/_directives.graphql`);
|
|
34
|
-
const layerServerDirs = nitro.options.graphql?.layerServerDirs || [];
|
|
35
|
-
for (const layerServerDir of layerServerDirs) patterns.push(`${layerServerDir}/graphql/_directives.graphql`);
|
|
36
|
-
return patterns;
|
|
31
|
+
function generateLayerIgnorePatterns() {
|
|
32
|
+
return [];
|
|
37
33
|
}
|
|
38
34
|
function getImportId(p, lazy) {
|
|
39
35
|
return (lazy ? "_lazy_" : "_") + hash(p).replace(/-/g, "").slice(0, 6);
|
|
@@ -44,10 +40,30 @@ function relativeWithDot(from, to) {
|
|
|
44
40
|
return RELATIVE_RE.test(rel) ? rel : `./${rel}`;
|
|
45
41
|
}
|
|
46
42
|
async function scanGraphql(nitro) {
|
|
47
|
-
|
|
43
|
+
const serverDirRelative = relative(nitro.options.rootDir, nitro.graphql.serverDir);
|
|
44
|
+
const files = await scanDir(nitro, nitro.options.rootDir, serverDirRelative, "**/*.{graphql,gql}");
|
|
45
|
+
const layerServerDirs = getLayerServerDirectories(nitro);
|
|
46
|
+
const layerFiles = await Promise.all(layerServerDirs.map((layerServerDir) => scanDir(nitro, layerServerDir, "graphql", "**/*.{graphql,gql}"))).then((r) => r.flat());
|
|
47
|
+
const allFiles = [...files, ...layerFiles];
|
|
48
|
+
const seenPaths = /* @__PURE__ */ new Set();
|
|
49
|
+
return allFiles.filter((file) => {
|
|
50
|
+
if (seenPaths.has(file.fullPath)) return false;
|
|
51
|
+
seenPaths.add(file.fullPath);
|
|
52
|
+
return true;
|
|
53
|
+
}).map((f) => f.fullPath);
|
|
48
54
|
}
|
|
49
55
|
async function scanResolvers(nitro) {
|
|
50
|
-
const
|
|
56
|
+
const serverDirRelative = relative(nitro.options.rootDir, nitro.graphql.serverDir);
|
|
57
|
+
const regularFiles = await scanDir(nitro, nitro.options.rootDir, serverDirRelative, "**/*.resolver.{ts,js}");
|
|
58
|
+
const layerServerDirs = getLayerServerDirectories(nitro);
|
|
59
|
+
const layerFiles = await Promise.all(layerServerDirs.map((layerServerDir) => scanDir(nitro, layerServerDir, "graphql", "**/*.resolver.{ts,js}"))).then((r) => r.flat());
|
|
60
|
+
const allFiles = [...regularFiles, ...layerFiles];
|
|
61
|
+
const seenPaths = /* @__PURE__ */ new Set();
|
|
62
|
+
const files = allFiles.filter((file) => {
|
|
63
|
+
if (seenPaths.has(file.fullPath)) return false;
|
|
64
|
+
seenPaths.add(file.fullPath);
|
|
65
|
+
return true;
|
|
66
|
+
});
|
|
51
67
|
const exportName = [];
|
|
52
68
|
for (const file of files) {
|
|
53
69
|
const fileContent = await readFile(file.fullPath, "utf-8");
|
|
@@ -97,7 +113,17 @@ async function scanResolvers(nitro) {
|
|
|
97
113
|
return exportName;
|
|
98
114
|
}
|
|
99
115
|
async function scanDirectives(nitro) {
|
|
100
|
-
const
|
|
116
|
+
const serverDirRelative = relative(nitro.options.rootDir, nitro.graphql.serverDir);
|
|
117
|
+
const regularFiles = await scanDir(nitro, nitro.options.rootDir, serverDirRelative, "**/*.directive.{ts,js}");
|
|
118
|
+
const layerServerDirs = getLayerServerDirectories(nitro);
|
|
119
|
+
const layerFiles = await Promise.all(layerServerDirs.map((layerServerDir) => scanDir(nitro, layerServerDir, "graphql", "**/*.directive.{ts,js}"))).then((r) => r.flat());
|
|
120
|
+
const allFiles = [...regularFiles, ...layerFiles];
|
|
121
|
+
const seenPaths = /* @__PURE__ */ new Set();
|
|
122
|
+
const files = allFiles.filter((file) => {
|
|
123
|
+
if (seenPaths.has(file.fullPath)) return false;
|
|
124
|
+
seenPaths.add(file.fullPath);
|
|
125
|
+
return true;
|
|
126
|
+
});
|
|
101
127
|
const exportName = [];
|
|
102
128
|
for (const file of files) {
|
|
103
129
|
const fileContent = await readFile(file.fullPath, "utf-8");
|
|
@@ -121,11 +147,18 @@ async function scanDirectives(nitro) {
|
|
|
121
147
|
}
|
|
122
148
|
return exportName;
|
|
123
149
|
}
|
|
124
|
-
async function scanTypeDefs(nitro) {
|
|
125
|
-
return (await scanFiles(nitro, "graphql", "**/*.typedef.{ts,js}")).map((f) => f.fullPath);
|
|
126
|
-
}
|
|
127
150
|
async function scanSchemas(nitro) {
|
|
128
|
-
|
|
151
|
+
const serverDirRelative = relative(nitro.options.rootDir, nitro.graphql.serverDir);
|
|
152
|
+
const files = await scanDir(nitro, nitro.options.rootDir, serverDirRelative, "**/*.graphql");
|
|
153
|
+
const layerServerDirs = getLayerServerDirectories(nitro);
|
|
154
|
+
const layerFiles = await Promise.all(layerServerDirs.map((layerServerDir) => scanDir(nitro, layerServerDir, "graphql", "**/*.graphql"))).then((r) => r.flat());
|
|
155
|
+
const allFiles = [...files, ...layerFiles];
|
|
156
|
+
const seenPaths = /* @__PURE__ */ new Set();
|
|
157
|
+
return allFiles.filter((file) => {
|
|
158
|
+
if (seenPaths.has(file.fullPath)) return false;
|
|
159
|
+
seenPaths.add(file.fullPath);
|
|
160
|
+
return true;
|
|
161
|
+
}).map((f) => f.fullPath);
|
|
129
162
|
}
|
|
130
163
|
async function scanDocs(nitro) {
|
|
131
164
|
const files = await scanDir(nitro, nitro.options.rootDir, nitro.graphql.dir.client, "**/*.graphql");
|
|
@@ -193,18 +226,6 @@ function validateExternalServices(services) {
|
|
|
193
226
|
}
|
|
194
227
|
return errors;
|
|
195
228
|
}
|
|
196
|
-
async function scanFiles(nitro, name, globPattern = GLOB_SCAN_PATTERN) {
|
|
197
|
-
const regularFiles = await Promise.all(nitro.options.scanDirs.map((dir) => scanDir(nitro, dir, name, globPattern))).then((r) => r.flat());
|
|
198
|
-
const layerDirectories = getLayerDirectories(nitro);
|
|
199
|
-
const layerFiles = await Promise.all(layerDirectories.map((layerDir) => scanDir(nitro, layerDir, `server/${name}`, globPattern))).then((r) => r.flat());
|
|
200
|
-
const allFiles = [...regularFiles, ...layerFiles];
|
|
201
|
-
const seenPaths = /* @__PURE__ */ new Set();
|
|
202
|
-
return allFiles.filter((file) => {
|
|
203
|
-
if (seenPaths.has(file.fullPath)) return false;
|
|
204
|
-
seenPaths.add(file.fullPath);
|
|
205
|
-
return true;
|
|
206
|
-
});
|
|
207
|
-
}
|
|
208
229
|
async function scanDir(nitro, dir, name, globPattern = GLOB_SCAN_PATTERN) {
|
|
209
230
|
return (await glob(join(name, globPattern), {
|
|
210
231
|
cwd: dir,
|
|
@@ -226,4 +247,4 @@ async function scanDir(nitro, dir, name, globPattern = GLOB_SCAN_PATTERN) {
|
|
|
226
247
|
}
|
|
227
248
|
|
|
228
249
|
//#endregion
|
|
229
|
-
export { GLOB_SCAN_PATTERN, directiveParser, generateDirectiveSchema, generateDirectiveSchemas, generateLayerIgnorePatterns, getImportId, getLayerAppDirectories, getLayerDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives, scanDocs, scanExternalServiceDocs, scanGraphql, scanResolvers, scanSchemas,
|
|
250
|
+
export { GLOB_SCAN_PATTERN, directiveParser, generateDirectiveSchema, generateDirectiveSchemas, generateLayerIgnorePatterns, getImportId, getLayerAppDirectories, getLayerDirectories, getLayerServerDirectories, relativeWithDot, scanDirectives, scanDocs, scanExternalServiceDocs, scanGraphql, scanResolvers, scanSchemas, validateExternalServices };
|
|
@@ -27,16 +27,20 @@ export * from './default/ofetch'
|
|
|
27
27
|
writeFileSync(indexPath, indexContent, "utf-8");
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
|
-
function
|
|
30
|
+
function generateOfetchClient(clientDir, serviceName, endpoint, isDefault = false) {
|
|
31
31
|
const serviceDir = resolve(clientDir, serviceName);
|
|
32
32
|
const ofetchPath = resolve(serviceDir, "ofetch.ts");
|
|
33
33
|
if (!existsSync(serviceDir)) mkdirSync(serviceDir, { recursive: true });
|
|
34
|
-
if (
|
|
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
|
|
35
39
|
// You can modify this file according to your needs
|
|
36
|
-
import type { Requester } from './sdk'
|
|
40
|
+
import type { ${isDefault ? "Requester" : "Sdk, Requester"} } from './sdk'
|
|
37
41
|
import { getSdk } from './sdk'
|
|
38
42
|
|
|
39
|
-
export function
|
|
43
|
+
export function ${functionName}(endpoint: string${isDefault ? "" : ` = '${endpoint}'`}): Requester {
|
|
40
44
|
return async <R>(doc: string, vars?: any): Promise<R> => {
|
|
41
45
|
const headers = import.meta.server ? useRequestHeaders() : undefined
|
|
42
46
|
|
|
@@ -53,38 +57,7 @@ export function createGraphQLClient(endpoint: string): Requester {
|
|
|
53
57
|
}
|
|
54
58
|
}
|
|
55
59
|
|
|
56
|
-
export const $
|
|
57
|
-
}
|
|
58
|
-
function generateExternalOfetchClient(clientDir, serviceName, endpoint) {
|
|
59
|
-
const serviceDir = resolve(clientDir, serviceName);
|
|
60
|
-
const ofetchPath = resolve(serviceDir, "ofetch.ts");
|
|
61
|
-
if (!existsSync(serviceDir)) mkdirSync(serviceDir, { recursive: true });
|
|
62
|
-
if (!existsSync(ofetchPath)) {
|
|
63
|
-
const capitalizedServiceName = serviceName.charAt(0).toUpperCase() + serviceName.slice(1);
|
|
64
|
-
writeFileSync(ofetchPath, `// This file is auto-generated once by nitro-graphql for quick start
|
|
65
|
-
// You can modify this file according to your needs
|
|
66
|
-
import type { Sdk, Requester } from './sdk'
|
|
67
|
-
import { getSdk } from './sdk'
|
|
68
|
-
|
|
69
|
-
export function create${capitalizedServiceName}GraphQLClient(endpoint: string = '${endpoint}'): Requester {
|
|
70
|
-
return async <R>(doc: string, vars?: any): Promise<R> => {
|
|
71
|
-
const headers = import.meta.server ? useRequestHeaders() : undefined
|
|
72
|
-
|
|
73
|
-
const result = await $fetch(endpoint, {
|
|
74
|
-
method: 'POST',
|
|
75
|
-
body: { query: doc, variables: vars },
|
|
76
|
-
headers: {
|
|
77
|
-
'Content-Type': 'application/json',
|
|
78
|
-
...headers,
|
|
79
|
-
},
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
return result as R
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export const $${serviceName}Sdk: Sdk = getSdk(create${capitalizedServiceName}GraphQLClient())`, "utf-8");
|
|
87
|
-
}
|
|
60
|
+
export const ${exportName}${isDefault ? "" : ": Sdk"} = getSdk(${functionName}(${isDefault ? "'/api/graphql'" : ""}))`, "utf-8");
|
|
88
61
|
}
|
|
89
62
|
/**
|
|
90
63
|
* Check for duplicate type definitions using a simpler approach
|
|
@@ -258,9 +231,11 @@ async function generateMainClientTypes(nitro) {
|
|
|
258
231
|
mkdirSync(dirname(clientTypesPath), { recursive: true });
|
|
259
232
|
writeFileSync(clientTypesPath, types.types, "utf-8");
|
|
260
233
|
mkdirSync(defaultServiceDir, { recursive: true });
|
|
261
|
-
|
|
234
|
+
let shouldWriteSdk = true;
|
|
235
|
+
if (existsSync(sdkTypesPath)) shouldWriteSdk = readFileSync(sdkTypesPath, "utf-8") !== types.sdk;
|
|
236
|
+
if (shouldWriteSdk) writeFileSync(sdkTypesPath, types.sdk, "utf-8");
|
|
262
237
|
if (nitro.options.framework?.name === "nuxt") {
|
|
263
|
-
|
|
238
|
+
generateOfetchClient(nitro.graphql.clientDir, "default", "/api/graphql", true);
|
|
264
239
|
const externalServices = nitro.options.graphql?.externalServices || [];
|
|
265
240
|
generateGraphQLIndexFile(nitro.graphql.clientDir, externalServices);
|
|
266
241
|
}
|
|
@@ -298,8 +273,10 @@ async function generateExternalServicesTypes(nitro) {
|
|
|
298
273
|
mkdirSync(dirname(serviceTypesPath), { recursive: true });
|
|
299
274
|
writeFileSync(serviceTypesPath, types.types, "utf-8");
|
|
300
275
|
mkdirSync(serviceDir, { recursive: true });
|
|
301
|
-
|
|
302
|
-
if (
|
|
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);
|
|
303
280
|
consola.success(`[graphql:${service.name}] External service types generated successfully`);
|
|
304
281
|
} catch (error) {
|
|
305
282
|
consola.error(`[graphql:${service.name}] External service generation failed:`, error);
|
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.3",
|
|
5
5
|
"description": "GraphQL integration for Nitro",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -78,13 +78,13 @@
|
|
|
78
78
|
}
|
|
79
79
|
},
|
|
80
80
|
"dependencies": {
|
|
81
|
-
"@apollo/subgraph": "^2.11.
|
|
81
|
+
"@apollo/subgraph": "^2.11.3",
|
|
82
82
|
"@graphql-codegen/core": "^5.0.0",
|
|
83
83
|
"@graphql-codegen/import-types-preset": "^3.0.1",
|
|
84
|
-
"@graphql-codegen/typescript": "^5.0.
|
|
84
|
+
"@graphql-codegen/typescript": "^5.0.2",
|
|
85
85
|
"@graphql-codegen/typescript-generic-sdk": "^4.0.2",
|
|
86
|
-
"@graphql-codegen/typescript-operations": "^5.0.
|
|
87
|
-
"@graphql-codegen/typescript-resolvers": "^5.0
|
|
86
|
+
"@graphql-codegen/typescript-operations": "^5.0.2",
|
|
87
|
+
"@graphql-codegen/typescript-resolvers": "^5.1.0",
|
|
88
88
|
"@graphql-tools/graphql-file-loader": "^8.1.2",
|
|
89
89
|
"@graphql-tools/load": "^8.1.2",
|
|
90
90
|
"@graphql-tools/load-files": "^7.0.1",
|
|
@@ -96,19 +96,19 @@
|
|
|
96
96
|
"consola": "^3.4.2",
|
|
97
97
|
"defu": "^6.1.4",
|
|
98
98
|
"graphql-config": "^5.1.5",
|
|
99
|
-
"graphql-scalars": "^1.
|
|
99
|
+
"graphql-scalars": "^1.25.0",
|
|
100
100
|
"knitwork": "^1.2.0",
|
|
101
101
|
"ohash": "^2.0.11",
|
|
102
|
-
"oxc-parser": "^0.
|
|
102
|
+
"oxc-parser": "^0.95.0",
|
|
103
103
|
"pathe": "^2.0.3",
|
|
104
104
|
"tinyglobby": "^0.2.15"
|
|
105
105
|
},
|
|
106
106
|
"devDependencies": {
|
|
107
|
-
"@antfu/eslint-config": "^
|
|
108
|
-
"@nuxt/kit": "^4.1.
|
|
109
|
-
"@nuxt/schema": "^4.1.
|
|
110
|
-
"@types/node": "^24.
|
|
111
|
-
"bumpp": "^10.
|
|
107
|
+
"@antfu/eslint-config": "^6.0.0",
|
|
108
|
+
"@nuxt/kit": "^4.1.3",
|
|
109
|
+
"@nuxt/schema": "^4.1.3",
|
|
110
|
+
"@types/node": "^24.7.2",
|
|
111
|
+
"bumpp": "^10.3.1",
|
|
112
112
|
"changelogen": "^0.6.2",
|
|
113
113
|
"crossws": "0.3.5",
|
|
114
114
|
"eslint": "^9.37.0",
|
|
@@ -116,7 +116,7 @@
|
|
|
116
116
|
"graphql-yoga": "^5.16.0",
|
|
117
117
|
"h3": "^2.0.1-rc.2",
|
|
118
118
|
"nitro": "^3.0.1-alpha.0",
|
|
119
|
-
"tsdown": "^0.15.
|
|
119
|
+
"tsdown": "^0.15.7",
|
|
120
120
|
"typescript": "^5.9.3"
|
|
121
121
|
},
|
|
122
122
|
"resolutions": {
|