nitro-graphql 1.4.0 → 1.4.2
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 +183 -1321
- package/dist/index.js +6 -3
- package/dist/utils/directive-parser.js +4 -2
- package/dist/utils/type-generation.js +5 -26
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -60,9 +60,12 @@ var src_default = defineNitroModule({
|
|
|
60
60
|
const watchDirs = [];
|
|
61
61
|
switch (nitro.options.framework.name) {
|
|
62
62
|
case "nuxt": {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
63
|
+
if (!nitro.graphql.clientDir) {
|
|
64
|
+
nitro.graphql.clientDir = resolve(nitro.options.rootDir, "app", "graphql");
|
|
65
|
+
nitro.graphql.dir.client = "app/graphql";
|
|
66
|
+
}
|
|
67
|
+
if (!nitro.options.graphql?.serverDir) nitro.graphql.serverDir = resolve(nitro.options.rootDir, "server", "graphql");
|
|
68
|
+
watchDirs.push(nitro.graphql.clientDir);
|
|
66
69
|
const layerServerDirs = getLayerServerDirectories(nitro);
|
|
67
70
|
const layerAppDirs = getLayerAppDirectories(nitro);
|
|
68
71
|
for (const layerServerDir of layerServerDirs) watchDirs.push(join(layerServerDir, "graphql"));
|
|
@@ -194,9 +194,9 @@ function generateDirectiveSchema(directive) {
|
|
|
194
194
|
*/
|
|
195
195
|
async function generateDirectiveSchemas(nitro, directives) {
|
|
196
196
|
if (directives.length === 0) return;
|
|
197
|
-
const { existsSync, readFileSync, writeFileSync } = await import("node:fs");
|
|
197
|
+
const { existsSync, readFileSync, writeFileSync, mkdirSync } = await import("node:fs");
|
|
198
198
|
const { readFile } = await import("node:fs/promises");
|
|
199
|
-
const { resolve } = await import("pathe");
|
|
199
|
+
const { resolve, dirname } = await import("pathe");
|
|
200
200
|
const directiveSchemas = [];
|
|
201
201
|
const seenDirectives = /* @__PURE__ */ new Set();
|
|
202
202
|
const parser = new DirectiveParser();
|
|
@@ -217,6 +217,8 @@ async function generateDirectiveSchemas(nitro, directives) {
|
|
|
217
217
|
# To define custom directives, create .directive.ts files using defineDirective()
|
|
218
218
|
|
|
219
219
|
${directiveSchemas.join("\n\n")}`;
|
|
220
|
+
const targetDir = dirname(directivesPath);
|
|
221
|
+
if (!existsSync(targetDir)) mkdirSync(targetDir, { recursive: true });
|
|
220
222
|
let shouldWrite = true;
|
|
221
223
|
if (existsSync(directivesPath)) {
|
|
222
224
|
const existingContent = readFileSync(directivesPath, "utf-8");
|
|
@@ -4,6 +4,7 @@ import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
|
4
4
|
import consola from "consola";
|
|
5
5
|
import { basename, dirname, join, resolve } from "pathe";
|
|
6
6
|
import { buildSchema, parse } from "graphql";
|
|
7
|
+
import { buildSubgraphSchema } from "@apollo/subgraph";
|
|
7
8
|
import { loadFilesSync } from "@graphql-tools/load-files";
|
|
8
9
|
import { mergeTypeDefs } from "@graphql-tools/merge";
|
|
9
10
|
import { printSchemaWithDirectives } from "@graphql-tools/utils";
|
|
@@ -192,36 +193,13 @@ async function serverTypeGeneration(app) {
|
|
|
192
193
|
const schemaStrings = loadSchemas.map((schema$1) => typeof schema$1 === "string" ? schema$1 : schema$1.loc?.source?.body || "").filter(Boolean);
|
|
193
194
|
const isValid = validateNoDuplicateTypes(schemas, schemaStrings);
|
|
194
195
|
if (!isValid) return;
|
|
195
|
-
const mergedSchemasString = schemaStrings.join("\n\n");
|
|
196
196
|
const federationEnabled = app.options.graphql?.federation?.enabled === true;
|
|
197
|
-
|
|
198
|
-
if (federationEnabled) {
|
|
199
|
-
const federationDirectives = `
|
|
200
|
-
directive @key(fields: String!) on OBJECT | INTERFACE
|
|
201
|
-
directive @requires(fields: String!) on FIELD_DEFINITION
|
|
202
|
-
directive @provides(fields: String!) on FIELD_DEFINITION
|
|
203
|
-
directive @external on FIELD_DEFINITION | OBJECT
|
|
204
|
-
directive @tag(name: String!) on FIELD_DEFINITION | OBJECT | INTERFACE | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
|
|
205
|
-
directive @extends on OBJECT | INTERFACE
|
|
206
|
-
directive @shareable on FIELD_DEFINITION | OBJECT
|
|
207
|
-
directive @inaccessible on FIELD_DEFINITION | OBJECT | INTERFACE | UNION | ARGUMENT_DEFINITION | SCALAR | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION
|
|
208
|
-
directive @override(from: String!) on FIELD_DEFINITION
|
|
209
|
-
directive @composeDirective(name: String!) on SCHEMA
|
|
210
|
-
directive @link(url: String!, as: String, for: Purpose, import: [String!]) on SCHEMA
|
|
211
|
-
|
|
212
|
-
enum Purpose {
|
|
213
|
-
SECURITY
|
|
214
|
-
EXECUTION
|
|
215
|
-
}
|
|
216
|
-
`;
|
|
217
|
-
schemaWithDirectives = `${federationDirectives}\n\n${mergedSchemasString}`;
|
|
218
|
-
}
|
|
219
|
-
const mergedSchemas = mergeTypeDefs([schemaWithDirectives], {
|
|
197
|
+
const mergedSchemas = mergeTypeDefs([schemaStrings.join("\n\n")], {
|
|
220
198
|
throwOnConflict: true,
|
|
221
199
|
commentDescriptions: true,
|
|
222
200
|
sort: true
|
|
223
201
|
});
|
|
224
|
-
const schema = buildSchema(mergedSchemas);
|
|
202
|
+
const schema = federationEnabled ? buildSubgraphSchema([{ typeDefs: parse(mergedSchemas) }]) : buildSchema(mergedSchemas);
|
|
225
203
|
const data = await generateTypes(app.options.graphql?.framework || "graphql-yoga", schema, app.options.graphql ?? {});
|
|
226
204
|
const printSchema = printSchemaWithDirectives(schema);
|
|
227
205
|
const schemaPath = resolve(app.graphql.buildDir, "schema.graphql");
|
|
@@ -281,7 +259,8 @@ async function generateMainClientTypes(nitro) {
|
|
|
281
259
|
return;
|
|
282
260
|
}
|
|
283
261
|
const graphqlString = readFileSync(schemaFilePath, "utf-8");
|
|
284
|
-
const
|
|
262
|
+
const federationEnabled = nitro.options.graphql?.federation?.enabled === true;
|
|
263
|
+
const schema = federationEnabled ? buildSubgraphSchema([{ typeDefs: parse(graphqlString) }]) : buildSchema(graphqlString);
|
|
285
264
|
const types = await generateClientTypes(schema, loadDocs, nitro.options.graphql?.codegen?.client ?? {}, nitro.options.graphql?.codegen?.clientSDK ?? {});
|
|
286
265
|
if (types === false) return;
|
|
287
266
|
const clientTypesPath = resolve(nitro.options.buildDir, "types", "nitro-graphql-client.d.ts");
|