nuxt-graphql-middleware 5.0.0 → 5.1.0

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 (44) hide show
  1. package/dist/client/200.html +8 -8
  2. package/dist/client/404.html +8 -8
  3. package/dist/client/_nuxt/{lIgCBhS_.js → CPiV13Ng.js} +1 -1
  4. package/dist/client/_nuxt/CQCiRbEL.js +1 -0
  5. package/dist/client/_nuxt/{CROlboVl.js → CYI2eNUl.js} +1 -1
  6. package/dist/client/_nuxt/UAIVpYSK.js +25 -0
  7. package/dist/client/_nuxt/builds/latest.json +1 -1
  8. package/dist/client/_nuxt/builds/meta/48332277-c4ad-4d6a-8f09-7a58a109c32d.json +1 -0
  9. package/dist/client/_nuxt/error-404.Bbd2eCoc.css +1 -0
  10. package/dist/client/_nuxt/error-500.Cd2cwFc3.css +1 -0
  11. package/dist/client/_nuxt/{FTbv7CO6.js → u1b7LyOw.js} +1 -1
  12. package/dist/client/index.html +8 -8
  13. package/dist/module.d.mts +1 -1
  14. package/dist/module.json +2 -2
  15. package/dist/module.mjs +141 -46
  16. package/dist/runtime/composables/nuxtApp.d.ts +30 -2
  17. package/dist/runtime/composables/nuxtApp.js +66 -24
  18. package/dist/runtime/composables/useAsyncGraphqlQuery.js +28 -25
  19. package/dist/runtime/composables/useGraphqlMutation.d.ts +1 -1
  20. package/dist/runtime/composables/useGraphqlMutation.js +11 -16
  21. package/dist/runtime/composables/useGraphqlQuery.d.ts +1 -1
  22. package/dist/runtime/composables/useGraphqlQuery.js +7 -25
  23. package/dist/runtime/composables/useGraphqlUploadMutation.js +1 -1
  24. package/dist/runtime/helpers/ClientCache.d.ts +1 -0
  25. package/dist/runtime/helpers/ClientCache.js +12 -0
  26. package/dist/runtime/helpers/composables.d.ts +8 -0
  27. package/dist/runtime/helpers/composables.js +27 -0
  28. package/dist/runtime/helpers/index.d.ts +0 -4
  29. package/dist/runtime/helpers/index.js +0 -13
  30. package/dist/runtime/helpers/queryEncoding.d.ts +11 -0
  31. package/dist/runtime/helpers/queryEncoding.js +89 -0
  32. package/dist/runtime/plugins/devMode.js +2 -1
  33. package/dist/runtime/server/api/query.js +5 -7
  34. package/dist/runtime/server/utils/useGraphqlQuery.js +2 -2
  35. package/dist/runtime/settings/index.d.ts +1 -0
  36. package/dist/runtime/settings/index.js +1 -0
  37. package/dist/shared/{nuxt-graphql-middleware.cXfDI4U3.d.mts → nuxt-graphql-middleware.-BeiPV4H.d.mts} +49 -0
  38. package/dist/utils.d.mts +1 -1
  39. package/package.json +7 -7
  40. package/dist/client/_nuxt/BM34SYth.js +0 -1
  41. package/dist/client/_nuxt/D5hBL5aZ.js +0 -25
  42. package/dist/client/_nuxt/builds/meta/83f9fcd5-bd28-4608-b499-05e08fe0f7d0.json +0 -1
  43. package/dist/client/_nuxt/error-404.ehK72JOs.css +0 -1
  44. package/dist/client/_nuxt/error-500._g0akJim.css +0 -1
package/dist/module.mjs CHANGED
@@ -15,11 +15,12 @@ import { defu } from 'defu';
15
15
  import micromatch from 'micromatch';
16
16
  import { ConfirmPrompt } from '@clack/core';
17
17
  import isUnicodeSupported from 'is-unicode-supported';
18
+ import { hash } from 'ohash';
18
19
  import { existsSync as existsSync$1 } from 'fs';
19
20
  import { onDevToolsInitialized, extendServerRpc } from '@nuxt/devtools-kit';
20
21
 
21
22
  const name = "nuxt-graphql-middleware";
22
- const version = "5.0.0";
23
+ const version = "5.1.0";
23
24
 
24
25
  const logger = useLogger("nuxt-graphql-middleware");
25
26
  const defaultOptions = {
@@ -32,7 +33,14 @@ const defaultOptions = {
32
33
  documents: [],
33
34
  devtools: true,
34
35
  errorOverlay: true,
35
- graphqlConfigFilePath: "./graphql.config.ts"
36
+ graphqlConfigFilePath: "./graphql.config.ts",
37
+ experimental: {
38
+ improvedQueryParamEncoding: false
39
+ },
40
+ clientCache: {
41
+ enabled: false,
42
+ maxSize: 100
43
+ }
36
44
  };
37
45
  function validateOptions(options) {
38
46
  if (!options.graphqlEndpoint) {
@@ -816,6 +824,7 @@ class ModuleHelper {
816
824
  if (!mergedOptions.autoImportPatterns) {
817
825
  mergedOptions.autoImportPatterns = [
818
826
  "~~/**/*.{gql,graphql}",
827
+ "~/**/*.{gql,graphql}",
819
828
  "!node_modules"
820
829
  ];
821
830
  }
@@ -1070,7 +1079,7 @@ const ClientOptions = defineStaticTemplate(
1070
1079
  export { clientOptions }
1071
1080
  `;
1072
1081
  }
1073
- return `export const clientOptions = {}`;
1082
+ return `export const clientOptions = undefined`;
1074
1083
  },
1075
1084
  (helper) => {
1076
1085
  if (helper.paths.clientOptions) {
@@ -1080,19 +1089,41 @@ export { clientOptions }
1080
1089
  return `import type { GraphqlClientOptions } from '${helper.paths.runtimeTypes}'
1081
1090
  import clientOptionsImport from '${pathRelative}'
1082
1091
 
1083
- declare export const clientOptions: GraphqlClientOptions
1092
+ declare export const clientOptions: GraphqlClientOptions|undefined
1084
1093
  export type GraphqlClientContext = typeof clientOptionsImport extends GraphqlClientOptions<infer R> ? R : {}
1085
1094
  `;
1086
1095
  }
1087
1096
  return `
1088
1097
  import type { GraphqlClientOptions } from '${helper.paths.runtimeTypes}'
1089
1098
 
1090
- declare export const clientOptions: GraphqlClientOptions
1099
+ declare export const clientOptions: GraphqlClientOptions|undefined
1091
1100
  export type GraphqlClientContext = {}
1092
1101
  `;
1093
1102
  }
1094
1103
  );
1095
1104
 
1105
+ const Config = defineStaticTemplate(
1106
+ { path: "nuxt-graphql-middleware/config" },
1107
+ (helper) => {
1108
+ const experimentalQueryParamEncoding = !!helper.options.experimental.improvedQueryParamEncoding;
1109
+ const clientCacheEnabledAtBuild = !!helper.options.clientCache.enabled;
1110
+ return `
1111
+ export const experimentalQueryParamEncoding = ${JSON.stringify(experimentalQueryParamEncoding)}
1112
+ export const clientCacheEnabledAtBuild = ${JSON.stringify(clientCacheEnabledAtBuild)}
1113
+ export const importMetaServer = import.meta.server
1114
+ export const importMetaClient = import.meta.client
1115
+ `;
1116
+ },
1117
+ () => {
1118
+ return `
1119
+ declare export const experimentalQueryParamEncoding: boolean
1120
+ declare export const clientCacheEnabledAtBuild: boolean
1121
+ declare export const importMetaServer: boolean
1122
+ declare export const importMetaClient: boolean
1123
+ `;
1124
+ }
1125
+ );
1126
+
1096
1127
  const Documents = defineGeneratorTemplate(
1097
1128
  { path: "nuxt-graphql-middleware/documents", virtual: true },
1098
1129
  (output, helper) => {
@@ -1181,6 +1212,43 @@ export function getEndpoint(operation: string, operationName: string): string`;
1181
1212
  }
1182
1213
  );
1183
1214
 
1215
+ const HookDocuments = defineGeneratorTemplate(
1216
+ {
1217
+ path: "nuxt-graphql-middleware/hook-documents.graphql",
1218
+ virtual: false,
1219
+ isFullPath: true
1220
+ },
1221
+ (_output, _helper, collector) => {
1222
+ return collector.getHookDocuments().map((v) => {
1223
+ return `
1224
+ # ${v.identifier}
1225
+ ${v.source}
1226
+ `;
1227
+ }).join("\n\n");
1228
+ },
1229
+ null
1230
+ );
1231
+
1232
+ const HookFiles = defineGeneratorTemplate(
1233
+ {
1234
+ path: "nuxt-graphql-middleware/hook-files",
1235
+ virtual: false
1236
+ },
1237
+ (_output, helper, collector) => {
1238
+ const configPath = helper.resolvers.root.resolve(
1239
+ (helper.options.graphqlConfigFilePath || "").replace(
1240
+ "/graphql.config.ts",
1241
+ ""
1242
+ )
1243
+ );
1244
+ const files = collector.getHookFiles().map((filePath) => {
1245
+ return "./" + relative(configPath, filePath);
1246
+ });
1247
+ return `export const hookFiles = ${JSON.stringify(files, null, 2)}`;
1248
+ },
1249
+ null
1250
+ );
1251
+
1184
1252
  const NitroTypes = defineGeneratorTemplate(
1185
1253
  { path: "nuxt-graphql-middleware/nitro" },
1186
1254
  null,
@@ -1211,6 +1279,46 @@ ${endpoints.sort().join("\n")}
1211
1279
  }
1212
1280
  );
1213
1281
 
1282
+ const OperationHashes = defineGeneratorTemplate(
1283
+ { path: "nuxt-graphql-middleware/operation-hashes", virtual: true },
1284
+ (output, helper) => {
1285
+ if (helper.isDev) {
1286
+ return `export const operationHashes = {}`;
1287
+ }
1288
+ const fragmentHashMap = {};
1289
+ const generatedCode = output.getGeneratedCode();
1290
+ generatedCode.forEach((code) => {
1291
+ if (code.type === "fragment") {
1292
+ const fragmentHash = hash(code.source);
1293
+ if (code.graphqlName) {
1294
+ fragmentHashMap[code.graphqlName] = fragmentHash;
1295
+ }
1296
+ }
1297
+ });
1298
+ const lines = [];
1299
+ generatedCode.forEach((code) => {
1300
+ if (code.type === "operation" && code.graphqlName && code.source) {
1301
+ const source = code.source;
1302
+ const fragments = code.getGraphQLFragmentDependencies().map((fragmentName) => {
1303
+ return fragmentHashMap[fragmentName];
1304
+ }).join("");
1305
+ const sourceHash = hash(source + fragments);
1306
+ lines.push(`"${code.graphqlName}": "${sourceHash.substring(0, 10)}"`);
1307
+ }
1308
+ });
1309
+ return `export const operationHashes = {
1310
+ ${lines.sort().join(",\n ")}
1311
+ }`;
1312
+ },
1313
+ () => {
1314
+ return `
1315
+ declare module '#nuxt-graphql-middleware/operation-hashes' {
1316
+ export const operationHashes: Record<string, string>;
1317
+ }
1318
+ `;
1319
+ }
1320
+ );
1321
+
1214
1322
  const OperationTypesAll = defineGeneratorTemplate(
1215
1323
  { path: "nuxt-graphql-middleware/operation-types" },
1216
1324
  () => `export {}`,
@@ -1233,6 +1341,27 @@ const Operations = defineGeneratorTemplate(
1233
1341
  }
1234
1342
  );
1235
1343
 
1344
+ const OperationVariables = defineGeneratorTemplate(
1345
+ { path: "nuxt-graphql-middleware/operation-variables" },
1346
+ (output) => {
1347
+ const operations = output.getCollectedOperations().reduce((acc, collectedOperation) => {
1348
+ const node = collectedOperation.node;
1349
+ const operationName = collectedOperation.graphqlName;
1350
+ const variables = (node.variableDefinitions || []).map(
1351
+ (v) => v.variable.name.value
1352
+ );
1353
+ acc[operationName] = variables;
1354
+ return acc;
1355
+ }, {});
1356
+ return `export const operationVariables = ${JSON.stringify(operations, null, 2)}`;
1357
+ },
1358
+ () => {
1359
+ return `
1360
+ declare export const operationVariables: Record<string, string[]>
1361
+ `;
1362
+ }
1363
+ );
1364
+
1236
1365
  const Response = defineGeneratorTemplate(
1237
1366
  { path: "nuxt-graphql-middleware/response" },
1238
1367
  null,
@@ -1313,56 +1442,22 @@ export const operationSources = {
1313
1442
  }
1314
1443
  );
1315
1444
 
1316
- const HookDocuments = defineGeneratorTemplate(
1317
- {
1318
- path: "nuxt-graphql-middleware/hook-documents.graphql",
1319
- virtual: false,
1320
- isFullPath: true
1321
- },
1322
- (_output, _helper, collector) => {
1323
- return collector.getHookDocuments().map((v) => {
1324
- return `
1325
- # ${v.identifier}
1326
- ${v.source}
1327
- `;
1328
- }).join("\n\n");
1329
- },
1330
- null
1331
- );
1332
-
1333
- const HookFiles = defineGeneratorTemplate(
1334
- {
1335
- path: "nuxt-graphql-middleware/hook-files",
1336
- virtual: false
1337
- },
1338
- (_output, helper, collector) => {
1339
- const configPath = helper.resolvers.root.resolve(
1340
- (helper.options.graphqlConfigFilePath || "").replace(
1341
- "/graphql.config.ts",
1342
- ""
1343
- )
1344
- );
1345
- const files = collector.getHookFiles().map((filePath) => {
1346
- return "./" + relative(configPath, filePath);
1347
- });
1348
- return `export const hookFiles = ${JSON.stringify(files, null, 2)}`;
1349
- },
1350
- null
1351
- );
1352
-
1353
1445
  const TEMPLATES = [
1354
1446
  ClientOptions,
1447
+ Config,
1355
1448
  Documents,
1356
1449
  GraphqlConfig,
1357
1450
  Helpers,
1451
+ HookDocuments,
1452
+ HookFiles,
1358
1453
  NitroTypes,
1454
+ OperationHashes,
1359
1455
  OperationTypesAll,
1360
1456
  Operations,
1457
+ OperationVariables,
1361
1458
  Response,
1362
1459
  ServerOptions,
1363
- Sources,
1364
- HookDocuments,
1365
- HookFiles
1460
+ Sources
1366
1461
  ];
1367
1462
 
1368
1463
  const DEVTOOLS_UI_ROUTE = "/__nuxt-graphql-middleware";
@@ -1612,7 +1707,7 @@ const module = defineNuxtModule({
1612
1707
  configKey: "graphqlMiddleware",
1613
1708
  version,
1614
1709
  compatibility: {
1615
- nuxt: ">=3.15.0"
1710
+ nuxt: ">=3.17.0"
1616
1711
  }
1617
1712
  },
1618
1713
  defaults: defaultOptions,
@@ -1,8 +1,36 @@
1
1
  import type { FetchOptions } from 'ofetch';
2
- import { GraphqlMiddlewareCache } from '../helpers/ClientCache.js';
2
+ import type { GraphqlMiddlewareCache } from '../helpers/ClientCache.js';
3
3
  import type { GraphqlResponse } from '#nuxt-graphql-middleware/response';
4
4
  import type { RequestCacheOptions } from './../types.js';
5
- export declare function performRequest<T>(operation: string, operationName: string, method: 'get' | 'post', options: FetchOptions, cacheOptions?: RequestCacheOptions): Promise<GraphqlResponse<T>>;
5
+ export declare function performRequest<T>(
6
+ /**
7
+ * The operation type.
8
+ */
9
+ operation: 'query' | 'mutation',
10
+ /**
11
+ * The name of the operation.
12
+ */
13
+ operationName: string,
14
+ /**
15
+ * The operation variables.
16
+ */
17
+ variablesOrBody: Record<string, any>,
18
+ /**
19
+ * Fetch options set on the composable.
20
+ */
21
+ overrideFetchOptions: FetchOptions,
22
+ /**
23
+ * The global client options determined in the composable.
24
+ */
25
+ globalClientContext: Record<string, any>,
26
+ /**
27
+ * Client options overrides set on the composable.
28
+ */
29
+ overrideClientContext: Record<string, any>,
30
+ /**
31
+ * The cache options set on the composable.
32
+ */
33
+ cacheOptions: RequestCacheOptions): Promise<GraphqlResponse<T>>;
6
34
  declare module '#app' {
7
35
  interface NuxtApp {
8
36
  $graphqlCache?: GraphqlMiddlewareCache;
@@ -1,26 +1,57 @@
1
1
  import { useGraphqlState } from "./useGraphqlState.js";
2
2
  import { hash } from "ohash";
3
- import { GraphqlMiddlewareCache } from "../helpers/ClientCache.js";
4
3
  import { getEndpoint } from "#nuxt-graphql-middleware/helpers";
5
4
  import { useNuxtApp, useAppConfig } from "#imports";
6
- export function performRequest(operation, operationName, method, options, cacheOptions) {
5
+ import { operationHashes } from "#nuxt-graphql-middleware/operation-hashes";
6
+ import {
7
+ clientCacheEnabledAtBuild,
8
+ importMetaClient
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";
16
+ import { OPERATION_HASH_PREFIX } from "../settings/index.js";
17
+ export function performRequest(operation, operationName, variablesOrBody, overrideFetchOptions, globalClientContext, overrideClientContext, cacheOptions) {
7
18
  const state = useGraphqlState();
8
19
  const app = useNuxtApp();
20
+ const config = useAppConfig();
21
+ const method = operation === "query" ? "get" : "post";
22
+ const isQuery = operation === "query";
9
23
  if (!state) {
10
24
  console.error(
11
25
  `A GraphQL composable for ${operation} "${operationName}" was called before the "nuxt-graphql-middleware-provide-state" plugin could provide the state, which might lead to unexpected behaviour. Make sure that custom plugins that perform GraphQL requests are executed after "nuxt-graphql-middleware-provide-state" by setting it as a dependency via "dependsOn".`
12
26
  );
13
27
  }
14
- const key = `${operation}:${operationName}:${hash(options.params)}`;
15
- if (cacheOptions) {
16
- const config = useAppConfig();
17
- if (import.meta.client && cacheOptions.client && config.graphqlMiddleware.clientCacheEnabled) {
18
- if (!app.$graphqlCache) {
19
- app.$graphqlCache = new GraphqlMiddlewareCache(
20
- config.graphqlMiddleware.clientCacheMaxSize
21
- );
22
- }
23
- const cached = app.$graphqlCache.get(key);
28
+ const clientContext = Object.assign(
29
+ {},
30
+ globalClientContext,
31
+ overrideClientContext
32
+ );
33
+ const fetchOptions = Object.assign(
34
+ {},
35
+ state?.fetchOptions,
36
+ overrideFetchOptions
37
+ );
38
+ const paramsRaw = Object.assign(
39
+ importMetaClient ? {
40
+ // The unique operation hash that changes whenever any operation source or
41
+ // fragment changes.
42
+ [OPERATION_HASH_PREFIX]: operationHashes[operationName]
43
+ } : {},
44
+ encodeContext(clientContext),
45
+ fetchOptions.params,
46
+ fetchOptions.query,
47
+ isQuery ? encodeVariables(variablesOrBody) : null
48
+ );
49
+ const params = importMetaClient && isQuery ? sortQueryParams(paramsRaw) : paramsRaw;
50
+ const cacheKey = importMetaClient && clientCacheEnabledAtBuild && isQuery && cacheOptions?.client && config.graphqlMiddleware.clientCacheEnabled ? `${operation}:${operationName}:${hash(params)}` : void 0;
51
+ if (importMetaClient && cacheKey) {
52
+ const cache = getOrCreateClientCache(app, config);
53
+ if (cache) {
54
+ const cached = cache.get(cacheKey);
24
55
  if (cached) {
25
56
  if (import.meta.dev) {
26
57
  cached.then((response) => {
@@ -40,11 +71,23 @@ export function performRequest(operation, operationName, method, options, cacheO
40
71
  }
41
72
  const promise = $fetch(
42
73
  getEndpoint(operation, operationName),
43
- {
44
- ...state && state.fetchOptions ? state.fetchOptions : {},
45
- ...options,
46
- method
47
- }
74
+ Object.assign(
75
+ {},
76
+ // Use the merged fetch options.
77
+ fetchOptions,
78
+ // Remove params and query from the fetch options.
79
+ {
80
+ params: void 0,
81
+ query: void 0
82
+ },
83
+ // Set the previously merged params. That way we only ever pass "params"
84
+ // as the query params.
85
+ {
86
+ params,
87
+ method,
88
+ body: operation === "mutation" ? variablesOrBody : void 0
89
+ }
90
+ )
48
91
  ).then((v) => {
49
92
  if (import.meta.dev && v.errors?.length) {
50
93
  app.callHook("nuxt-graphql-middleware:errors", {
@@ -54,14 +97,13 @@ export function performRequest(operation, operationName, method, options, cacheO
54
97
  stack: Error().stack
55
98
  });
56
99
  }
57
- return {
58
- ...v,
59
- data: v.data,
60
- errors: v.errors || []
61
- };
100
+ return Object.assign({}, v, {
101
+ data: v?.data,
102
+ errors: v?.errors || []
103
+ });
62
104
  });
63
- if (import.meta.client && cacheOptions?.client && app.$graphqlCache) {
64
- app.$graphqlCache.set(key, promise);
105
+ if (importMetaClient && cacheKey && app.$graphqlCache && clientCacheEnabledAtBuild) {
106
+ app.$graphqlCache.set(cacheKey, promise);
65
107
  }
66
108
  return promise;
67
109
  }
@@ -1,57 +1,60 @@
1
1
  import {
2
- encodeContext
2
+ getOrCreateClientCache
3
3
  } from "./../helpers/composables.js";
4
4
  import { isRef, unref } from "vue";
5
- import { buildRequestParams } from "./../helpers/index.js";
6
5
  import { performRequest } from "./nuxtApp.js";
7
6
  import {
8
7
  clientOptions
9
8
  } from "#nuxt-graphql-middleware/client-options";
10
- import { useAsyncData, useAppConfig, useNuxtApp } from "#imports";
9
+ import { useAsyncData, useAppConfig, useNuxtApp, computed } from "#imports";
11
10
  import { hash } from "ohash";
11
+ import { importMetaClient } from "#nuxt-graphql-middleware/config";
12
12
  export function useAsyncGraphqlQuery(name, ...args) {
13
13
  const variables = args[0];
14
14
  const asyncDataOptions = args[1] || {};
15
- const fetchOptions = asyncDataOptions.fetchOptions;
16
- const key = `graphql:${name}:${hash(unref(variables))}`;
15
+ const asyncDataKey = computed(() => {
16
+ const vars = isRef(variables) ? variables.value : variables;
17
+ return `useAsyncGraphqlQuery:${name}:${hash(vars)}`;
18
+ });
17
19
  const config = useAppConfig();
18
20
  const app = useNuxtApp();
19
- if (import.meta.client) {
21
+ if (importMetaClient) {
20
22
  if (variables && isRef(variables)) {
21
23
  if (!asyncDataOptions.watch) {
22
24
  asyncDataOptions.watch = [];
23
25
  }
24
26
  asyncDataOptions.watch.push(variables);
25
27
  }
28
+ if (asyncDataOptions.graphqlCaching?.client && app.isHydrating) {
29
+ const cache = getOrCreateClientCache(app, config);
30
+ if (cache) {
31
+ const key = asyncDataKey.value;
32
+ const payload = app.payload.data[asyncDataKey.value];
33
+ if (payload) {
34
+ cache.set(key, payload);
35
+ }
36
+ }
37
+ }
26
38
  if (asyncDataOptions.graphqlCaching?.client && !asyncDataOptions.getCachedData) {
27
- asyncDataOptions.getCachedData = function(key2) {
28
- if (!app.isHydrating && !config.graphqlMiddleware.clientCacheEnabled) {
29
- return;
39
+ asyncDataOptions.getCachedData = function(key, app2, ctx) {
40
+ if (ctx.cause === "initial") {
41
+ return app2.payload.data[key] ?? app2.$graphqlCache?.get(key);
30
42
  }
31
- return app.payload.data[key2];
32
43
  };
33
44
  }
34
45
  }
35
46
  const result = useAsyncData(
36
- key,
47
+ asyncDataKey,
37
48
  () => {
38
- const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
49
+ const globalClientContext = clientOptions && clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
39
50
  return performRequest(
40
51
  "query",
41
52
  name,
42
- "get",
43
- {
44
- ...fetchOptions,
45
- params: {
46
- ...fetchOptions?.params || {},
47
- ...buildRequestParams(unref(variables)),
48
- ...encodeContext({
49
- ...globalClientContext,
50
- ...asyncDataOptions.clientContext || {}
51
- })
52
- }
53
- },
54
- asyncDataOptions.graphqlCaching
53
+ unref(variables) || {},
54
+ asyncDataOptions.fetchOptions || {},
55
+ globalClientContext,
56
+ asyncDataOptions.clientContext || {},
57
+ asyncDataOptions.graphqlCaching || {}
55
58
  );
56
59
  },
57
60
  asyncDataOptions
@@ -1,4 +1,4 @@
1
- import { type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './../helpers/composables.js';
1
+ import type { GetMutationArgs, MutationObjectArgs, GetMutationResult } from './../helpers/composables.js';
2
2
  import type { GraphqlResponse } from '#nuxt-graphql-middleware/response';
3
3
  import type { Mutation } from '#nuxt-graphql-middleware/operation-types';
4
4
  /**
@@ -1,25 +1,20 @@
1
- import {
2
- encodeContext
3
- } from "./../helpers/composables.js";
4
1
  import { performRequest } from "./nuxtApp.js";
5
2
  import { clientOptions } from "#nuxt-graphql-middleware/client-options";
6
3
  export function useGraphqlMutation(...args) {
7
- const [name, body, fetchOptions = {}, overrideClientContext = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.clientContext] : [
4
+ const [name, body, fetchOptions, overrideClientContext] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.clientContext] : [
8
5
  args[0].name,
9
6
  args[0].variables,
10
7
  args[0].fetchOptions,
11
8
  args[0].clientContext
12
9
  ];
13
- const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
14
- return performRequest("mutation", name, "post", {
15
- ...fetchOptions,
16
- body,
17
- params: {
18
- ...fetchOptions.params || {},
19
- ...encodeContext({
20
- ...globalClientContext,
21
- ...overrideClientContext
22
- })
23
- }
24
- });
10
+ const globalClientContext = clientOptions && clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
11
+ return performRequest(
12
+ "mutation",
13
+ name,
14
+ body || {},
15
+ fetchOptions || {},
16
+ globalClientContext,
17
+ overrideClientContext || {},
18
+ {}
19
+ );
25
20
  }
@@ -1,4 +1,4 @@
1
- import { type GetQueryArgs, type QueryObjectArgs, type GetQueryResult } from './../helpers/composables.js';
1
+ import type { GetQueryArgs, QueryObjectArgs, GetQueryResult } from './../helpers/composables.js';
2
2
  import type { GraphqlResponse } from '#nuxt-graphql-middleware/response';
3
3
  import type { Query } from '#nuxt-graphql-middleware/operation-types';
4
4
  /**
@@ -1,17 +1,7 @@
1
- import {
2
- encodeContext
3
- } from "./../helpers/composables.js";
4
- import { buildRequestParams } from "./../helpers/index.js";
5
1
  import { performRequest } from "./nuxtApp.js";
6
2
  import { clientOptions } from "#nuxt-graphql-middleware/client-options";
7
3
  export function useGraphqlQuery(...args) {
8
- const [
9
- name,
10
- variables,
11
- fetchOptions = {},
12
- graphqlCaching = {},
13
- overrideClientContext = {}
14
- ] = typeof args[0] === "string" ? [
4
+ const [name, variables, fetchOptions, graphqlCaching, overrideClientContext] = typeof args[0] === "string" ? [
15
5
  args[0],
16
6
  args[1],
17
7
  args[2]?.fetchOptions,
@@ -24,22 +14,14 @@ export function useGraphqlQuery(...args) {
24
14
  args[0].graphqlCaching,
25
15
  args[0].clientContext
26
16
  ];
27
- const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
17
+ const globalClientContext = clientOptions && clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
28
18
  return performRequest(
29
19
  "query",
30
20
  name,
31
- "get",
32
- {
33
- ...fetchOptions,
34
- params: {
35
- ...fetchOptions.params || {},
36
- ...buildRequestParams(variables),
37
- ...encodeContext({
38
- ...globalClientContext,
39
- ...overrideClientContext
40
- })
41
- }
42
- },
43
- graphqlCaching
21
+ variables || {},
22
+ fetchOptions || {},
23
+ globalClientContext || {},
24
+ overrideClientContext || {},
25
+ graphqlCaching || {}
44
26
  );
45
27
  }
@@ -49,7 +49,7 @@ export function useGraphqlUploadMutation(...args) {
49
49
  }
50
50
  const state = useGraphqlState();
51
51
  const formData = createFormData(variables);
52
- const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
52
+ const globalClientContext = clientOptions && clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
53
53
  const clientContext = encodeContext({
54
54
  ...globalClientContext,
55
55
  ...overrideClientContext
@@ -12,4 +12,5 @@ export declare class GraphqlMiddlewareCache {
12
12
  set(key: string, value: unknown): void;
13
13
  get<T>(key: string): T | undefined;
14
14
  purge(): void;
15
+ remove(key: string): boolean;
15
16
  }
@@ -36,5 +36,17 @@ export class GraphqlMiddlewareCache {
36
36
  }
37
37
  purge() {
38
38
  this.cache = {};
39
+ this.keys = [];
40
+ }
41
+ remove(key) {
42
+ if (Object.prototype.hasOwnProperty.call(this.cache, key)) {
43
+ delete this.cache[key];
44
+ const index = this.keys.indexOf(key);
45
+ if (index > -1) {
46
+ this.keys.splice(index, 1);
47
+ }
48
+ return true;
49
+ }
50
+ return false;
39
51
  }
40
52
  }
@@ -2,6 +2,9 @@ import type { FetchOptions } from 'ofetch';
2
2
  import type { RequestCacheOptions } from './../types.js';
3
3
  import type { GraphqlClientContext } from '#nuxt-graphql-middleware/client-options';
4
4
  import type { Query, Mutation } from '#nuxt-graphql-middleware/operation-types';
5
+ import type { NuxtApp } from '#app';
6
+ import type { AppConfig } from 'nuxt/schema';
7
+ import { GraphqlMiddlewareCache } from './ClientCache.js';
5
8
  export type GraphqlComposableOptions = {
6
9
  fetchOptions?: FetchOptions;
7
10
  graphqlCaching?: RequestCacheOptions;
@@ -38,3 +41,8 @@ export type MutationObjectArgs<K extends keyof Mutation, M extends Mutation[K] =
38
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;
39
42
  export type KeysOf<T> = Array<T extends T ? (keyof T extends string ? keyof T : never) : never>;
40
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
+ export declare function getOrCreateClientCache(app: NuxtApp, config: AppConfig): GraphqlMiddlewareCache | undefined;