nuxt-graphql-middleware 4.0.0-beta.6 → 4.0.0-beta.8

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.
Files changed (34) hide show
  1. package/dist/client/200.html +12 -2
  2. package/dist/client/404.html +12 -2
  3. package/dist/client/_nuxt/entry.1bc811cf.css +1 -0
  4. package/dist/client/_nuxt/entry.ea5ff097.js +7 -0
  5. package/dist/client/_nuxt/error-404.0c27f143.js +1 -0
  6. package/dist/client/_nuxt/error-404.a6f724cc.css +1 -0
  7. package/dist/client/_nuxt/error-500.0ea30584.js +1 -0
  8. package/dist/client/_nuxt/error-500.d92554dd.css +1 -0
  9. package/dist/client/_nuxt/index.3f9296a3.css +1 -0
  10. package/dist/client/_nuxt/index.e982558e.js +2 -0
  11. package/dist/client/_nuxt/vue.f36acd1f.b03bdbb5.js +1 -0
  12. package/dist/client/index.html +12 -2
  13. package/dist/module.d.mts +318 -0
  14. package/dist/module.d.ts +63 -1
  15. package/dist/module.json +1 -1
  16. package/dist/module.mjs +103 -16
  17. package/dist/runtime/composables/useGraphqlState.d.ts +1 -2
  18. package/dist/runtime/composables/useGraphqlState.mjs +1 -1
  19. package/dist/runtime/plugins/provideState.d.ts +1 -1
  20. package/dist/runtime/serverHandler/debug.d.ts +1 -1
  21. package/dist/runtime/serverHandler/index.d.ts +1 -1
  22. package/dist/runtime/serverHandler/index.mjs +33 -12
  23. package/dist/types.d.mts +17 -0
  24. package/dist/types.d.ts +6 -0
  25. package/package.json +35 -34
  26. package/dist/client/_nuxt/entry.0bb826e1.js +0 -5
  27. package/dist/client/_nuxt/entry.0dd29d47.css +0 -1
  28. package/dist/client/_nuxt/error-404.0cd4f3dd.css +0 -1
  29. package/dist/client/_nuxt/error-404.ffea5d7c.js +0 -1
  30. package/dist/client/_nuxt/error-500.748cb764.css +0 -1
  31. package/dist/client/_nuxt/error-500.d39ec227.js +0 -1
  32. package/dist/client/_nuxt/error-component.3d690bfd.js +0 -3
  33. package/dist/client/_nuxt/index.86b21181.js +0 -2
  34. package/dist/client/_nuxt/index.dc8fb7ec.css +0 -1
package/dist/module.mjs CHANGED
@@ -6,21 +6,21 @@ import inquirer from 'inquirer';
6
6
  import { onDevToolsInitialized, extendServerRpc } from '@nuxt/devtools-kit';
7
7
  import { existsSync } from 'fs';
8
8
  import { promises, existsSync as existsSync$1 } from 'node:fs';
9
+ import { oldVisit } from '@graphql-codegen/plugin-helpers';
9
10
  import fragmentImport from '@graphql-fragment-import/lib/inline-imports.js';
10
11
  import { validateGraphQlDocuments } from '@graphql-tools/utils';
11
12
  import { loadSchema } from '@graphql-tools/load';
12
13
  import Table from 'cli-table';
13
14
  import chalk from 'chalk';
14
- import { concatAST, print, Source, parse } from 'graphql';
15
+ import { concatAST, parse, print, visit, Kind, Source } from 'graphql';
15
16
  import { generate as generate$1, executeCodegen } from '@graphql-codegen/cli';
16
17
  import * as PluginTypescript from '@graphql-codegen/typescript';
17
18
  import * as PluginTypescriptOperations from '@graphql-codegen/typescript-operations';
18
19
  import * as PluginSchemaAst from '@graphql-codegen/schema-ast';
19
- import { oldVisit } from '@graphql-codegen/plugin-helpers';
20
20
  import { pascalCase } from 'change-case-all';
21
21
 
22
22
  const name = "nuxt-graphql-middleware";
23
- const version = "4.0.0-beta.6";
23
+ const version = "4.0.0-beta.8";
24
24
 
25
25
  const DEVTOOLS_UI_ROUTE = "/__nuxt-graphql-middleware";
26
26
  const DEVTOOLS_UI_LOCAL_PORT = 3300;
@@ -303,7 +303,7 @@ function validateDeprecated(options) {
303
303
  );
304
304
  if (key === "graphqlEndpoint") {
305
305
  logger.info(`
306
- import { defineGraphqlServerOptions } from '#graphql-server-options'
306
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/dist/runtime/serverOptions'
307
307
  import { getHeader } from 'h3'
308
308
  import acceptLanguageParser from 'accept-language-parser';
309
309
 
@@ -321,7 +321,7 @@ export default defineGraphqlServerOptions({
321
321
  }
322
322
  if (key === "serverFetchOptions") {
323
323
  logger.info(`
324
- import { defineGraphqlServerOptions } from '#graphql-server-options'
324
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/dist/runtime/serverOptions'
325
325
  import { getHeader } from 'h3'
326
326
 
327
327
  // Pass the cookie from the client request to the GraphQL request.
@@ -337,7 +337,7 @@ export default defineGraphqlServerOptions({
337
337
  }
338
338
  if (key === "onServerResponse") {
339
339
  logger.info(`
340
- import { defineGraphqlServerOptions } from '#graphql-server-options'
340
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/dist/runtime/serverOptions'
341
341
  import type { H3Event } from 'h3'
342
342
  import type { FetchResponse } from 'ofetch'
343
343
 
@@ -362,7 +362,7 @@ export default defineGraphqlServerOptions({
362
362
  }
363
363
  if (key === "onServerError") {
364
364
  logger.info(`
365
- import { defineGraphqlServerOptions } from '#graphql-server-options'
365
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/dist/runtime/serverOptions'
366
366
  import type { H3Event } from 'h3'
367
367
  import type { FetchError } from 'ofetch'
368
368
 
@@ -461,8 +461,9 @@ function parseDocument(document, srcDir) {
461
461
  return parse(source);
462
462
  }
463
463
  function validateDocuments(schema, documents, srcDir) {
464
+ const validated = [];
464
465
  for (let i = 0; i < documents.length; i++) {
465
- const document = documents[i];
466
+ const document = { ...documents[i] };
466
467
  if (document.filename) {
467
468
  document.relativePath = document.filename.replace(srcDir + "/", "");
468
469
  }
@@ -490,8 +491,59 @@ function validateDocuments(schema, documents, srcDir) {
490
491
  document.isValid = false;
491
492
  }
492
493
  document.id = [document.operation, document.name, document.filename].filter(Boolean).join("_");
494
+ validated.push(document);
495
+ if (!document.isValid) {
496
+ break;
497
+ }
498
+ }
499
+ return validated;
500
+ }
501
+ function cleanGraphqlDocument(graphqlContent, operationName) {
502
+ const document = parse(graphqlContent);
503
+ let selectedOperation = null;
504
+ const fragments = {};
505
+ const usedFragments = /* @__PURE__ */ new Set();
506
+ visit(document, {
507
+ OperationDefinition(node) {
508
+ if (node.name?.value === operationName) {
509
+ selectedOperation = node;
510
+ }
511
+ },
512
+ FragmentDefinition(node) {
513
+ fragments[node.name.value] = node;
514
+ }
515
+ });
516
+ if (!selectedOperation) {
517
+ throw new Error(`Operation named "${operationName}" not found`);
518
+ }
519
+ visit(selectedOperation, {
520
+ FragmentSpread(node) {
521
+ usedFragments.add(node.name.value);
522
+ }
523
+ });
524
+ let hasNewFragments = true;
525
+ while (hasNewFragments) {
526
+ hasNewFragments = false;
527
+ for (const fragmentName of usedFragments) {
528
+ visit(fragments[fragmentName], {
529
+ FragmentSpread(node) {
530
+ if (!usedFragments.has(node.name.value)) {
531
+ usedFragments.add(node.name.value);
532
+ hasNewFragments = true;
533
+ }
534
+ }
535
+ });
536
+ }
493
537
  }
494
- return documents;
538
+ return {
539
+ kind: Kind.DOCUMENT,
540
+ definitions: [
541
+ selectedOperation,
542
+ ...Array.from(usedFragments).map(
543
+ (fragmentName) => fragments[fragmentName]
544
+ )
545
+ ]
546
+ };
495
547
  }
496
548
  async function generate(options, schemaPath, resolver, srcDir, logEverything = false) {
497
549
  const schemaContent = await promises.readFile(schemaPath).then((v) => v.toString());
@@ -502,17 +554,55 @@ async function generate(options, schemaPath, resolver, srcDir, logEverything = f
502
554
  resolver
503
555
  );
504
556
  const validated = validateDocuments(schema, documents, srcDir);
557
+ const extracted = validated.filter(
558
+ (v) => !v.operation
559
+ );
560
+ validated.forEach((v) => {
561
+ if (v.isValid) {
562
+ try {
563
+ const node = parse(v.content);
564
+ oldVisit(node, {
565
+ enter: {
566
+ OperationDefinition: (node2) => {
567
+ if (node2.name?.value && node2.loc?.source && (node2.operation === "query" || node2.operation === "mutation")) {
568
+ const document = { ...v };
569
+ const cleaned = cleanGraphqlDocument(
570
+ node2.loc.source.body,
571
+ node2.name.value
572
+ );
573
+ const errors = validateGraphQlDocuments(schema, [cleaned]);
574
+ document.errors = document.errors || [];
575
+ document.errors.push(...errors);
576
+ document.isValid = !document.errors.length;
577
+ document.name = node2.name.value;
578
+ document.operation = node2.operation;
579
+ document.content = print(cleaned);
580
+ document.id = [
581
+ document.operation,
582
+ document.name,
583
+ document.filename
584
+ ].filter(Boolean).join("_");
585
+ extracted.push(document);
586
+ }
587
+ }
588
+ }
589
+ });
590
+ } catch (e) {
591
+ logger.error(e);
592
+ }
593
+ }
594
+ });
505
595
  const templates = await generateTemplates(
506
- validated.filter((v) => v.isValid).map((v) => v.content),
596
+ extracted.filter((v) => v.isValid).map((v) => v.content),
507
597
  schemaPath,
508
598
  options
509
599
  );
510
- const hasErrors = validated.some((v) => !v.isValid);
600
+ const hasErrors = extracted.some((v) => !v.isValid);
511
601
  if (hasErrors || logEverything) {
512
602
  const table = new Table({
513
603
  head: ["Operation", "Name", "File", "Errors"].map((v) => chalk.white(v))
514
604
  });
515
- validated.forEach((document) => {
605
+ extracted.forEach((document) => {
516
606
  if (logEverything || !document.isValid) {
517
607
  table.push(
518
608
  [
@@ -539,7 +629,7 @@ async function generate(options, schemaPath, resolver, srcDir, logEverything = f
539
629
  return a.filename.localeCompare(b.filename);
540
630
  }),
541
631
  hasErrors,
542
- documents: validated.sort((a, b) => {
632
+ documents: extracted.sort((a, b) => {
543
633
  const nameA = a.name || "";
544
634
  const nameB = b.name || "";
545
635
  return nameA.localeCompare(nameB);
@@ -741,9 +831,6 @@ declare module '#graphql-documents' {
741
831
  `;
742
832
  }
743
833
  });
744
- nuxt.options.alias["#graphql-server-options"] = moduleResolver(
745
- "runtime/serverOptions"
746
- );
747
834
  const extensions = ["js", "mjs", "ts"];
748
835
  const resolvedPath = "~/app/graphqlMiddleware.serverOptions".replace(/^(~~|@@)/, nuxt.options.rootDir).replace(/^(~|@)/, nuxt.options.srcDir);
749
836
  const template = (() => {
@@ -1,3 +1,2 @@
1
- import { NuxtApp } from 'nuxt/app';
2
1
  import { GraphqlMiddlewareState } from './../../types';
3
- export declare const useGraphqlState: (providedApp?: NuxtApp) => GraphqlMiddlewareState | null;
2
+ export declare const useGraphqlState: (providedApp?: any) => GraphqlMiddlewareState | null;
@@ -1,4 +1,4 @@
1
- import { useNuxtApp } from "nuxt/app";
1
+ import { useNuxtApp } from "#app";
2
2
  export const useGraphqlState = function(providedApp) {
3
3
  try {
4
4
  const app = providedApp || useNuxtApp();
@@ -1,5 +1,5 @@
1
1
  /**
2
2
  * Create and provide the state singleton for the composables.
3
3
  */
4
- declare const _default: import("nuxt/app").Plugin<Record<string, unknown>>;
4
+ declare const _default: import("nuxt/app").Plugin<Record<string, unknown>> & import("nuxt/app").ObjectPlugin<Record<string, unknown>>;
5
5
  export default _default;
@@ -1,2 +1,2 @@
1
- declare const _default: import("h3").EventHandler<string>;
1
+ declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, string>;
2
2
  export default _default;
@@ -1,2 +1,2 @@
1
- declare const _default: import("h3").EventHandler<any>;
1
+ declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
2
2
  export default _default;
@@ -1,4 +1,4 @@
1
- import { defineEventHandler, getQuery, getMethod, readBody } from "h3";
1
+ import { defineEventHandler, getQuery, readBody } from "h3";
2
2
  import {
3
3
  queryParamToVariables,
4
4
  getEndpoint,
@@ -12,38 +12,59 @@ import { documents } from "#graphql-documents";
12
12
  import serverOptions from "#graphql-middleware-server-options-build";
13
13
  import { useRuntimeConfig } from "#imports";
14
14
  export default defineEventHandler(async (event) => {
15
- const method = getMethod(event);
15
+ const method = event.method;
16
16
  const operation = event.context?.params?.operation;
17
- const name = event.context?.params?.name;
18
- validateRequest(method, operation, name, documents);
19
- const query = documents[operation][name];
17
+ const operationName = event.context?.params?.name;
18
+ validateRequest(method, operation, operationName, documents);
19
+ const operationDocument = documents[operation][operationName];
20
+ const variables = operation === GraphqlMiddlewareOperation.Query ? queryParamToVariables(getQuery(event)) : await readBody(event);
21
+ if (serverOptions.doGraphqlRequest) {
22
+ return serverOptions.doGraphqlRequest({
23
+ event,
24
+ operation,
25
+ operationName,
26
+ operationDocument,
27
+ variables
28
+ });
29
+ }
20
30
  const runtimeConfig = useRuntimeConfig().graphqlMiddleware;
21
31
  const endpoint = await getEndpoint(
22
32
  runtimeConfig,
23
33
  serverOptions,
24
34
  event,
25
35
  operation,
26
- name
36
+ operationName
27
37
  );
28
38
  const fetchOptions = await getFetchOptions(
29
39
  serverOptions,
30
40
  event,
31
41
  operation,
32
- name
42
+ operationName
33
43
  );
34
- const variables = operation === GraphqlMiddlewareOperation.Query ? queryParamToVariables(getQuery(event)) : await readBody(event);
35
44
  return $fetch.raw(endpoint, {
36
45
  // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
37
46
  method: "POST",
38
47
  body: {
39
- query,
48
+ query: operationDocument,
40
49
  variables,
41
- operationName: name
50
+ operationName
42
51
  },
43
52
  ...fetchOptions
44
53
  }).then((response) => {
45
- return onServerResponse(serverOptions, event, response, operation, name);
54
+ return onServerResponse(
55
+ serverOptions,
56
+ event,
57
+ response,
58
+ operation,
59
+ operationName
60
+ );
46
61
  }).catch((error) => {
47
- return onServerError(serverOptions, event, error, operation, name);
62
+ return onServerError(
63
+ serverOptions,
64
+ event,
65
+ error,
66
+ operation,
67
+ operationName
68
+ );
48
69
  });
49
70
  });
@@ -0,0 +1,17 @@
1
+
2
+ import { ModuleOptions, ModuleHooks } from './module'
3
+
4
+ declare module '@nuxt/schema' {
5
+ interface NuxtConfig { ['graphqlMiddleware']?: Partial<ModuleOptions> }
6
+ interface NuxtOptions { ['graphqlMiddleware']?: ModuleOptions }
7
+ interface NuxtHooks extends ModuleHooks {}
8
+ }
9
+
10
+ declare module 'nuxt/schema' {
11
+ interface NuxtConfig { ['graphqlMiddleware']?: Partial<ModuleOptions> }
12
+ interface NuxtOptions { ['graphqlMiddleware']?: ModuleOptions }
13
+ interface NuxtHooks extends ModuleHooks {}
14
+ }
15
+
16
+
17
+ export { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, default } from './module'
package/dist/types.d.ts CHANGED
@@ -7,5 +7,11 @@ declare module '@nuxt/schema' {
7
7
  interface NuxtHooks extends ModuleHooks {}
8
8
  }
9
9
 
10
+ declare module 'nuxt/schema' {
11
+ interface NuxtConfig { ['graphqlMiddleware']?: Partial<ModuleOptions> }
12
+ interface NuxtOptions { ['graphqlMiddleware']?: ModuleOptions }
13
+ interface NuxtHooks extends ModuleHooks {}
14
+ }
15
+
10
16
 
11
17
  export { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, default } from './module'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
- "version": "4.0.0-beta.6",
3
+ "version": "4.0.0-beta.8",
4
4
  "description": "Module to perform GraphQL requests as a server middleware.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,7 +12,8 @@
12
12
  ".": {
13
13
  "import": "./dist/module.mjs",
14
14
  "require": "./dist/module.cjs"
15
- }
15
+ },
16
+ "./dist/runtime/serverOptions": "./dist/runtime/serverOptions/index.mjs"
16
17
  },
17
18
  "main": "./dist/module.cjs",
18
19
  "types": "./dist/types.d.ts",
@@ -35,45 +36,45 @@
35
36
  "cypress:open": "cypress open --e2e",
36
37
  "test": "vitest",
37
38
  "test:ci": "vitest run",
38
- "test:coverage": "vitest run --coverage"
39
+ "test:coverage": "vitest run --coverage",
40
+ "prettier": "prettier --check .",
41
+ "prettier:fix": "prettier --write ."
39
42
  },
40
43
  "dependencies": {
41
- "@graphql-codegen/cli": "^3.3.1",
42
- "@graphql-codegen/schema-ast": "^3.0.1",
43
- "@graphql-codegen/typescript": "^3.0.4",
44
- "@graphql-codegen/typescript-generic-sdk": "^3.1.0",
45
- "@graphql-codegen/typescript-operations": "^3.0.4",
44
+ "@graphql-codegen/cli": "^5.0.0",
45
+ "@graphql-codegen/schema-ast": "^4.0.0",
46
+ "@graphql-codegen/typescript": "^4.0.1",
47
+ "@graphql-codegen/typescript-generic-sdk": "^4.0.0",
48
+ "@graphql-codegen/typescript-operations": "^4.0.1",
46
49
  "@graphql-fragment-import/lib": "^3.0.0",
47
- "@graphql-tools/utils": "^9.2.1",
48
- "@nuxt/devtools-kit": "0.4.5",
49
- "@nuxt/kit": "^3.4.3",
50
+ "@graphql-tools/utils": "^10.0.6",
51
+ "@nuxt/devtools-kit": "0.8.5",
52
+ "@nuxt/kit": "^3.7.4",
50
53
  "cli-table": "^0.3.11",
51
- "inquirer": "^9.2.0",
52
- "minisearch": "^6.0.1"
54
+ "inquirer": "^9.2.11",
55
+ "minisearch": "^6.1.0"
53
56
  },
54
57
  "devDependencies": {
55
- "@iconify-json/carbon": "^1.1.16",
56
- "@nuxt/devtools": "^0.4.5",
57
- "@nuxt/devtools-ui-kit": "0.4.5",
58
- "@nuxt/module-builder": "^0.3.0",
59
- "@nuxt/schema": "^3.4.3",
60
- "@nuxt/test-utils": "^3.4.3",
61
- "@nuxtjs/eslint-config-typescript": "^12.0.0",
62
- "@types/capture-console": "^1.0.1",
63
- "@types/cli-table": "^0.3.1",
58
+ "@iconify-json/carbon": "^1.1.21",
59
+ "@nuxt/devtools": "^0.8.5",
60
+ "@nuxt/devtools-ui-kit": "0.8.5",
61
+ "@nuxt/module-builder": "^0.5.2",
62
+ "@nuxt/schema": "^3.7.4",
63
+ "@nuxtjs/eslint-config-typescript": "^12.1.0",
64
+ "@types/capture-console": "^1.0.2",
65
+ "@types/cli-table": "^0.3.2",
64
66
  "@types/inquirer": "^9.0.3",
65
- "@vitest/coverage-c8": "^0.30.1",
66
- "cypress": "^12.11.0",
67
- "eslint": "^8.39.0",
68
- "eslint-config-prettier": "^8.8.0",
69
- "eslint-plugin-prettier": "^4.2.1",
67
+ "cypress": "^13.3.0",
68
+ "eslint": "^8.50.0",
69
+ "eslint-config-prettier": "^9.0.0",
70
+ "eslint-plugin-prettier": "^5.0.0",
70
71
  "jsdoc-to-markdown": "^8.0.0",
71
- "nuxt": "^3.4.3",
72
- "prettier": "^2.8.8",
73
- "strip-ansi": "^7.0.1",
74
- "typedoc": "^0.24.6",
75
- "typedoc-plugin-markdown": "^3.15.3",
76
- "vitepress": "^1.0.0-alpha.75",
77
- "vitest": "^0.30.1"
72
+ "nuxt": "^3.7.4",
73
+ "prettier": "^3.0.3",
74
+ "strip-ansi": "^7.1.0",
75
+ "typedoc": "^0.25.1",
76
+ "typedoc-plugin-markdown": "^3.16.0",
77
+ "vitepress": "^1.0.0-rc.20",
78
+ "vitest": "^0.34.6"
78
79
  }
79
80
  }