nitro-graphql 1.4.2 → 1.4.4
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/ecosystem/nuxt.js +1 -2
- package/dist/index.js +6 -13
- package/dist/rollup.js +8 -16
- package/dist/routes/apollo-server.d.ts +2 -2
- package/dist/routes/apollo-server.js +11 -5
- package/dist/routes/graphql-yoga.d.ts +2 -2
- package/dist/routes/graphql-yoga.js +1 -2
- package/dist/routes/health.d.ts +2 -2
- package/dist/utils/apollo.d.ts +1 -0
- package/dist/utils/apollo.js +3 -4
- package/dist/utils/client-codegen.js +15 -24
- package/dist/utils/directive-parser.js +7 -16
- package/dist/utils/index.js +7 -13
- package/dist/utils/server-codegen.js +1 -2
- package/dist/utils/type-generation.js +8 -17
- package/package.json +24 -24
package/dist/ecosystem/nuxt.js
CHANGED
|
@@ -44,8 +44,7 @@ var nuxt_default = defineNuxtModule({
|
|
|
44
44
|
nitroConfig.graphql.layerServerDirs = layerServerDirs;
|
|
45
45
|
nitroConfig.graphql.layerAppDirs = layerAppDirs;
|
|
46
46
|
const appGraphqlDir = resolve(nuxt.options.rootDir, "app/graphql");
|
|
47
|
-
|
|
48
|
-
if (!hasAppGraphqlDir) {
|
|
47
|
+
if (!existsSync(appGraphqlDir)) {
|
|
49
48
|
const defaultDir = join(clientDir, "default");
|
|
50
49
|
if (!existsSync(defaultDir)) mkdirSync(defaultDir, { recursive: true });
|
|
51
50
|
const sampleQueryFile = join(defaultDir, "queries.graphql");
|
package/dist/index.js
CHANGED
|
@@ -99,25 +99,19 @@ var src_default = defineNitroModule({
|
|
|
99
99
|
const tsConfigPath = resolve(nitro.options.buildDir, nitro.options.typescript.tsconfigPath);
|
|
100
100
|
const tsconfigDir = dirname(tsConfigPath);
|
|
101
101
|
const typesDir = resolve(nitro.options.buildDir, "types");
|
|
102
|
-
|
|
103
|
-
nitro.
|
|
104
|
-
|
|
105
|
-
nitro.scanDocuments = docs;
|
|
106
|
-
const resolvers = await scanResolvers(nitro);
|
|
107
|
-
nitro.scanResolvers = resolvers;
|
|
102
|
+
nitro.scanSchemas = await scanSchemas(nitro);
|
|
103
|
+
nitro.scanDocuments = await scanDocs(nitro);
|
|
104
|
+
nitro.scanResolvers = await scanResolvers(nitro);
|
|
108
105
|
const directives = await scanDirectives(nitro);
|
|
109
106
|
nitro.scanDirectives = directives;
|
|
110
107
|
await generateDirectiveSchemas(nitro, directives);
|
|
111
108
|
nitro.hooks.hook("dev:start", async () => {
|
|
112
|
-
|
|
113
|
-
nitro.
|
|
114
|
-
const resolvers$1 = await scanResolvers(nitro);
|
|
115
|
-
nitro.scanResolvers = resolvers$1;
|
|
109
|
+
nitro.scanSchemas = await scanSchemas(nitro);
|
|
110
|
+
nitro.scanResolvers = await scanResolvers(nitro);
|
|
116
111
|
const directives$1 = await scanDirectives(nitro);
|
|
117
112
|
nitro.scanDirectives = directives$1;
|
|
118
113
|
await generateDirectiveSchemas(nitro, directives$1);
|
|
119
|
-
|
|
120
|
-
nitro.scanDocuments = docs$1;
|
|
114
|
+
nitro.scanDocuments = await scanDocs(nitro);
|
|
121
115
|
});
|
|
122
116
|
await rollupConfig(nitro);
|
|
123
117
|
await serverTypeGeneration(nitro);
|
|
@@ -170,7 +164,6 @@ var src_default = defineNitroModule({
|
|
|
170
164
|
if (id.endsWith(".graphql") || id.endsWith(".gql")) return "schemas";
|
|
171
165
|
if (id.endsWith(".resolver.ts")) return "resolvers";
|
|
172
166
|
if (typeof manualChunks === "function") return manualChunks(id, meta);
|
|
173
|
-
return void 0;
|
|
174
167
|
};
|
|
175
168
|
rollupConfig$1.output.chunkFileNames = (chunkInfo) => {
|
|
176
169
|
if (chunkInfo.moduleIds && chunkInfo.moduleIds.some((id) => id.endsWith(".graphql") || id.endsWith(".resolver.ts") || id.endsWith(".gql"))) return `chunks/graphql/[name].mjs`;
|
package/dist/rollup.js
CHANGED
|
@@ -49,26 +49,23 @@ async function rollupConfig(app) {
|
|
|
49
49
|
}
|
|
50
50
|
function virtualSchemas(app) {
|
|
51
51
|
const getSchemas = () => {
|
|
52
|
-
|
|
53
|
-
return schemas;
|
|
52
|
+
return [...app.scanSchemas, ...app.options.graphql?.typedefs ?? []];
|
|
54
53
|
};
|
|
55
54
|
app.options.virtual ??= {};
|
|
56
55
|
app.options.virtual["#nitro-internal-virtual/server-schemas"] = () => {
|
|
57
56
|
const imports = getSchemas();
|
|
58
|
-
|
|
57
|
+
return `
|
|
59
58
|
${imports.map((handler) => `import ${getImportId(handler)} from '${handler}';`).join("\n")}
|
|
60
59
|
|
|
61
60
|
export const schemas = [
|
|
62
61
|
${imports.map((h) => `{ def: ${getImportId(h)} }`).join(",\n")}
|
|
63
62
|
];
|
|
64
63
|
`;
|
|
65
|
-
return code;
|
|
66
64
|
};
|
|
67
65
|
}
|
|
68
66
|
function virtualResolvers(app) {
|
|
69
67
|
const getResolvers = () => {
|
|
70
|
-
|
|
71
|
-
return resolvers;
|
|
68
|
+
return [...app.scanResolvers];
|
|
72
69
|
};
|
|
73
70
|
app.options.virtual ??= {};
|
|
74
71
|
app.options.virtual["#nitro-internal-virtual/server-resolvers"] = () => {
|
|
@@ -76,22 +73,19 @@ function virtualResolvers(app) {
|
|
|
76
73
|
const importsContent = [...imports.map(({ specifier, imports: imports$1, options }) => {
|
|
77
74
|
return genImport(specifier, imports$1, options);
|
|
78
75
|
})];
|
|
79
|
-
const data = imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ resolver: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n");
|
|
80
76
|
const content = [
|
|
81
77
|
"export const resolvers = [",
|
|
82
|
-
|
|
78
|
+
imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ resolver: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n"),
|
|
83
79
|
"]",
|
|
84
80
|
""
|
|
85
81
|
];
|
|
86
82
|
content.unshift(...importsContent);
|
|
87
|
-
|
|
88
|
-
return code;
|
|
83
|
+
return content.join("\n");
|
|
89
84
|
};
|
|
90
85
|
}
|
|
91
86
|
function virtualDirectives(app) {
|
|
92
87
|
const getDirectives = () => {
|
|
93
|
-
|
|
94
|
-
return directives;
|
|
88
|
+
return [...app.scanDirectives || []];
|
|
95
89
|
};
|
|
96
90
|
app.options.virtual ??= {};
|
|
97
91
|
app.options.virtual["#nitro-internal-virtual/server-directives"] = () => {
|
|
@@ -99,16 +93,14 @@ function virtualDirectives(app) {
|
|
|
99
93
|
const importsContent = [...imports.map(({ specifier, imports: imports$1, options }) => {
|
|
100
94
|
return genImport(specifier, imports$1, options);
|
|
101
95
|
})];
|
|
102
|
-
const data = imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ directive: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n");
|
|
103
96
|
const content = [
|
|
104
97
|
"export const directives = [",
|
|
105
|
-
|
|
98
|
+
imports.map(({ imports: imports$1 }) => imports$1.map((i) => `{ directive: ${i.as} }`).join(",\n")).filter(Boolean).join(",\n"),
|
|
106
99
|
"]",
|
|
107
100
|
""
|
|
108
101
|
];
|
|
109
102
|
content.unshift(...importsContent);
|
|
110
|
-
|
|
111
|
-
return code;
|
|
103
|
+
return content.join("\n");
|
|
112
104
|
};
|
|
113
105
|
}
|
|
114
106
|
function getGraphQLConfig(app) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as h31 from "h3";
|
|
2
2
|
|
|
3
3
|
//#region src/routes/apollo-server.d.ts
|
|
4
|
-
declare const _default:
|
|
4
|
+
declare const _default: h31.EventHandler<h31.EventHandlerRequest, Promise<any>>;
|
|
5
5
|
//#endregion
|
|
6
6
|
export { _default as default };
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { startServerAndCreateH3Handler } from "../utils/apollo.js";
|
|
2
1
|
import { consola as consola$1 } from "consola";
|
|
3
2
|
import defu from "defu";
|
|
4
3
|
import { parse } from "graphql";
|
|
@@ -12,14 +11,14 @@ import { ApolloServer } from "@apollo/server";
|
|
|
12
11
|
import { ApolloServerPluginLandingPageLocalDefault } from "@apollo/server/plugin/landingPage/default";
|
|
13
12
|
import { makeExecutableSchema } from "@graphql-tools/schema";
|
|
14
13
|
import { defineEventHandler } from "h3";
|
|
14
|
+
import { startServerAndCreateH3Handler } from "nitro-graphql/utils/apollo";
|
|
15
15
|
|
|
16
16
|
//#region src/routes/apollo-server.ts
|
|
17
17
|
let buildSubgraphSchema = null;
|
|
18
18
|
async function loadFederationSupport() {
|
|
19
19
|
if (buildSubgraphSchema !== null) return buildSubgraphSchema;
|
|
20
20
|
try {
|
|
21
|
-
|
|
22
|
-
buildSubgraphSchema = apolloSubgraph.buildSubgraphSchema;
|
|
21
|
+
buildSubgraphSchema = (await import("@apollo/subgraph")).buildSubgraphSchema;
|
|
23
22
|
} catch {
|
|
24
23
|
buildSubgraphSchema = false;
|
|
25
24
|
}
|
|
@@ -65,6 +64,7 @@ async function createMergedSchema() {
|
|
|
65
64
|
}
|
|
66
65
|
}
|
|
67
66
|
let apolloServer = null;
|
|
67
|
+
let serverStarted = false;
|
|
68
68
|
async function createApolloServer() {
|
|
69
69
|
if (!apolloServer) {
|
|
70
70
|
const schema = await createMergedSchema();
|
|
@@ -73,6 +73,10 @@ async function createApolloServer() {
|
|
|
73
73
|
introspection: true,
|
|
74
74
|
plugins: [ApolloServerPluginLandingPageLocalDefault({ embed: true })]
|
|
75
75
|
}, importedConfig));
|
|
76
|
+
if (!serverStarted) {
|
|
77
|
+
await apolloServer.start();
|
|
78
|
+
serverStarted = true;
|
|
79
|
+
}
|
|
76
80
|
}
|
|
77
81
|
return apolloServer;
|
|
78
82
|
}
|
|
@@ -80,8 +84,10 @@ let serverPromise = null;
|
|
|
80
84
|
var apollo_server_default = defineEventHandler(async (event) => {
|
|
81
85
|
if (!serverPromise) serverPromise = createApolloServer();
|
|
82
86
|
const server = await serverPromise;
|
|
83
|
-
|
|
84
|
-
|
|
87
|
+
return startServerAndCreateH3Handler(server, {
|
|
88
|
+
context: async () => ({ event }),
|
|
89
|
+
serverAlreadyStarted: true
|
|
90
|
+
})(event);
|
|
85
91
|
});
|
|
86
92
|
|
|
87
93
|
//#endregion
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as h30 from "h3";
|
|
2
2
|
|
|
3
3
|
//#region src/routes/graphql-yoga.d.ts
|
|
4
|
-
declare const _default:
|
|
4
|
+
declare const _default: h30.EventHandler<h30.EventHandlerRequest, Promise<Response>>;
|
|
5
5
|
//#endregion
|
|
6
6
|
export { _default as default };
|
|
@@ -16,8 +16,7 @@ let buildSubgraphSchema = null;
|
|
|
16
16
|
async function loadFederationSupport() {
|
|
17
17
|
if (buildSubgraphSchema !== null) return buildSubgraphSchema;
|
|
18
18
|
try {
|
|
19
|
-
|
|
20
|
-
buildSubgraphSchema = apolloSubgraph.buildSubgraphSchema;
|
|
19
|
+
buildSubgraphSchema = (await import("@apollo/subgraph")).buildSubgraphSchema;
|
|
21
20
|
} catch {
|
|
22
21
|
buildSubgraphSchema = false;
|
|
23
22
|
}
|
package/dist/routes/health.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import * as
|
|
1
|
+
import * as h33 from "h3";
|
|
2
2
|
|
|
3
3
|
//#region src/routes/health.d.ts
|
|
4
|
-
declare const _default:
|
|
4
|
+
declare const _default: h33.EventHandler<h33.EventHandlerRequest, Promise<{
|
|
5
5
|
status: string;
|
|
6
6
|
message: string;
|
|
7
7
|
timestamp: string;
|
package/dist/utils/apollo.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ interface H3ContextFunctionArgument {
|
|
|
10
10
|
interface H3HandlerOptions<TContext extends BaseContext> {
|
|
11
11
|
context?: ContextFunction<[H3ContextFunctionArgument], TContext>;
|
|
12
12
|
websocket?: Partial<Hooks>;
|
|
13
|
+
serverAlreadyStarted?: boolean;
|
|
13
14
|
}
|
|
14
15
|
declare function startServerAndCreateH3Handler(server: ApolloServer<BaseContext> | (() => ApolloServer<BaseContext>), options?: H3HandlerOptions<BaseContext>): EventHandler;
|
|
15
16
|
declare function startServerAndCreateH3Handler<TContext extends BaseContext>(server: ApolloServer<TContext> | (() => ApolloServer<TContext>), options: WithRequired<H3HandlerOptions<TContext>, 'context'>): EventHandler;
|
package/dist/utils/apollo.js
CHANGED
|
@@ -8,7 +8,7 @@ function startServerAndCreateH3Handler(server, options) {
|
|
|
8
8
|
return eventHandler({
|
|
9
9
|
handler: async (event) => {
|
|
10
10
|
const apolloServer = typeof server === "function" ? server() : server;
|
|
11
|
-
apolloServer.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
|
|
11
|
+
if (!options?.serverAlreadyStarted) apolloServer.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests();
|
|
12
12
|
if (isMethod(event, "OPTIONS")) return null;
|
|
13
13
|
try {
|
|
14
14
|
const graphqlRequest = await toGraphqlRequest(event);
|
|
@@ -49,13 +49,12 @@ function normalizeQueryString(url) {
|
|
|
49
49
|
return url.split("?")[1] || "";
|
|
50
50
|
}
|
|
51
51
|
async function normalizeBody(event) {
|
|
52
|
-
|
|
52
|
+
if (isMethod(event, [
|
|
53
53
|
"PATCH",
|
|
54
54
|
"POST",
|
|
55
55
|
"PUT",
|
|
56
56
|
"DELETE"
|
|
57
|
-
];
|
|
58
|
-
if (isMethod(event, PayloadMethods)) return await readBody(event);
|
|
57
|
+
])) return await readBody(event);
|
|
59
58
|
}
|
|
60
59
|
|
|
61
60
|
//#endregion
|
|
@@ -29,8 +29,7 @@ function pluginContent(_schema, _documents, _config, _info) {
|
|
|
29
29
|
};
|
|
30
30
|
}
|
|
31
31
|
async function graphQLLoadSchemaSync(schemaPointers, data = {}) {
|
|
32
|
-
const
|
|
33
|
-
const filteredPointers = [...pointers, "!**/vfs/**"];
|
|
32
|
+
const filteredPointers = [...Array.isArray(schemaPointers) ? schemaPointers : [schemaPointers], "!**/vfs/**"];
|
|
34
33
|
let result;
|
|
35
34
|
try {
|
|
36
35
|
result = loadSchemaSync(filteredPointers, {
|
|
@@ -58,8 +57,7 @@ async function loadExternalSchema(service, buildDir) {
|
|
|
58
57
|
const defaultPath = resolve(buildDir, "graphql", "schemas", `${service.name}.graphql`);
|
|
59
58
|
const schemaFilePath = service.downloadPath ? resolve(service.downloadPath) : defaultPath;
|
|
60
59
|
if (existsSync(schemaFilePath)) try {
|
|
61
|
-
|
|
62
|
-
return result$1;
|
|
60
|
+
return loadSchemaSync([schemaFilePath], { loaders: [new GraphQLFileLoader()] });
|
|
63
61
|
} catch {
|
|
64
62
|
consola$1.warn(`[graphql:${service.name}] Cached schema invalid, loading from source`);
|
|
65
63
|
}
|
|
@@ -70,14 +68,13 @@ async function loadExternalSchema(service, buildDir) {
|
|
|
70
68
|
if (hasLocalFiles) loaders.push(new GraphQLFileLoader());
|
|
71
69
|
if (hasUrls) loaders.push(new UrlLoader());
|
|
72
70
|
if (loaders.length === 0) throw new Error("No appropriate loaders found for schema sources");
|
|
73
|
-
|
|
71
|
+
return loadSchemaSync(schemas, {
|
|
74
72
|
loaders,
|
|
75
73
|
...Object.keys(headers).length > 0 && { headers }
|
|
76
74
|
});
|
|
77
|
-
return result;
|
|
78
75
|
} catch (error) {
|
|
79
76
|
consola$1.error(`[graphql:${service.name}] Failed to load external schema:`, error);
|
|
80
|
-
return
|
|
77
|
+
return;
|
|
81
78
|
}
|
|
82
79
|
}
|
|
83
80
|
/**
|
|
@@ -91,7 +88,7 @@ function isUrl(path) {
|
|
|
91
88
|
*/
|
|
92
89
|
async function downloadAndSaveSchema(service, buildDir) {
|
|
93
90
|
const downloadMode = service.downloadSchema;
|
|
94
|
-
if (!downloadMode || downloadMode === "manual") return
|
|
91
|
+
if (!downloadMode || downloadMode === "manual") return;
|
|
95
92
|
const defaultPath = resolve(buildDir, "graphql", "schemas", `${service.name}.graphql`);
|
|
96
93
|
const schemaFilePath = service.downloadPath ? resolve(service.downloadPath) : defaultPath;
|
|
97
94
|
try {
|
|
@@ -162,13 +159,12 @@ async function downloadAndSaveSchema(service, buildDir) {
|
|
|
162
159
|
return schemaFilePath;
|
|
163
160
|
} catch (error) {
|
|
164
161
|
consola$1.error(`[graphql:${service.name}] Failed to download/copy schema:`, error);
|
|
165
|
-
return
|
|
162
|
+
return;
|
|
166
163
|
}
|
|
167
164
|
}
|
|
168
165
|
async function loadGraphQLDocuments(patterns) {
|
|
169
166
|
try {
|
|
170
|
-
|
|
171
|
-
return result;
|
|
167
|
+
return await loadDocuments(patterns, { loaders: [new GraphQLFileLoader()] });
|
|
172
168
|
} catch (e) {
|
|
173
169
|
if ((e.message || "").includes("Unable to find any GraphQL type definitions for the following pointers:")) return [];
|
|
174
170
|
else throw e;
|
|
@@ -208,8 +204,8 @@ async function generateClientTypes(schema, docs, config = {}, sdkConfig = {}, ou
|
|
|
208
204
|
const mergedConfig = defu$1(defaultConfig, config);
|
|
209
205
|
const mergedSdkConfig = defu$1(mergedConfig, sdkConfig);
|
|
210
206
|
try {
|
|
211
|
-
if (docs.length === 0) {
|
|
212
|
-
|
|
207
|
+
if (docs.length === 0) return {
|
|
208
|
+
types: await codegen({
|
|
213
209
|
filename: outputPath || "client-types.generated.ts",
|
|
214
210
|
schema: parse(printSchemaWithDirectives(schema)),
|
|
215
211
|
documents: [],
|
|
@@ -219,8 +215,8 @@ async function generateClientTypes(schema, docs, config = {}, sdkConfig = {}, ou
|
|
|
219
215
|
pluginContent: { plugin: pluginContent },
|
|
220
216
|
typescript: { plugin }
|
|
221
217
|
}
|
|
222
|
-
})
|
|
223
|
-
|
|
218
|
+
}),
|
|
219
|
+
sdk: `// THIS FILE IS GENERATED, DO NOT EDIT!
|
|
224
220
|
/* eslint-disable eslint-comments/no-unlimited-disable */
|
|
225
221
|
/* tslint:disable */
|
|
226
222
|
/* eslint-disable */
|
|
@@ -244,12 +240,8 @@ export function getSdk(requester: Requester): Sdk {
|
|
|
244
240
|
}
|
|
245
241
|
}
|
|
246
242
|
}
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
types: output$1,
|
|
250
|
-
sdk: sdkContent$1
|
|
251
|
-
};
|
|
252
|
-
}
|
|
243
|
+
`
|
|
244
|
+
};
|
|
253
245
|
const output = await codegen({
|
|
254
246
|
filename: outputPath || "client-types.generated.ts",
|
|
255
247
|
schema: parse(printSchemaWithDirectives(schema)),
|
|
@@ -279,13 +271,12 @@ export function getSdk(requester: Requester): Sdk {
|
|
|
279
271
|
typescriptGenericSdk: { plugin: plugin$1 }
|
|
280
272
|
}
|
|
281
273
|
});
|
|
282
|
-
const
|
|
274
|
+
const sdkContent = (await Promise.all(sdkOutput.map(async (config$1) => {
|
|
283
275
|
return {
|
|
284
276
|
file: config$1.filename,
|
|
285
277
|
content: await codegen(config$1)
|
|
286
278
|
};
|
|
287
|
-
}));
|
|
288
|
-
const sdkContent = results[0]?.content || "";
|
|
279
|
+
})))[0]?.content || "";
|
|
289
280
|
return {
|
|
290
281
|
types: output,
|
|
291
282
|
sdk: sdkContent
|
|
@@ -109,14 +109,12 @@ var DirectiveParser = class {
|
|
|
109
109
|
*/
|
|
110
110
|
extractStringLiteral(node) {
|
|
111
111
|
if (node?.type === "Literal" && typeof node.value === "string") return node.value;
|
|
112
|
-
return void 0;
|
|
113
112
|
}
|
|
114
113
|
/**
|
|
115
114
|
* Extract boolean literal value
|
|
116
115
|
*/
|
|
117
116
|
extractBooleanLiteral(node) {
|
|
118
117
|
if (node?.type === "Literal" && typeof node.value === "boolean") return node.value;
|
|
119
|
-
return void 0;
|
|
120
118
|
}
|
|
121
119
|
/**
|
|
122
120
|
* Extract array of strings
|
|
@@ -169,7 +167,6 @@ var DirectiveParser = class {
|
|
|
169
167
|
*/
|
|
170
168
|
extractLiteralValue(node) {
|
|
171
169
|
if (node?.type === "Literal") return node.value;
|
|
172
|
-
return void 0;
|
|
173
170
|
}
|
|
174
171
|
};
|
|
175
172
|
/**
|
|
@@ -177,15 +174,12 @@ var DirectiveParser = class {
|
|
|
177
174
|
*/
|
|
178
175
|
function generateDirectiveSchema(directive) {
|
|
179
176
|
let args = "";
|
|
180
|
-
if (directive.args && Object.keys(directive.args).length > 0) {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
});
|
|
187
|
-
args = `(${argDefs.join(", ")})`;
|
|
188
|
-
}
|
|
177
|
+
if (directive.args && Object.keys(directive.args).length > 0) args = `(${Object.entries(directive.args).map(([name, arg]) => {
|
|
178
|
+
let defaultValue = "";
|
|
179
|
+
if (arg.defaultValue !== void 0) if (typeof arg.defaultValue === "string") defaultValue = ` = "${arg.defaultValue}"`;
|
|
180
|
+
else defaultValue = ` = ${arg.defaultValue}`;
|
|
181
|
+
return `${name}: ${arg.type}${defaultValue}`;
|
|
182
|
+
}).join(", ")})`;
|
|
189
183
|
const locations = directive.locations.join(" | ");
|
|
190
184
|
return `directive @${directive.name}${args} on ${locations}`;
|
|
191
185
|
}
|
|
@@ -220,10 +214,7 @@ ${directiveSchemas.join("\n\n")}`;
|
|
|
220
214
|
const targetDir = dirname(directivesPath);
|
|
221
215
|
if (!existsSync(targetDir)) mkdirSync(targetDir, { recursive: true });
|
|
222
216
|
let shouldWrite = true;
|
|
223
|
-
if (existsSync(directivesPath))
|
|
224
|
-
const existingContent = readFileSync(directivesPath, "utf-8");
|
|
225
|
-
shouldWrite = existingContent !== content;
|
|
226
|
-
}
|
|
217
|
+
if (existsSync(directivesPath)) shouldWrite = readFileSync(directivesPath, "utf-8") !== content;
|
|
227
218
|
if (shouldWrite) writeFileSync(directivesPath, content, "utf-8");
|
|
228
219
|
if (!nitro.scanSchemas.includes(directivesPath)) nitro.scanSchemas.push(directivesPath);
|
|
229
220
|
}
|
package/dist/utils/index.js
CHANGED
|
@@ -44,8 +44,7 @@ function relativeWithDot(from, to) {
|
|
|
44
44
|
return RELATIVE_RE.test(rel) ? rel : `./${rel}`;
|
|
45
45
|
}
|
|
46
46
|
async function scanGraphql(nitro) {
|
|
47
|
-
|
|
48
|
-
return files.map((f) => f.fullPath);
|
|
47
|
+
return (await scanFiles(nitro, "graphql")).map((f) => f.fullPath);
|
|
49
48
|
}
|
|
50
49
|
async function scanResolvers(nitro) {
|
|
51
50
|
const files = await scanFiles(nitro, "graphql", "**/*.resolver.{ts,js}");
|
|
@@ -123,12 +122,10 @@ async function scanDirectives(nitro) {
|
|
|
123
122
|
return exportName;
|
|
124
123
|
}
|
|
125
124
|
async function scanTypeDefs(nitro) {
|
|
126
|
-
|
|
127
|
-
return files.map((f) => f.fullPath);
|
|
125
|
+
return (await scanFiles(nitro, "graphql", "**/*.typedef.{ts,js}")).map((f) => f.fullPath);
|
|
128
126
|
}
|
|
129
127
|
async function scanSchemas(nitro) {
|
|
130
|
-
|
|
131
|
-
return files.map((f) => f.fullPath);
|
|
128
|
+
return (await scanFiles(nitro, "graphql", "**/*.graphql")).map((f) => f.fullPath);
|
|
132
129
|
}
|
|
133
130
|
async function scanDocs(nitro) {
|
|
134
131
|
const files = await scanDir(nitro, nitro.options.rootDir, nitro.graphql.dir.client, "**/*.graphql");
|
|
@@ -141,14 +138,12 @@ async function scanDocs(nitro) {
|
|
|
141
138
|
seenPaths.add(file.fullPath);
|
|
142
139
|
return true;
|
|
143
140
|
});
|
|
144
|
-
const
|
|
145
|
-
const externalPatterns = externalServices.flatMap((service) => service.documents || []);
|
|
141
|
+
const externalPatterns = (nitro.options.graphql?.externalServices || []).flatMap((service) => service.documents || []);
|
|
146
142
|
return allFiles.filter((f) => !f.path.startsWith("external/")).filter((f) => {
|
|
147
143
|
const relativePath = f.path;
|
|
148
144
|
for (const pattern of externalPatterns) {
|
|
149
145
|
const clientDirPattern = `${nitro.graphql.dir.client}/`;
|
|
150
|
-
const
|
|
151
|
-
const patternDir = cleanPattern.split("/")[0];
|
|
146
|
+
const patternDir = pattern.replace(/* @__PURE__ */ new RegExp(`^${clientDirPattern.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}`), "").split("/")[0];
|
|
152
147
|
const fileDir = relativePath.split("/")[0];
|
|
153
148
|
if (patternDir === fileDir) return false;
|
|
154
149
|
}
|
|
@@ -213,7 +208,7 @@ async function scanFiles(nitro, name, globPattern = GLOB_SCAN_PATTERN) {
|
|
|
213
208
|
});
|
|
214
209
|
}
|
|
215
210
|
async function scanDir(nitro, dir, name, globPattern = GLOB_SCAN_PATTERN) {
|
|
216
|
-
|
|
211
|
+
return (await glob(join(name, globPattern), {
|
|
217
212
|
cwd: dir,
|
|
218
213
|
dot: true,
|
|
219
214
|
ignore: nitro.options.ignore,
|
|
@@ -224,8 +219,7 @@ async function scanDir(nitro, dir, name, globPattern = GLOB_SCAN_PATTERN) {
|
|
|
224
219
|
return [];
|
|
225
220
|
}
|
|
226
221
|
throw error;
|
|
227
|
-
})
|
|
228
|
-
return fileNames.map((fullPath) => {
|
|
222
|
+
})).map((fullPath) => {
|
|
229
223
|
return {
|
|
230
224
|
fullPath,
|
|
231
225
|
path: relative(join(dir, name), fullPath)
|
|
@@ -45,7 +45,7 @@ async function generateTypes(selectFremework, schema, config = {}, outputPath) {
|
|
|
45
45
|
...config.federation?.enabled && { federation: true }
|
|
46
46
|
};
|
|
47
47
|
const mergedConfig = defu$1(defaultConfig, config.codegen?.server);
|
|
48
|
-
|
|
48
|
+
return await codegen({
|
|
49
49
|
filename: outputPath || "types.generated.ts",
|
|
50
50
|
schema: parse(printSchemaWithDirectives(schema)),
|
|
51
51
|
documents: [],
|
|
@@ -131,7 +131,6 @@ type ResolverReturnTypeObject<T extends object> =
|
|
|
131
131
|
consola.withTag("graphql").error("Error generating types:", e);
|
|
132
132
|
return "";
|
|
133
133
|
});
|
|
134
|
-
return output;
|
|
135
134
|
}
|
|
136
135
|
|
|
137
136
|
//#endregion
|
|
@@ -31,8 +31,7 @@ function generateNuxtOfetchClient(clientDir, serviceName = "default") {
|
|
|
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 (!existsSync(ofetchPath))
|
|
35
|
-
const ofetchContent = `// This file is auto-generated once by nitro-graphql for quick start
|
|
34
|
+
if (!existsSync(ofetchPath)) writeFileSync(ofetchPath, `// This file is auto-generated once by nitro-graphql for quick start
|
|
36
35
|
// You can modify this file according to your needs
|
|
37
36
|
import type { Requester } from './sdk'
|
|
38
37
|
import { getSdk } from './sdk'
|
|
@@ -54,9 +53,7 @@ export function createGraphQLClient(endpoint: string): Requester {
|
|
|
54
53
|
}
|
|
55
54
|
}
|
|
56
55
|
|
|
57
|
-
export const $sdk = getSdk(createGraphQLClient('/api/graphql'))
|
|
58
|
-
writeFileSync(ofetchPath, ofetchContent, "utf-8");
|
|
59
|
-
}
|
|
56
|
+
export const $sdk = getSdk(createGraphQLClient('/api/graphql'))`, "utf-8");
|
|
60
57
|
}
|
|
61
58
|
function generateExternalOfetchClient(clientDir, serviceName, endpoint) {
|
|
62
59
|
const serviceDir = resolve(clientDir, serviceName);
|
|
@@ -127,8 +124,7 @@ function validateNoDuplicateTypes(schemas, schemaStrings) {
|
|
|
127
124
|
schemaStrings.forEach((schemaContent, index) => {
|
|
128
125
|
const fileName = basename(schemas[index]);
|
|
129
126
|
try {
|
|
130
|
-
|
|
131
|
-
document.definitions.forEach((def) => {
|
|
127
|
+
parse(schemaContent).definitions.forEach((def) => {
|
|
132
128
|
if (def.kind === "ObjectTypeDefinition" || def.kind === "InterfaceTypeDefinition" || def.kind === "UnionTypeDefinition" || def.kind === "EnumTypeDefinition" || def.kind === "InputObjectTypeDefinition" || def.kind === "ScalarTypeDefinition") {
|
|
133
129
|
const typeName = def.name.value;
|
|
134
130
|
if ([
|
|
@@ -148,8 +144,7 @@ function validateNoDuplicateTypes(schemas, schemaStrings) {
|
|
|
148
144
|
const content = schemaStrings[i];
|
|
149
145
|
if (!content) return false;
|
|
150
146
|
try {
|
|
151
|
-
|
|
152
|
-
return doc.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);
|
|
147
|
+
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);
|
|
153
148
|
} catch {
|
|
154
149
|
return false;
|
|
155
150
|
}
|
|
@@ -189,10 +184,8 @@ async function serverTypeGeneration(app) {
|
|
|
189
184
|
consola.info("No GraphQL definitions found for server type generation.");
|
|
190
185
|
return;
|
|
191
186
|
}
|
|
192
|
-
const
|
|
193
|
-
|
|
194
|
-
const isValid = validateNoDuplicateTypes(schemas, schemaStrings);
|
|
195
|
-
if (!isValid) return;
|
|
187
|
+
const schemaStrings = loadFilesSync(schemas).map((schema$1) => typeof schema$1 === "string" ? schema$1 : schema$1.loc?.source?.body || "").filter(Boolean);
|
|
188
|
+
if (!validateNoDuplicateTypes(schemas, schemaStrings)) return;
|
|
196
189
|
const federationEnabled = app.options.graphql?.federation?.enabled === true;
|
|
197
190
|
const mergedSchemas = mergeTypeDefs([schemaStrings.join("\n\n")], {
|
|
198
191
|
throwOnConflict: true,
|
|
@@ -214,8 +207,7 @@ async function serverTypeGeneration(app) {
|
|
|
214
207
|
}
|
|
215
208
|
async function clientTypeGeneration(nitro) {
|
|
216
209
|
try {
|
|
217
|
-
|
|
218
|
-
if (hasServerSchema) await generateMainClientTypes(nitro);
|
|
210
|
+
if (nitro.scanSchemas && nitro.scanSchemas.length > 0) await generateMainClientTypes(nitro);
|
|
219
211
|
if (nitro.options.graphql?.externalServices?.length) await generateExternalServicesTypes(nitro);
|
|
220
212
|
} catch (error) {
|
|
221
213
|
consola.error("Client schema generation error:", error);
|
|
@@ -259,8 +251,7 @@ async function generateMainClientTypes(nitro) {
|
|
|
259
251
|
return;
|
|
260
252
|
}
|
|
261
253
|
const graphqlString = readFileSync(schemaFilePath, "utf-8");
|
|
262
|
-
const
|
|
263
|
-
const schema = federationEnabled ? buildSubgraphSchema([{ typeDefs: parse(graphqlString) }]) : buildSchema(graphqlString);
|
|
254
|
+
const schema = nitro.options.graphql?.federation?.enabled === true ? buildSubgraphSchema([{ typeDefs: parse(graphqlString) }]) : buildSchema(graphqlString);
|
|
264
255
|
const types = await generateClientTypes(schema, loadDocs, nitro.options.graphql?.codegen?.client ?? {}, nitro.options.graphql?.codegen?.clientSDK ?? {});
|
|
265
256
|
if (types === false) return;
|
|
266
257
|
const clientTypesPath = resolve(nitro.options.buildDir, "types", "nitro-graphql-client.d.ts");
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nitro-graphql",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.4.
|
|
4
|
+
"version": "1.4.4",
|
|
5
5
|
"description": "GraphQL integration for Nitro",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"sideEffects": false,
|
|
@@ -40,6 +40,10 @@
|
|
|
40
40
|
"types": "./dist/utils/define.d.ts",
|
|
41
41
|
"import": "./dist/utils/define.js"
|
|
42
42
|
},
|
|
43
|
+
"./utils/apollo": {
|
|
44
|
+
"types": "./dist/utils/apollo.d.ts",
|
|
45
|
+
"import": "./dist/utils/apollo.js"
|
|
46
|
+
},
|
|
43
47
|
"./internal": {
|
|
44
48
|
"types": "./dist/internal/index.d.ts",
|
|
45
49
|
"import": "./dist/internal/index.js"
|
|
@@ -56,7 +60,6 @@
|
|
|
56
60
|
],
|
|
57
61
|
"peerDependencies": {
|
|
58
62
|
"@apollo/server": "^5.0.0",
|
|
59
|
-
"@apollo/subgraph": "^2.0.0",
|
|
60
63
|
"@apollo/utils.withrequired": "^3.0.0",
|
|
61
64
|
"@as-integrations/h3": "^2.0.0",
|
|
62
65
|
"graphql": "^16.11.0",
|
|
@@ -67,9 +70,6 @@
|
|
|
67
70
|
"@apollo/server": {
|
|
68
71
|
"optional": true
|
|
69
72
|
},
|
|
70
|
-
"@apollo/subgraph": {
|
|
71
|
-
"optional": true
|
|
72
|
-
},
|
|
73
73
|
"@apollo/utils.withrequired": {
|
|
74
74
|
"optional": true
|
|
75
75
|
},
|
|
@@ -78,18 +78,19 @@
|
|
|
78
78
|
}
|
|
79
79
|
},
|
|
80
80
|
"dependencies": {
|
|
81
|
-
"@
|
|
81
|
+
"@apollo/subgraph": "^2.11.2",
|
|
82
|
+
"@graphql-codegen/core": "^5.0.0",
|
|
82
83
|
"@graphql-codegen/import-types-preset": "^3.0.1",
|
|
83
|
-
"@graphql-codegen/typescript": "^
|
|
84
|
+
"@graphql-codegen/typescript": "^5.0.1",
|
|
84
85
|
"@graphql-codegen/typescript-generic-sdk": "^4.0.2",
|
|
85
|
-
"@graphql-codegen/typescript-operations": "^
|
|
86
|
-
"@graphql-codegen/typescript-resolvers": "^
|
|
87
|
-
"@graphql-tools/graphql-file-loader": "^8.1.
|
|
86
|
+
"@graphql-codegen/typescript-operations": "^5.0.1",
|
|
87
|
+
"@graphql-codegen/typescript-resolvers": "^5.0.1",
|
|
88
|
+
"@graphql-tools/graphql-file-loader": "^8.1.2",
|
|
88
89
|
"@graphql-tools/load": "^8.1.2",
|
|
89
90
|
"@graphql-tools/load-files": "^7.0.1",
|
|
90
91
|
"@graphql-tools/merge": "^9.1.1",
|
|
91
92
|
"@graphql-tools/schema": "^10.0.25",
|
|
92
|
-
"@graphql-tools/url-loader": "^
|
|
93
|
+
"@graphql-tools/url-loader": "^9.0.0",
|
|
93
94
|
"@graphql-tools/utils": "^10.9.1",
|
|
94
95
|
"chokidar": "^4.0.3",
|
|
95
96
|
"consola": "^3.4.2",
|
|
@@ -98,26 +99,25 @@
|
|
|
98
99
|
"graphql-scalars": "^1.24.2",
|
|
99
100
|
"knitwork": "^1.2.0",
|
|
100
101
|
"ohash": "^2.0.11",
|
|
101
|
-
"oxc-parser": "^0.
|
|
102
|
+
"oxc-parser": "^0.93.0",
|
|
102
103
|
"pathe": "^2.0.3",
|
|
103
|
-
"tinyglobby": "^0.2.
|
|
104
|
+
"tinyglobby": "^0.2.15"
|
|
104
105
|
},
|
|
105
106
|
"devDependencies": {
|
|
106
|
-
"@antfu/eslint-config": "^5.
|
|
107
|
-
"@
|
|
108
|
-
"@nuxt/
|
|
109
|
-
"@
|
|
110
|
-
"@types/node": "^22.18.1",
|
|
107
|
+
"@antfu/eslint-config": "^5.4.1",
|
|
108
|
+
"@nuxt/kit": "^4.1.2",
|
|
109
|
+
"@nuxt/schema": "^4.1.2",
|
|
110
|
+
"@types/node": "^24.6.2",
|
|
111
111
|
"bumpp": "^10.2.3",
|
|
112
112
|
"changelogen": "^0.6.2",
|
|
113
113
|
"crossws": "0.3.5",
|
|
114
|
-
"eslint": "^9.
|
|
115
|
-
"graphql": "
|
|
116
|
-
"graphql-yoga": "^5.
|
|
114
|
+
"eslint": "^9.37.0",
|
|
115
|
+
"graphql": "16.11.0",
|
|
116
|
+
"graphql-yoga": "^5.16.0",
|
|
117
117
|
"h3": "1.15.3",
|
|
118
|
-
"nitropack": "^2.12.
|
|
119
|
-
"tsdown": "^0.
|
|
120
|
-
"typescript": "^5.9.
|
|
118
|
+
"nitropack": "^2.12.6",
|
|
119
|
+
"tsdown": "^0.15.6",
|
|
120
|
+
"typescript": "^5.9.3"
|
|
121
121
|
},
|
|
122
122
|
"resolutions": {
|
|
123
123
|
"nitro-graphql": "link:."
|