nuxt-graphql-middleware 5.1.1 → 5.2.1

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 (40) hide show
  1. package/dist/client/200.html +1 -11
  2. package/dist/client/404.html +1 -11
  3. package/dist/client/_nuxt/CKZE-Gmz.js +2 -0
  4. package/dist/client/_nuxt/CLvFsrJf.js +1 -0
  5. package/dist/client/_nuxt/CYCWsMRt.js +1 -0
  6. package/dist/client/_nuxt/DKGPmQRi.js +1 -0
  7. package/dist/client/_nuxt/DPP_zJIh.js +25 -0
  8. package/dist/client/_nuxt/builds/latest.json +1 -1
  9. package/dist/client/_nuxt/builds/meta/3986797b-ae4e-4a02-94e5-5e33c2f09ce7.json +1 -0
  10. package/dist/client/_nuxt/entry.DUAtNXP_.css +1 -0
  11. package/dist/client/_nuxt/error-404.DlVPZ4GE.css +1 -0
  12. package/dist/client/_nuxt/error-500.DjyirMQI.css +1 -0
  13. package/dist/client/_nuxt/index.B-PWu_0C.css +1 -0
  14. package/dist/client/index.html +1 -11
  15. package/dist/module.d.mts +1 -1
  16. package/dist/module.json +1 -1
  17. package/dist/module.mjs +20 -8
  18. package/dist/runtime/composables/nuxtApp.d.ts +5 -1
  19. package/dist/runtime/composables/nuxtApp.js +5 -9
  20. package/dist/runtime/composables/useAsyncGraphqlQuery.d.ts +1 -2
  21. package/dist/runtime/composables/useAsyncGraphqlQuery.js +10 -2
  22. package/dist/runtime/helpers/ClientCache.d.ts +3 -1
  23. package/dist/runtime/helpers/ClientCache.js +23 -5
  24. package/dist/runtime/helpers/composables.d.ts +0 -4
  25. package/dist/runtime/helpers/composables.js +0 -8
  26. package/dist/runtime/helpers/queryEncoding.d.ts +5 -1
  27. package/dist/runtime/helpers/queryEncoding.js +10 -2
  28. package/dist/shared/{nuxt-graphql-middleware.-BeiPV4H.d.mts → nuxt-graphql-middleware.ct2xvPoD.d.mts} +4 -1
  29. package/dist/utils.d.mts +1 -1
  30. package/package.json +10 -8
  31. package/dist/client/_nuxt/B-BZSpkz.js +0 -25
  32. package/dist/client/_nuxt/C6_BMIRJ.js +0 -1
  33. package/dist/client/_nuxt/CFKT0oF2.js +0 -1
  34. package/dist/client/_nuxt/CrwW1KlQ.js +0 -2
  35. package/dist/client/_nuxt/CyxO-88q.js +0 -1
  36. package/dist/client/_nuxt/builds/meta/bab648bf-3b57-4a37-bfa7-d3789fe7395f.json +0 -1
  37. package/dist/client/_nuxt/entry.BBgLZ1Jk.css +0 -1
  38. package/dist/client/_nuxt/error-404.Bbd2eCoc.css +0 -1
  39. package/dist/client/_nuxt/error-500.Cd2cwFc3.css +0 -1
  40. package/dist/client/_nuxt/index.DGEN-H8t.css +0 -1
package/dist/module.mjs CHANGED
@@ -20,7 +20,7 @@ import { existsSync as existsSync$1 } from 'fs';
20
20
  import { onDevToolsInitialized, extendServerRpc } from '@nuxt/devtools-kit';
21
21
 
22
22
  const name = "nuxt-graphql-middleware";
23
- const version = "5.1.1";
23
+ const version = "5.2.1";
24
24
 
25
25
  const logger = useLogger("nuxt-graphql-middleware");
26
26
  const defaultOptions = {
@@ -820,7 +820,11 @@ ${color.cyan(S_BAR_END)}
820
820
  class ModuleHelper {
821
821
  constructor(nuxt, moduleUrl, options) {
822
822
  this.nuxt = nuxt;
823
- const mergedOptions = defu({}, options, defaultOptions);
823
+ const mergedOptions = defu(
824
+ {},
825
+ options,
826
+ defaultOptions
827
+ );
824
828
  if (!mergedOptions.autoImportPatterns) {
825
829
  mergedOptions.autoImportPatterns = [
826
830
  "~~/**/*.{gql,graphql}",
@@ -828,6 +832,15 @@ class ModuleHelper {
828
832
  "!node_modules"
829
833
  ];
830
834
  }
835
+ if (!mergedOptions.graphqlEndpoint) {
836
+ const graphqlEndpoint = process.env.NUXT_GRAPHQL_MIDDLEWARE_GRAPHQL_ENDPOINT;
837
+ if (!graphqlEndpoint) {
838
+ throw new Error(
839
+ 'Missing GraphQL endpoint in module build. Please either provide the endpoint via "graphqlMiddleware.graphqlEndpoint" in nuxt.config.ts or via "NUXT_GRAPHQL_MIDDLEWARE_GRAPHQL_ENDPOINT" environment variable.'
840
+ );
841
+ }
842
+ mergedOptions.graphqlEndpoint = graphqlEndpoint;
843
+ }
831
844
  const layerAliases = nuxt.options._layers.map((layer) => {
832
845
  return {
833
846
  "~~": layer.config.rootDir,
@@ -1347,9 +1360,7 @@ const OperationVariables = defineGeneratorTemplate(
1347
1360
  const operations = output.getCollectedOperations().reduce((acc, collectedOperation) => {
1348
1361
  const node = collectedOperation.node;
1349
1362
  const operationName = collectedOperation.graphqlName;
1350
- const variables = (node.variableDefinitions || []).map(
1351
- (v) => v.variable.name.value
1352
- );
1363
+ const variables = (node.variableDefinitions || []).map((v) => v.variable.name.value).sort();
1353
1364
  acc[operationName] = variables;
1354
1365
  return acc;
1355
1366
  }, {});
@@ -1403,8 +1414,9 @@ export { serverOptions }
1403
1414
  import type { GraphqlMiddlewareServerOptions } from '${helper.paths.runtimeTypes}'
1404
1415
  import serverOptionsImport from '${resolvedPathRelative}'
1405
1416
 
1406
- export type GraphqlResponseAdditions =
1407
- typeof serverOptionsImport extends GraphqlMiddlewareServerOptions<infer R, any, any> ? R : {}
1417
+ type AdditionsFromServerOptions = typeof serverOptionsImport extends GraphqlMiddlewareServerOptions<infer R, any, any> ? R : {}
1418
+
1419
+ export type GraphqlResponseAdditions = Omit<AdditionsFromServerOptions, 'data' | 'errors'>
1408
1420
 
1409
1421
  declare export const serverOptions: GraphqlMiddlewareServerOptions
1410
1422
  `;
@@ -1732,7 +1744,7 @@ const module = defineNuxtModule({
1732
1744
  clientCacheMaxSize: helper.options.clientCache?.maxSize ?? 100
1733
1745
  };
1734
1746
  nuxt.options.runtimeConfig.graphqlMiddleware = {
1735
- graphqlEndpoint: helper.options.graphqlEndpoint || ""
1747
+ graphqlEndpoint: helper.options.graphqlEndpoint
1736
1748
  };
1737
1749
  helper.transpile(fileURLToPath(new URL("./runtime", import.meta.url)));
1738
1750
  helper.inlineNitroExternals(helper.resolvers.module.resolve("./runtime"));
@@ -30,7 +30,11 @@ overrideClientContext: Record<string, any>,
30
30
  /**
31
31
  * The cache options set on the composable.
32
32
  */
33
- cacheOptions: RequestCacheOptions): Promise<GraphqlResponse<T>>;
33
+ cacheOptions: RequestCacheOptions,
34
+ /**
35
+ * The async data key.
36
+ */
37
+ asyncDataKey?: string): Promise<GraphqlResponse<T>>;
34
38
  declare module '#app' {
35
39
  interface NuxtApp {
36
40
  $graphqlCache?: GraphqlMiddlewareCache;
@@ -7,14 +7,10 @@ import {
7
7
  clientCacheEnabledAtBuild,
8
8
  importMetaClient
9
9
  } from "#nuxt-graphql-middleware/config";
10
- import { encodeVariables } from "../helpers/queryEncoding.js";
11
- import {
12
- encodeContext,
13
- getOrCreateClientCache,
14
- sortQueryParams
15
- } from "../helpers/composables.js";
10
+ import { encodeVariables, sortQueryParams } from "../helpers/queryEncoding.js";
11
+ import { encodeContext, getOrCreateClientCache } from "../helpers/composables.js";
16
12
  import { OPERATION_HASH_PREFIX } from "../settings/index.js";
17
- export function performRequest(operation, operationName, variablesOrBody, overrideFetchOptions, globalClientContext, overrideClientContext, cacheOptions) {
13
+ export function performRequest(operation, operationName, variablesOrBody, overrideFetchOptions, globalClientContext, overrideClientContext, cacheOptions, asyncDataKey) {
18
14
  const state = useGraphqlState();
19
15
  const app = useNuxtApp();
20
16
  const config = useAppConfig();
@@ -89,7 +85,7 @@ export function performRequest(operation, operationName, variablesOrBody, overri
89
85
  }
90
86
  )
91
87
  ).then((v) => {
92
- if (import.meta.dev && v.errors?.length) {
88
+ if (import.meta.dev && v?.errors?.length) {
93
89
  app.callHook("nuxt-graphql-middleware:errors", {
94
90
  operation,
95
91
  operationName,
@@ -103,7 +99,7 @@ export function performRequest(operation, operationName, variablesOrBody, overri
103
99
  });
104
100
  });
105
101
  if (importMetaClient && cacheKey && app.$graphqlCache && clientCacheEnabledAtBuild) {
106
- app.$graphqlCache.set(cacheKey, promise);
102
+ app.$graphqlCache.set(cacheKey, promise, asyncDataKey);
107
103
  }
108
104
  return promise;
109
105
  }
@@ -5,9 +5,8 @@ import { type GraphqlClientContext } from '#nuxt-graphql-middleware/client-optio
5
5
  import type { GraphqlResponse } from '#nuxt-graphql-middleware/response';
6
6
  import type { RequestCacheOptions } from './../types.js';
7
7
  import type { AsyncData, AsyncDataOptions, NuxtError } from '#app';
8
- import type { DefaultAsyncDataValue } from 'nuxt/app/defaults';
9
8
  import type { Query } from '#nuxt-graphql-middleware/operation-types';
10
- type AsyncGraphqlQueryOptions<FetchOptions, ResT, DataT = ResT, PickKeys extends KeysOf<DataT> = KeysOf<DataT>, DefaultT = DefaultAsyncDataValue> = AsyncDataOptions<ResT, DataT, PickKeys, DefaultT> & {
9
+ type AsyncGraphqlQueryOptions<FetchOptions, ResT, DataT = ResT, PickKeys extends KeysOf<DataT> = KeysOf<DataT>, DefaultT = undefined> = AsyncDataOptions<ResT, DataT, PickKeys, DefaultT> & {
11
10
  /**
12
11
  * Control how the GraphQL response can be cached.
13
12
  */
@@ -16,6 +16,7 @@ export function useAsyncGraphqlQuery(name, ...args) {
16
16
  const vars = isRef(variables) ? variables.value : variables;
17
17
  return `useAsyncGraphqlQuery:${name}:${hash(vars)}`;
18
18
  });
19
+ let prevKey = "";
19
20
  const config = useAppConfig();
20
21
  const app = useNuxtApp();
21
22
  if (importMetaClient) {
@@ -39,13 +40,19 @@ export function useAsyncGraphqlQuery(name, ...args) {
39
40
  asyncDataOptions.getCachedData = function(key, app2, ctx) {
40
41
  if (ctx.cause === "initial") {
41
42
  return app2.payload.data[key] ?? app2.$graphqlCache?.get(key);
43
+ } else if (ctx.cause === "refresh:manual" || ctx.cause === "refresh:hook") {
44
+ if (app2.$graphqlCache) {
45
+ app2.$graphqlCache.purgeAsyncDataKey(key);
46
+ app2.$graphqlCache.purgeAsyncDataKey(prevKey);
47
+ }
42
48
  }
43
49
  };
44
50
  }
45
51
  }
46
52
  const result = useAsyncData(
47
- asyncDataKey,
53
+ asyncDataKey.value,
48
54
  () => {
55
+ prevKey = asyncDataKey.value;
49
56
  const globalClientContext = clientOptions && clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
50
57
  return performRequest(
51
58
  "query",
@@ -54,7 +61,8 @@ export function useAsyncGraphqlQuery(name, ...args) {
54
61
  asyncDataOptions.fetchOptions || {},
55
62
  globalClientContext,
56
63
  asyncDataOptions.clientContext || {},
57
- asyncDataOptions.graphqlCaching || {}
64
+ asyncDataOptions.graphqlCaching || {},
65
+ asyncDataKey.value
58
66
  );
59
67
  },
60
68
  asyncDataOptions
@@ -6,11 +6,13 @@
6
6
  */
7
7
  export declare class GraphqlMiddlewareCache {
8
8
  cache: Record<string, unknown>;
9
+ asyncDataKeyMap: Record<string, string[]>;
9
10
  keys: string[];
10
11
  maxSize: number;
11
12
  constructor(maxSize?: number);
12
- set(key: string, value: unknown): void;
13
+ set(key: string, value: unknown, asyncDataKey?: string): void;
13
14
  get<T>(key: string): T | undefined;
14
15
  purge(): void;
16
+ purgeAsyncDataKey(asyncDataKey: string): void;
15
17
  remove(key: string): boolean;
16
18
  }
@@ -1,13 +1,12 @@
1
1
  export class GraphqlMiddlewareCache {
2
- cache;
3
- keys;
2
+ cache = {};
3
+ asyncDataKeyMap = {};
4
+ keys = [];
4
5
  maxSize;
5
6
  constructor(maxSize = 100) {
6
- this.cache = {};
7
- this.keys = [];
8
7
  this.maxSize = maxSize;
9
8
  }
10
- set(key, value) {
9
+ set(key, value, asyncDataKey) {
11
10
  if (Object.prototype.hasOwnProperty.call(this.cache, key)) {
12
11
  const index = this.keys.indexOf(key);
13
12
  if (index > -1) {
@@ -17,10 +16,20 @@ export class GraphqlMiddlewareCache {
17
16
  const oldestKey = this.keys.shift();
18
17
  if (oldestKey !== void 0) {
19
18
  delete this.cache[oldestKey];
19
+ delete this.asyncDataKeyMap[oldestKey];
20
20
  }
21
21
  }
22
22
  this.cache[key] = value;
23
23
  this.keys.push(key);
24
+ if (asyncDataKey) {
25
+ if (!Object.prototype.hasOwnProperty.call(
26
+ this.asyncDataKeyMap,
27
+ asyncDataKey
28
+ )) {
29
+ this.asyncDataKeyMap[asyncDataKey] = [];
30
+ }
31
+ this.asyncDataKeyMap[asyncDataKey].push(key);
32
+ }
24
33
  }
25
34
  get(key) {
26
35
  const value = this.cache[key];
@@ -37,6 +46,15 @@ export class GraphqlMiddlewareCache {
37
46
  purge() {
38
47
  this.cache = {};
39
48
  this.keys = [];
49
+ this.asyncDataKeyMap = {};
50
+ }
51
+ purgeAsyncDataKey(asyncDataKey) {
52
+ const keys = this.asyncDataKeyMap[asyncDataKey];
53
+ if (keys && keys.length) {
54
+ keys.forEach((key) => {
55
+ this.remove(key);
56
+ });
57
+ }
40
58
  }
41
59
  remove(key) {
42
60
  if (Object.prototype.hasOwnProperty.call(this.cache, key)) {
@@ -41,8 +41,4 @@ export type MutationObjectArgs<K extends keyof Mutation, M extends Mutation[K] =
41
41
  export type PickFrom<T, K extends Array<string>> = T extends Array<any> ? T : T extends Record<string, any> ? keyof T extends K[number] ? T : K[number] extends never ? T : Pick<T, K[number]> : T;
42
42
  export type KeysOf<T> = Array<T extends T ? (keyof T extends string ? keyof T : never) : never>;
43
43
  export declare function encodeContext(context: Record<string, string | null | undefined>): Record<string, string>;
44
- /**
45
- * Sort an object defining query params alphabetically.
46
- */
47
- export declare function sortQueryParams(obj: Record<string, string>): Record<string, string>;
48
44
  export declare function getOrCreateClientCache(app: NuxtApp, config: AppConfig): GraphqlMiddlewareCache | undefined;
@@ -15,14 +15,6 @@ export function encodeContext(context) {
15
15
  {}
16
16
  );
17
17
  }
18
- export function sortQueryParams(obj) {
19
- const sortedKeys = Object.keys(obj).sort();
20
- const sortedObj = {};
21
- for (const key of sortedKeys) {
22
- sortedObj[key] = obj[key];
23
- }
24
- return sortedObj;
25
- }
26
18
  export function getOrCreateClientCache(app, config) {
27
19
  if (importMetaServer || !clientCacheEnabledAtBuild) {
28
20
  return;
@@ -8,4 +8,8 @@ export declare function encodeVariables(variables?: Record<string, any> | null):
8
8
  * In complex cases, the entire variables are sent as a JSON encoded string:
9
9
  * ?__variables=%7B%22foobar%22:%7B%22path%22:%22%22%7D%7D
10
10
  */
11
- export declare function decodeVariables(query: Record<string, any>, validKeys?: string[]): any;
11
+ export declare function decodeVariables(query: Record<string, any>, validKeys?: string[]): Record<string, any>;
12
+ /**
13
+ * Sort an object defining query params alphabetically.
14
+ */
15
+ export declare function sortQueryParams(obj: Record<string, string>): Record<string, string>;
@@ -49,7 +49,7 @@ function filterValidKeys(validKeys, obj) {
49
49
  export function decodeVariables(query, validKeys) {
50
50
  try {
51
51
  if (query.__variables && typeof query.__variables === "string") {
52
- return JSON.parse(query.__variables);
52
+ return sortQueryParams(JSON.parse(query.__variables));
53
53
  }
54
54
  } catch {
55
55
  }
@@ -85,5 +85,13 @@ export function decodeVariables(query, validKeys) {
85
85
  result[key] = query[key];
86
86
  }
87
87
  }
88
- return result;
88
+ return sortQueryParams(result);
89
+ }
90
+ export function sortQueryParams(obj) {
91
+ const sortedKeys = Object.keys(obj).sort();
92
+ const sortedObj = {};
93
+ for (const key of sortedKeys) {
94
+ sortedObj[key] = obj[key];
95
+ }
96
+ return sortedObj;
89
97
  }
@@ -95,11 +95,14 @@ interface ModuleOptions {
95
95
  /**
96
96
  * The URL of the GraphQL server.
97
97
  *
98
+ * If not provided, the module will use the NUXT_GRAPHQL_MIDDLEWARE_GRAPHQL_ENDPOINT
99
+ * environment variable during dev mode.
100
+ *
98
101
  * For the runtime execution you can provide a method that determines the endpoint
99
102
  * during runtime. See the server/graphqlMiddleware.serverOptions.ts documentation
100
103
  * for more information.
101
104
  */
102
- graphqlEndpoint: string;
105
+ graphqlEndpoint?: string;
103
106
  /**
104
107
  * Download the GraphQL schema and store it on disk.
105
108
  *
package/dist/utils.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { M as ModuleContext } from './shared/nuxt-graphql-middleware.-BeiPV4H.mjs';
1
+ import { M as ModuleContext } from './shared/nuxt-graphql-middleware.ct2xvPoD.mjs';
2
2
  import 'graphql';
3
3
  import 'nuxt/schema';
4
4
  import '@nuxt/kit';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
- "version": "5.1.1",
3
+ "version": "5.2.1",
4
4
  "description": "Module to perform GraphQL requests as a server middleware.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -72,26 +72,27 @@
72
72
  "@graphql-codegen/cli": "^5.0.5",
73
73
  "@graphql-codegen/schema-ast": "^4.1.0",
74
74
  "@graphql-tools/utils": "^10.8.6",
75
- "@nuxt/devtools-kit": "^2.3.1",
76
- "graphql-typescript-deluxe": "^0.0.14",
75
+ "@nuxt/devtools-kit": "^2.6.2",
76
+ "graphql-typescript-deluxe": "^0.0.15",
77
77
  "minisearch": "^7.1.2",
78
78
  "picocolors": "^1.1.1"
79
79
  },
80
80
  "devDependencies": {
81
81
  "@iconify-json/carbon": "^1.2.8",
82
82
  "@nuxt/devtools": "^2.4.0",
83
- "@nuxt/devtools-ui-kit": "^2.4.0",
83
+ "@nuxt/devtools-ui-kit": "^2.6.2",
84
84
  "@nuxt/eslint": "^1.2.0",
85
- "@nuxt/kit": "^3.17.2",
85
+ "@nuxt/kit": "^4.0.3",
86
86
  "@nuxt/module-builder": "^1.0.1",
87
- "@nuxt/schema": "^3.17.2",
87
+ "@nuxt/schema": "^4.0.3",
88
88
  "@types/micromatch": "^4.0.9",
89
89
  "cypress": "^13.12.0",
90
+ "cypress-wait-until": "^3.0.2",
90
91
  "eslint": "^9.23.0",
91
92
  "eslint-config-prettier": "^10.1.1",
92
93
  "eslint-plugin-prettier": "^5.2.3",
93
94
  "mermaid": "^11.5.0",
94
- "nuxt": "^3.17.2",
95
+ "nuxt": "^4.0.3",
95
96
  "postcss": "^8.5.3",
96
97
  "postcss-cli": "^11.0.1",
97
98
  "postcss-import": "^16.1.0",
@@ -106,6 +107,7 @@
106
107
  "typedoc-vitepress-theme": "^1.1.2",
107
108
  "vitepress": "^1.6.3",
108
109
  "vitepress-plugin-mermaid": "^2.0.17",
109
- "vitest": "^1.6.0"
110
+ "vitest": "^1.6.0",
111
+ "vue-tsc": "^2.2.12"
110
112
  }
111
113
  }