nuxt-graphql-middleware 5.0.0-alpha.1 → 5.0.0-alpha.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/client/200.html +11 -0
- package/dist/client/404.html +11 -0
- package/dist/client/_nuxt/BS583yk8.js +25 -0
- package/dist/client/_nuxt/CZ2Qwgdk.js +2 -0
- package/dist/client/_nuxt/DpxjPVZy.js +1 -0
- package/dist/client/_nuxt/GOrnHr4p.js +1 -0
- package/dist/client/_nuxt/builds/latest.json +1 -0
- package/dist/client/_nuxt/builds/meta/106a09af-649a-473b-b0c7-0e4ce5709429.json +1 -0
- package/dist/client/_nuxt/entry.AjgXSF89.css +1 -0
- package/dist/client/_nuxt/error-404.BJkSn6RI.css +1 -0
- package/dist/client/_nuxt/error-500.TOCKLquH.css +1 -0
- package/dist/client/_nuxt/exxdaCPN.js +1 -0
- package/dist/client/_nuxt/index.D19Q16VT.css +1 -0
- package/dist/client/index.html +11 -0
- package/dist/module.d.mts +6 -0
- package/dist/module.d.ts +6 -0
- package/dist/module.json +1 -1
- package/dist/module.mjs +213 -81
- package/dist/runtime/composables/nuxtApp.js +19 -0
- package/dist/runtime/settings/index.d.ts +5 -1
- package/dist/runtime/settings/index.js +2 -1
- package/package.json +3 -8
package/dist/module.mjs
CHANGED
|
@@ -6,18 +6,19 @@ import { useLogger, resolveFiles, defineNuxtModule, resolveAlias, createResolver
|
|
|
6
6
|
import { onDevToolsInitialized, extendServerRpc } from '@nuxt/devtools-kit';
|
|
7
7
|
import { existsSync } from 'fs';
|
|
8
8
|
import { GraphqlMiddlewareTemplate } from '../dist/runtime/settings/index.js';
|
|
9
|
-
import
|
|
9
|
+
import fs from 'node:fs/promises';
|
|
10
|
+
import { existsSync as existsSync$1, promises } from 'node:fs';
|
|
10
11
|
import { generate } from '@graphql-codegen/cli';
|
|
11
12
|
import * as PluginSchemaAst from '@graphql-codegen/schema-ast';
|
|
12
13
|
import { basename } from 'node:path';
|
|
13
|
-
import {
|
|
14
|
-
import { Generator } from 'graphql-typescript-deluxe';
|
|
14
|
+
import { printSourceLocation, parse, Source } from 'graphql';
|
|
15
|
+
import { Generator, FieldNotFoundError, TypeNotFoundError, FragmentNotFoundError } from 'graphql-typescript-deluxe';
|
|
15
16
|
import { pascalCase } from 'change-case-all';
|
|
16
17
|
import colors from 'picocolors';
|
|
17
18
|
import { validateGraphQlDocuments } from '@graphql-tools/utils';
|
|
18
19
|
|
|
19
20
|
const name = "nuxt-graphql-middleware";
|
|
20
|
-
const version = "5.0.0-alpha.
|
|
21
|
+
const version = "5.0.0-alpha.4";
|
|
21
22
|
|
|
22
23
|
const DEVTOOLS_UI_ROUTE = "/__nuxt-graphql-middleware";
|
|
23
24
|
const DEVTOOLS_UI_LOCAL_PORT = 3300;
|
|
@@ -107,14 +108,14 @@ function validateOptions(options) {
|
|
|
107
108
|
async function getSchemaPath(schemaPath, options, resolver, writeToDisk = false) {
|
|
108
109
|
const dest = resolver(schemaPath);
|
|
109
110
|
if (!options.downloadSchema) {
|
|
110
|
-
const fileExists2 = await
|
|
111
|
+
const fileExists2 = await fs.access(dest).then(() => true).catch(() => false);
|
|
111
112
|
if (!fileExists2) {
|
|
112
113
|
logger.error(
|
|
113
114
|
'"downloadSchema" is set to false but no schema exists at ' + dest
|
|
114
115
|
);
|
|
115
116
|
throw new Error("Missing GraphQL schema.");
|
|
116
117
|
}
|
|
117
|
-
const schemaContent = await
|
|
118
|
+
const schemaContent = await fs.readFile(dest).then((v) => v.toString());
|
|
118
119
|
return { schemaPath, schemaContent };
|
|
119
120
|
}
|
|
120
121
|
if (!options.graphqlEndpoint) {
|
|
@@ -216,20 +217,21 @@ function generateContextTemplate(collectedOperations, serverApiPrefix) {
|
|
|
216
217
|
queryResult.code,
|
|
217
218
|
mutationResult.code,
|
|
218
219
|
subscriptionResult.code
|
|
219
|
-
].join("\n");
|
|
220
|
+
].filter(Boolean).join("\n");
|
|
220
221
|
const combinedNitroCode = [
|
|
221
222
|
queryResult.nitroCode,
|
|
222
223
|
mutationResult.nitroCode,
|
|
223
224
|
subscriptionResult.nitroCode
|
|
224
225
|
].join("\n");
|
|
226
|
+
const typeImports = allImports.length ? `import type {
|
|
227
|
+
${allImports.join(",\n ")}
|
|
228
|
+
} from './../graphql-operations'` : "";
|
|
225
229
|
return `
|
|
226
230
|
import type { GraphqlResponse } from '#graphql-middleware-server-options-build'
|
|
227
|
-
|
|
228
|
-
${allImports.join(",\n ")}
|
|
229
|
-
} from './../graphql-operations'
|
|
231
|
+
${typeImports}
|
|
230
232
|
|
|
231
233
|
declare module '#nuxt-graphql-middleware/generated-types' {
|
|
232
|
-
export type GraphqlMiddlewareResponseUnion = ${allResultTypes.join(" | ")}
|
|
234
|
+
export type GraphqlMiddlewareResponseUnion = ${allResultTypes.join(" | ") || "never"}
|
|
233
235
|
${combinedCode}
|
|
234
236
|
}
|
|
235
237
|
|
|
@@ -241,6 +243,8 @@ ${combinedNitroCode}
|
|
|
241
243
|
`;
|
|
242
244
|
}
|
|
243
245
|
|
|
246
|
+
const SYMBOL_CROSS = "x";
|
|
247
|
+
const SYMBOL_CHECK = "\u2714";
|
|
244
248
|
function getMaxLengths(entries) {
|
|
245
249
|
let name = 0;
|
|
246
250
|
let path = 0;
|
|
@@ -263,11 +267,11 @@ function logAllEntries(entries) {
|
|
|
263
267
|
let prevHadError = false;
|
|
264
268
|
for (const entry of entries) {
|
|
265
269
|
const hasErrors = entry.errors.length > 0;
|
|
266
|
-
const icon = hasErrors ? colors.red(
|
|
270
|
+
const icon = hasErrors ? colors.red(SYMBOL_CROSS) : colors.green(SYMBOL_CHECK);
|
|
267
271
|
const type = entry.type.padEnd(lengths.type);
|
|
268
272
|
const namePadded = colors.bold(entry.name.padEnd(lengths.name));
|
|
269
273
|
const name = hasErrors ? colors.red(namePadded) : colors.green(namePadded);
|
|
270
|
-
const path = colors.dim(entry.path
|
|
274
|
+
const path = colors.dim(entry.path);
|
|
271
275
|
const parts = [icon, type, name, path];
|
|
272
276
|
if (hasErrors && !prevHadError) {
|
|
273
277
|
process.stdout.write("-".repeat(process.stdout.columns) + "\n");
|
|
@@ -293,6 +297,7 @@ function logAllEntries(entries) {
|
|
|
293
297
|
}
|
|
294
298
|
logger.restoreStd();
|
|
295
299
|
}
|
|
300
|
+
|
|
296
301
|
class CollectedFile {
|
|
297
302
|
filePath;
|
|
298
303
|
fileContents;
|
|
@@ -324,12 +329,22 @@ class CollectedFile {
|
|
|
324
329
|
return false;
|
|
325
330
|
}
|
|
326
331
|
}
|
|
332
|
+
|
|
327
333
|
class Collector {
|
|
328
|
-
constructor(schema, context, nuxtConfigDocuments = [], generatorOptions) {
|
|
334
|
+
constructor(schema, context, nuxtConfigDocuments = [], generatorOptions = {}) {
|
|
329
335
|
this.schema = schema;
|
|
330
336
|
this.context = context;
|
|
331
337
|
this.nuxtConfigDocuments = nuxtConfigDocuments;
|
|
332
|
-
|
|
338
|
+
const mappedOptions = { ...generatorOptions };
|
|
339
|
+
if (!mappedOptions.output) {
|
|
340
|
+
mappedOptions.output = {};
|
|
341
|
+
}
|
|
342
|
+
if (!mappedOptions.output.buildTypeDocFilePath) {
|
|
343
|
+
mappedOptions.output.buildTypeDocFilePath = (filePath) => {
|
|
344
|
+
return this.filePathToBuildRelative(filePath);
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
this.generator = new Generator(schema, mappedOptions);
|
|
333
348
|
}
|
|
334
349
|
/**
|
|
335
350
|
* All collected files.
|
|
@@ -343,10 +358,18 @@ class Collector {
|
|
|
343
358
|
* A map of operation name and timestamp when the operation was last validated.
|
|
344
359
|
*/
|
|
345
360
|
operationTimestamps = /* @__PURE__ */ new Map();
|
|
361
|
+
/**
|
|
362
|
+
* The generated operations and fragments.
|
|
363
|
+
*/
|
|
364
|
+
rpcItems = /* @__PURE__ */ new Map();
|
|
346
365
|
/**
|
|
347
366
|
* The generated TypeScript type template output.
|
|
348
367
|
*/
|
|
349
368
|
outputTypes = "";
|
|
369
|
+
/**
|
|
370
|
+
* The generated TypeScript enum template output.
|
|
371
|
+
*/
|
|
372
|
+
outputEnums = "";
|
|
350
373
|
/**
|
|
351
374
|
* The generated oeprations file.
|
|
352
375
|
*/
|
|
@@ -355,21 +378,31 @@ class Collector {
|
|
|
355
378
|
* The generated context template file.
|
|
356
379
|
*/
|
|
357
380
|
outputContext = "";
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
return filePath.replace(this.context.srcDir, "~");
|
|
381
|
+
filePathToBuildRelative(filePath) {
|
|
382
|
+
return "./" + relative(this.context.buildDir, filePath);
|
|
383
|
+
}
|
|
384
|
+
filePathToSourceRelative(filePath) {
|
|
385
|
+
return "./" + relative(process.cwd(), filePath);
|
|
364
386
|
}
|
|
365
387
|
operationToLogEntry(operation, errors) {
|
|
366
388
|
return {
|
|
367
389
|
name: operation.graphqlName,
|
|
368
390
|
type: operation.operationType,
|
|
369
|
-
path: this.
|
|
391
|
+
path: this.filePathToSourceRelative(operation.filePath),
|
|
370
392
|
errors
|
|
371
393
|
};
|
|
372
394
|
}
|
|
395
|
+
buildOutputTypes(file) {
|
|
396
|
+
let output = "";
|
|
397
|
+
const enumImports = file.getTypeScriptEnumDependencies();
|
|
398
|
+
if (enumImports.length) {
|
|
399
|
+
output += `import type { ${enumImports.join(", ")} } from './enums'
|
|
400
|
+
|
|
401
|
+
`;
|
|
402
|
+
}
|
|
403
|
+
output += file.getSource();
|
|
404
|
+
return output;
|
|
405
|
+
}
|
|
373
406
|
/**
|
|
374
407
|
* Executes code gen and performs validation for operations.
|
|
375
408
|
*/
|
|
@@ -377,8 +410,9 @@ class Collector {
|
|
|
377
410
|
const output = this.generator.build();
|
|
378
411
|
const operations = output.getCollectedOperations();
|
|
379
412
|
const generatedCode = output.getGeneratedCode();
|
|
380
|
-
this.outputOperations = output.getOperationsFile();
|
|
381
|
-
this.
|
|
413
|
+
this.outputOperations = output.getOperationsFile().getSource();
|
|
414
|
+
this.outputEnums = output.buildFile(["enum"]).getSource();
|
|
415
|
+
this.outputTypes = this.buildOutputTypes(output.getTypes());
|
|
382
416
|
this.outputContext = generateContextTemplate(
|
|
383
417
|
operations,
|
|
384
418
|
this.context.serverApiPrefix
|
|
@@ -401,9 +435,7 @@ class Collector {
|
|
|
401
435
|
if (previousTimestamp === operation.timestamp) {
|
|
402
436
|
continue;
|
|
403
437
|
}
|
|
404
|
-
const fragments = operation.
|
|
405
|
-
(v) => v.type === "fragment-name" ? fragmentMap.get(v.value) || "" : ""
|
|
406
|
-
).join("\n");
|
|
438
|
+
const fragments = operation.getGraphQLFragmentDependencies().map((v) => fragmentMap.get(v) || "").join("\n");
|
|
407
439
|
const fullOperation = operationSourceMap.get(operation.graphqlName) + fragments;
|
|
408
440
|
const source = new Source(fullOperation, basename(operation.filePath));
|
|
409
441
|
const document = parse(source);
|
|
@@ -413,12 +445,55 @@ class Collector {
|
|
|
413
445
|
} else {
|
|
414
446
|
this.operationTimestamps.set(operation.graphqlName, operation.timestamp);
|
|
415
447
|
}
|
|
416
|
-
|
|
448
|
+
const shouldLog = errors.length || !this.context.logOnlyErrors;
|
|
449
|
+
if (shouldLog) {
|
|
450
|
+
logEntries.push(this.operationToLogEntry(operation, errors));
|
|
451
|
+
}
|
|
417
452
|
}
|
|
418
453
|
logAllEntries(logEntries);
|
|
419
454
|
if (hasErrors) {
|
|
420
455
|
throw new Error("GraphQL errors");
|
|
421
456
|
}
|
|
457
|
+
for (const code of generatedCode) {
|
|
458
|
+
const id = `${code.identifier}_${code.graphqlName}`;
|
|
459
|
+
if (code.identifier === "fragment" || code.identifier === "mutation" || code.identifier === "query") {
|
|
460
|
+
if (this.rpcItems.get(id)?.timestamp === code.timestamp) {
|
|
461
|
+
continue;
|
|
462
|
+
}
|
|
463
|
+
const fragmentDepdendencies = code.getGraphQLFragmentDependencies().map((name) => fragmentMap.get(name) || "").join("\n\n");
|
|
464
|
+
this.rpcItems.set(id, {
|
|
465
|
+
id,
|
|
466
|
+
timestamp: code.timestamp,
|
|
467
|
+
source: code.source + "\n\n" + fragmentDepdendencies,
|
|
468
|
+
name: code.graphqlName,
|
|
469
|
+
filePath: code.filePath,
|
|
470
|
+
identifier: code.identifier
|
|
471
|
+
});
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
buildErrorMessage(error) {
|
|
476
|
+
let output = "";
|
|
477
|
+
if (error instanceof FieldNotFoundError || error instanceof TypeNotFoundError || error instanceof FragmentNotFoundError) {
|
|
478
|
+
const filePath = error.context?.filePath;
|
|
479
|
+
const file = filePath ? this.files.get(filePath) : null;
|
|
480
|
+
if (filePath) {
|
|
481
|
+
output += ` | ${this.filePathToSourceRelative(filePath)}
|
|
482
|
+
`;
|
|
483
|
+
}
|
|
484
|
+
output += "\n" + error.message + "\n\n";
|
|
485
|
+
if (file) {
|
|
486
|
+
output += file.fileContents;
|
|
487
|
+
}
|
|
488
|
+
} else if (error instanceof Error) {
|
|
489
|
+
output += "\n" + error.message;
|
|
490
|
+
}
|
|
491
|
+
return output;
|
|
492
|
+
}
|
|
493
|
+
logError(error) {
|
|
494
|
+
let output = `${SYMBOL_CROSS}`;
|
|
495
|
+
output += this.buildErrorMessage(error);
|
|
496
|
+
logger.error(colors.red(output));
|
|
422
497
|
}
|
|
423
498
|
/**
|
|
424
499
|
* Get all file paths that match the import patterns.
|
|
@@ -435,20 +510,27 @@ class Collector {
|
|
|
435
510
|
* Initialise the collector.
|
|
436
511
|
*/
|
|
437
512
|
async init() {
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
const
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
513
|
+
try {
|
|
514
|
+
const files = await this.getImportPatternFiles();
|
|
515
|
+
for (const filePath of files) {
|
|
516
|
+
await this.addFile(filePath);
|
|
517
|
+
}
|
|
518
|
+
const nuxtConfigDocuments = this.nuxtConfigDocuments.join("\n\n");
|
|
519
|
+
if (nuxtConfigDocuments.length) {
|
|
520
|
+
const filePath = this.context.nuxtConfigPath;
|
|
521
|
+
const file = new CollectedFile(filePath, nuxtConfigDocuments, false);
|
|
522
|
+
this.files.set(filePath, file);
|
|
523
|
+
this.generator.add({
|
|
524
|
+
filePath,
|
|
525
|
+
documentNode: file.parsed
|
|
526
|
+
});
|
|
527
|
+
}
|
|
528
|
+
this.buildState();
|
|
529
|
+
logger.success("All GraphQL documents are valid.");
|
|
530
|
+
} catch (e) {
|
|
531
|
+
this.logError(e);
|
|
532
|
+
throw new Error("GraphQL document validation failed.");
|
|
533
|
+
}
|
|
452
534
|
}
|
|
453
535
|
/**
|
|
454
536
|
* Add a file.
|
|
@@ -457,12 +539,16 @@ class Collector {
|
|
|
457
539
|
const file = await CollectedFile.fromFilePath(filePath);
|
|
458
540
|
this.files.set(filePath, file);
|
|
459
541
|
this.generator.add({
|
|
460
|
-
filePath
|
|
542
|
+
filePath,
|
|
461
543
|
documentNode: file.parsed
|
|
462
544
|
});
|
|
463
545
|
return file;
|
|
464
546
|
}
|
|
465
547
|
async handleAdd(filePath) {
|
|
548
|
+
const matching = await this.getImportPatternFiles();
|
|
549
|
+
if (!matching.includes(filePath)) {
|
|
550
|
+
return false;
|
|
551
|
+
}
|
|
466
552
|
await this.addFile(filePath);
|
|
467
553
|
return true;
|
|
468
554
|
}
|
|
@@ -476,7 +562,7 @@ class Collector {
|
|
|
476
562
|
return false;
|
|
477
563
|
}
|
|
478
564
|
this.generator.update({
|
|
479
|
-
filePath
|
|
565
|
+
filePath,
|
|
480
566
|
documentNode: file.parsed
|
|
481
567
|
});
|
|
482
568
|
return true;
|
|
@@ -487,7 +573,7 @@ class Collector {
|
|
|
487
573
|
return false;
|
|
488
574
|
}
|
|
489
575
|
this.files.delete(filePath);
|
|
490
|
-
this.generator.remove(
|
|
576
|
+
this.generator.remove(filePath);
|
|
491
577
|
return true;
|
|
492
578
|
}
|
|
493
579
|
handleUnlinkDir(folderPath) {
|
|
@@ -506,8 +592,8 @@ class Collector {
|
|
|
506
592
|
* Handle the watcher event for the given file path.
|
|
507
593
|
*/
|
|
508
594
|
async handleWatchEvent(event, filePath) {
|
|
595
|
+
let hasChanged = false;
|
|
509
596
|
try {
|
|
510
|
-
let hasChanged = false;
|
|
511
597
|
if (event === "add") {
|
|
512
598
|
hasChanged = await this.handleAdd(filePath);
|
|
513
599
|
} else if (event === "change") {
|
|
@@ -523,8 +609,17 @@ class Collector {
|
|
|
523
609
|
}
|
|
524
610
|
} catch (e) {
|
|
525
611
|
this.generator.resetCaches();
|
|
526
|
-
|
|
612
|
+
logger.error("Failed to update GraphQL code.");
|
|
613
|
+
this.logError(e);
|
|
614
|
+
return {
|
|
615
|
+
hasChanged: false,
|
|
616
|
+
error: { message: this.buildErrorMessage(e) }
|
|
617
|
+
};
|
|
618
|
+
}
|
|
619
|
+
if (hasChanged) {
|
|
620
|
+
logger.success("Finished GraphQL code update successfully.");
|
|
527
621
|
}
|
|
622
|
+
return { hasChanged };
|
|
528
623
|
}
|
|
529
624
|
/**
|
|
530
625
|
* Get the TypeScript types template contents.
|
|
@@ -532,6 +627,12 @@ class Collector {
|
|
|
532
627
|
getTemplateTypes() {
|
|
533
628
|
return this.outputTypes;
|
|
534
629
|
}
|
|
630
|
+
/**
|
|
631
|
+
* Get the TypeScript Enums template contents.
|
|
632
|
+
*/
|
|
633
|
+
getTemplateEnums() {
|
|
634
|
+
return this.outputEnums;
|
|
635
|
+
}
|
|
535
636
|
/**
|
|
536
637
|
* Get the context template contents.
|
|
537
638
|
*/
|
|
@@ -544,13 +645,15 @@ class Collector {
|
|
|
544
645
|
getTemplateOperations() {
|
|
545
646
|
return this.outputOperations;
|
|
546
647
|
}
|
|
547
|
-
/**
|
|
548
|
-
* Log results (including parse/validation errors).
|
|
549
|
-
*/
|
|
550
|
-
logDocuments(logEverything) {
|
|
551
|
-
}
|
|
552
648
|
}
|
|
553
649
|
|
|
650
|
+
function useViteWebSocket(nuxt) {
|
|
651
|
+
return new Promise((resolve) => {
|
|
652
|
+
nuxt.hooks.hook("vite:serverCreated", (viteServer) => {
|
|
653
|
+
resolve(viteServer.ws);
|
|
654
|
+
});
|
|
655
|
+
});
|
|
656
|
+
}
|
|
554
657
|
const RPC_NAMESPACE = "nuxt-graphql-middleware";
|
|
555
658
|
const module = defineNuxtModule({
|
|
556
659
|
meta: {
|
|
@@ -564,7 +667,8 @@ const module = defineNuxtModule({
|
|
|
564
667
|
defaults: defaultOptions,
|
|
565
668
|
async setup(passedOptions, nuxt) {
|
|
566
669
|
const options = defu({}, passedOptions, defaultOptions);
|
|
567
|
-
function addAlias(name2,
|
|
670
|
+
function addAlias(name2, arg) {
|
|
671
|
+
const aliasPath = typeof arg === "string" ? arg : arg.dst;
|
|
568
672
|
nuxt.options.alias[name2] = aliasPath;
|
|
569
673
|
}
|
|
570
674
|
const isModuleBuild = process.env.MODULE_BUILD === "true" && nuxt.options._prepare;
|
|
@@ -605,40 +709,37 @@ const module = defineNuxtModule({
|
|
|
605
709
|
});
|
|
606
710
|
const runtimeDir = fileURLToPath(new URL("./runtime", import.meta.url));
|
|
607
711
|
nuxt.options.build.transpile.push(runtimeDir);
|
|
608
|
-
const context = {
|
|
609
|
-
patterns: options.autoImportPatterns || [],
|
|
610
|
-
srcDir: nuxt.options.srcDir,
|
|
611
|
-
schemaPath,
|
|
612
|
-
serverApiPrefix: options.serverApiPrefix
|
|
613
|
-
};
|
|
614
712
|
const collector = new Collector(
|
|
615
713
|
schema,
|
|
616
|
-
|
|
714
|
+
{
|
|
715
|
+
patterns: options.autoImportPatterns || [],
|
|
716
|
+
srcDir: nuxt.options.srcDir,
|
|
717
|
+
buildDir: srcResolver.resolve(nuxt.options.buildDir),
|
|
718
|
+
nuxtConfigPath: rootResolver.resolve("nuxt.config.ts"),
|
|
719
|
+
schemaPath,
|
|
720
|
+
serverApiPrefix: options.serverApiPrefix,
|
|
721
|
+
logOnlyErrors: !!options.logOnlyErrors
|
|
722
|
+
},
|
|
617
723
|
options.documents,
|
|
618
724
|
options.codegenConfig
|
|
619
725
|
);
|
|
620
726
|
await collector.init();
|
|
621
|
-
|
|
727
|
+
const isDevToolsEnabled = nuxt.options.dev && options.devtools;
|
|
728
|
+
let rpc;
|
|
729
|
+
if (isDevToolsEnabled) {
|
|
622
730
|
const clientPath = moduleResolver.resolve("./client");
|
|
623
731
|
setupDevToolsUI(nuxt, clientPath);
|
|
624
|
-
|
|
625
|
-
extendServerRpc(RPC_NAMESPACE, {
|
|
732
|
+
onDevToolsInitialized(() => {
|
|
733
|
+
rpc = extendServerRpc(RPC_NAMESPACE, {
|
|
626
734
|
// register server RPC functions
|
|
627
735
|
getModuleOptions() {
|
|
628
736
|
return options;
|
|
629
737
|
},
|
|
630
738
|
getDocuments() {
|
|
631
|
-
return [];
|
|
739
|
+
return [...collector.rpcItems.values()];
|
|
632
740
|
}
|
|
633
741
|
});
|
|
634
|
-
};
|
|
635
|
-
try {
|
|
636
|
-
setupRpc();
|
|
637
|
-
} catch (_e) {
|
|
638
|
-
onDevToolsInitialized(() => {
|
|
639
|
-
setupRpc();
|
|
640
|
-
});
|
|
641
|
-
}
|
|
742
|
+
});
|
|
642
743
|
}
|
|
643
744
|
nuxt.options.runtimeConfig.public["nuxt-graphql-middleware"] = {
|
|
644
745
|
serverApiPrefix: options.serverApiPrefix
|
|
@@ -676,12 +777,22 @@ const module = defineNuxtModule({
|
|
|
676
777
|
);
|
|
677
778
|
addServerImports(serverUtils);
|
|
678
779
|
}
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
780
|
+
addAlias(
|
|
781
|
+
"#graphql-operations",
|
|
782
|
+
addTemplate({
|
|
783
|
+
filename: GraphqlMiddlewareTemplate.OperationTypes,
|
|
784
|
+
write: true,
|
|
785
|
+
getContents: () => collector.getTemplateTypes()
|
|
786
|
+
})
|
|
787
|
+
);
|
|
788
|
+
addAlias(
|
|
789
|
+
"#graphql-operations/enums",
|
|
790
|
+
addTemplate({
|
|
791
|
+
filename: GraphqlMiddlewareTemplate.Enums,
|
|
792
|
+
write: true,
|
|
793
|
+
getContents: () => collector.getTemplateEnums()
|
|
794
|
+
})
|
|
795
|
+
);
|
|
685
796
|
const templateDocuments = addTemplate({
|
|
686
797
|
filename: GraphqlMiddlewareTemplate.Documents,
|
|
687
798
|
write: true,
|
|
@@ -738,12 +849,12 @@ declare module '#graphql-documents' {
|
|
|
738
849
|
}
|
|
739
850
|
logger.info("No graphqlMiddleware.serverOptions file found.");
|
|
740
851
|
};
|
|
741
|
-
const
|
|
852
|
+
const resolvedServerOptionsPath = findServerOptions();
|
|
742
853
|
const moduleTypesPath = relative(
|
|
743
854
|
nuxt.options.buildDir,
|
|
744
855
|
moduleResolver.resolve("./types")
|
|
745
856
|
);
|
|
746
|
-
const resolvedPathRelative =
|
|
857
|
+
const resolvedPathRelative = resolvedServerOptionsPath ? relative(nuxt.options.buildDir, resolvedServerOptionsPath) : null;
|
|
747
858
|
const template = addTemplate({
|
|
748
859
|
filename: "graphqlMiddleware.serverOptions.mjs",
|
|
749
860
|
write: true,
|
|
@@ -853,10 +964,22 @@ export type GraphqlClientContext = {}
|
|
|
853
964
|
);
|
|
854
965
|
});
|
|
855
966
|
if (nuxt.options.dev || nuxt.options._prepare) {
|
|
967
|
+
let sendError = function(error) {
|
|
968
|
+
wsPromise.then((ws) => {
|
|
969
|
+
ws.send({
|
|
970
|
+
type: "error",
|
|
971
|
+
err: {
|
|
972
|
+
message: error.message,
|
|
973
|
+
stack: ""
|
|
974
|
+
}
|
|
975
|
+
});
|
|
976
|
+
});
|
|
977
|
+
};
|
|
856
978
|
addServerHandler({
|
|
857
979
|
handler: moduleResolver.resolve("./runtime/serverHandler/debug"),
|
|
858
980
|
route: options.serverApiPrefix + "/debug"
|
|
859
981
|
});
|
|
982
|
+
const wsPromise = useViteWebSocket(nuxt);
|
|
860
983
|
nuxt.hook("builder:watch", async (event, pathAbsolute) => {
|
|
861
984
|
if (pathAbsolute === schemaPath) {
|
|
862
985
|
return;
|
|
@@ -864,7 +987,16 @@ export type GraphqlClientContext = {}
|
|
|
864
987
|
if (!pathAbsolute.match(/\.(gql|graphql)$/)) {
|
|
865
988
|
return;
|
|
866
989
|
}
|
|
867
|
-
await collector.handleWatchEvent(
|
|
990
|
+
const { hasChanged, error } = await collector.handleWatchEvent(
|
|
991
|
+
event,
|
|
992
|
+
pathAbsolute
|
|
993
|
+
);
|
|
994
|
+
if (error) {
|
|
995
|
+
sendError(error);
|
|
996
|
+
}
|
|
997
|
+
if (hasChanged && rpc) {
|
|
998
|
+
rpc.broadcast.documentsUpdated([...collector.rpcItems.values()]);
|
|
999
|
+
}
|
|
868
1000
|
});
|
|
869
1001
|
}
|
|
870
1002
|
}
|
|
@@ -3,6 +3,22 @@ import { getEndpoint } from "./../helpers/composables.js";
|
|
|
3
3
|
import { hash } from "ohash";
|
|
4
4
|
import { GraphqlMiddlewareCache } from "../helpers/ClientCache.js";
|
|
5
5
|
import { useNuxtApp, useAppConfig } from "#imports";
|
|
6
|
+
function logGraphQLErrors(operation, operationName, errors) {
|
|
7
|
+
errors.forEach((error) => {
|
|
8
|
+
console.group(
|
|
9
|
+
`Error in GraphQL response for ${operation} "${operationName}"`
|
|
10
|
+
);
|
|
11
|
+
console.error(`Message: ${error.message}`);
|
|
12
|
+
if (error.locations && error.locations.length > 0) {
|
|
13
|
+
const formattedLocations = error.locations.map((loc) => `line ${loc.line}, column ${loc.column}`).join(" | ");
|
|
14
|
+
console.error(`Locations: ${formattedLocations}`);
|
|
15
|
+
}
|
|
16
|
+
if (error.path) {
|
|
17
|
+
console.error(`Path: ${error.path.join(" -> ")}`);
|
|
18
|
+
}
|
|
19
|
+
console.groupEnd();
|
|
20
|
+
});
|
|
21
|
+
}
|
|
6
22
|
export function performRequest(operation, operationName, method, options, cacheOptions) {
|
|
7
23
|
const state = useGraphqlState();
|
|
8
24
|
const app = useNuxtApp();
|
|
@@ -34,6 +50,9 @@ export function performRequest(operation, operationName, method, options, cacheO
|
|
|
34
50
|
method
|
|
35
51
|
}
|
|
36
52
|
).then((v) => {
|
|
53
|
+
if (import.meta.dev && v.errors?.length) {
|
|
54
|
+
logGraphQLErrors(operation, operationName, v.errors);
|
|
55
|
+
}
|
|
37
56
|
return {
|
|
38
57
|
...v,
|
|
39
58
|
data: v.data,
|
|
@@ -2,7 +2,11 @@ export declare enum GraphqlMiddlewareTemplate {
|
|
|
2
2
|
/**
|
|
3
3
|
* Contains the TS definitions for all GraphQL queries, mutations and fragments.
|
|
4
4
|
*/
|
|
5
|
-
OperationTypes = "graphql-operations.ts",
|
|
5
|
+
OperationTypes = "graphql-operations/index.d.ts",
|
|
6
|
+
/**
|
|
7
|
+
* Contains the TS definitions for all GraphQL queries, mutations and fragments.
|
|
8
|
+
*/
|
|
9
|
+
Enums = "graphql-operations/enums.ts",
|
|
6
10
|
/**
|
|
7
11
|
* Signature for the GraphQL composable arguments and return types.
|
|
8
12
|
*/
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export var GraphqlMiddlewareTemplate = /* @__PURE__ */ ((GraphqlMiddlewareTemplate2) => {
|
|
2
|
-
GraphqlMiddlewareTemplate2["OperationTypes"] = "graphql-operations.ts";
|
|
2
|
+
GraphqlMiddlewareTemplate2["OperationTypes"] = "graphql-operations/index.d.ts";
|
|
3
|
+
GraphqlMiddlewareTemplate2["Enums"] = "graphql-operations/enums.ts";
|
|
3
4
|
GraphqlMiddlewareTemplate2["ComposableContext"] = "nuxt-graphql-middleware/generated-types.d.ts";
|
|
4
5
|
GraphqlMiddlewareTemplate2["Documents"] = "nuxt-graphql-middleware/graphql-documents.mjs";
|
|
5
6
|
return GraphqlMiddlewareTemplate2;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "nuxt-graphql-middleware",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.4",
|
|
4
4
|
"description": "Module to perform GraphQL requests as a server middleware.",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -35,8 +35,7 @@
|
|
|
35
35
|
"dist"
|
|
36
36
|
],
|
|
37
37
|
"scripts": {
|
|
38
|
-
"prepack": "nuxt-module-build build",
|
|
39
|
-
"prepack_": "nuxt-module-build build && npm run client:build",
|
|
38
|
+
"prepack": "nuxt-module-build build && npm run client:build",
|
|
40
39
|
"dev": "nuxi dev playground --trace-warnings",
|
|
41
40
|
"dev:layers": "nuxi dev playground-layers --trace-warnings",
|
|
42
41
|
"debug": "nuxi dev playground --inspect",
|
|
@@ -65,13 +64,9 @@
|
|
|
65
64
|
"dependencies": {
|
|
66
65
|
"@graphql-codegen/cli": "^5.0.5",
|
|
67
66
|
"@graphql-codegen/schema-ast": "^4.1.0",
|
|
68
|
-
"@graphql-codegen/typescript": "^4.1.3",
|
|
69
|
-
"@graphql-codegen/typescript-generic-sdk": "^4.0.1",
|
|
70
|
-
"@graphql-codegen/typescript-operations": "^4.4.1",
|
|
71
67
|
"@graphql-tools/utils": "^10.2.2",
|
|
72
68
|
"@nuxt/devtools-kit": "1.3.7",
|
|
73
|
-
"
|
|
74
|
-
"graphql-typescript-deluxe": "^0.0.1",
|
|
69
|
+
"graphql-typescript-deluxe": "^0.0.5",
|
|
75
70
|
"inquirer": "^9.3.2",
|
|
76
71
|
"minisearch": "^6.3.0",
|
|
77
72
|
"picocolors": "^1.0.1"
|