nuxt-graphql-middleware 5.0.0-alpha.9 → 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 (72) hide show
  1. package/README.md +101 -19
  2. package/dist/client/200.html +8 -8
  3. package/dist/client/404.html +8 -8
  4. package/dist/client/_nuxt/{C9pb_2rp.js → CPiV13Ng.js} +2 -2
  5. package/dist/client/_nuxt/CQCiRbEL.js +1 -0
  6. package/dist/client/_nuxt/CYI2eNUl.js +1 -0
  7. package/dist/client/_nuxt/UAIVpYSK.js +25 -0
  8. package/dist/client/_nuxt/builds/latest.json +1 -1
  9. package/dist/client/_nuxt/builds/meta/48332277-c4ad-4d6a-8f09-7a58a109c32d.json +1 -0
  10. package/dist/client/_nuxt/error-404.Bbd2eCoc.css +1 -0
  11. package/dist/client/_nuxt/error-500.Cd2cwFc3.css +1 -0
  12. package/dist/client/_nuxt/{BLvMh1Ga.js → u1b7LyOw.js} +1 -1
  13. package/dist/client/index.html +8 -8
  14. package/dist/client-options.d.mts +6 -0
  15. package/dist/client-options.mjs +5 -0
  16. package/dist/module.d.mts +63 -181
  17. package/dist/module.json +4 -4
  18. package/dist/module.mjs +435 -92
  19. package/dist/runtime/components/CodeFrame.vue +19 -28
  20. package/dist/runtime/components/CodeFrame.vue.d.ts +7 -0
  21. package/dist/runtime/components/DevModeOverlay.vue +25 -33
  22. package/dist/runtime/components/DevModeOverlay.vue.d.ts +3 -0
  23. package/dist/runtime/components/ErrorExtensions.vue +9 -11
  24. package/dist/runtime/components/ErrorExtensions.vue.d.ts +5 -0
  25. package/dist/runtime/components/ErrorGroup.vue +28 -39
  26. package/dist/runtime/components/ErrorGroup.vue.d.ts +9 -0
  27. package/dist/runtime/composables/nuxtApp.d.ts +30 -2
  28. package/dist/runtime/composables/nuxtApp.js +66 -24
  29. package/dist/runtime/composables/useAsyncGraphqlQuery.js +28 -25
  30. package/dist/runtime/composables/useGraphqlMutation.d.ts +1 -1
  31. package/dist/runtime/composables/useGraphqlMutation.js +11 -16
  32. package/dist/runtime/composables/useGraphqlQuery.d.ts +1 -1
  33. package/dist/runtime/composables/useGraphqlQuery.js +7 -25
  34. package/dist/runtime/composables/useGraphqlUploadMutation.js +1 -1
  35. package/dist/runtime/helpers/ClientCache.d.ts +1 -0
  36. package/dist/runtime/helpers/ClientCache.js +12 -0
  37. package/dist/runtime/helpers/composables.d.ts +8 -0
  38. package/dist/runtime/helpers/composables.js +27 -0
  39. package/dist/runtime/helpers/index.d.ts +0 -4
  40. package/dist/runtime/helpers/index.js +0 -13
  41. package/dist/runtime/helpers/queryEncoding.d.ts +11 -0
  42. package/dist/runtime/helpers/queryEncoding.js +89 -0
  43. package/dist/runtime/plugins/devMode.js +2 -1
  44. package/dist/runtime/server/api/mutation.js +2 -1
  45. package/dist/runtime/server/api/query.js +7 -8
  46. package/dist/runtime/server/utils/doGraphqlRequest.js +5 -4
  47. package/dist/runtime/server/utils/useGraphqlQuery.js +2 -2
  48. package/dist/runtime/settings/index.d.ts +1 -0
  49. package/dist/runtime/settings/index.js +1 -0
  50. package/dist/runtime/types.d.ts +2 -2
  51. package/dist/server-options.d.mts +8 -0
  52. package/dist/server-options.mjs +5 -0
  53. package/dist/shared/nuxt-graphql-middleware.-BeiPV4H.d.mts +566 -0
  54. package/dist/types.d.mts +1 -7
  55. package/dist/utils.d.mts +15 -0
  56. package/dist/utils.mjs +18 -0
  57. package/package.json +36 -32
  58. package/dist/client/_nuxt/CBwfSTyQ.js +0 -1
  59. package/dist/client/_nuxt/CPyoLiCY.js +0 -1
  60. package/dist/client/_nuxt/VpkRx2_e.js +0 -25
  61. package/dist/client/_nuxt/builds/meta/826a43da-d42c-4fbf-8dfd-2572141eaf8f.json +0 -1
  62. package/dist/client/_nuxt/error-404.BJkSn6RI.css +0 -1
  63. package/dist/client/_nuxt/error-500.TOCKLquH.css +0 -1
  64. package/dist/module.cjs +0 -5
  65. package/dist/module.d.ts +0 -210
  66. package/dist/runtime/clientOptions/index.d.ts +0 -2
  67. package/dist/runtime/clientOptions/index.js +0 -3
  68. package/dist/runtime/serverOptions/defineGraphqlServerOptions.d.ts +0 -4
  69. package/dist/runtime/serverOptions/defineGraphqlServerOptions.js +0 -3
  70. package/dist/runtime/serverOptions/index.d.ts +0 -2
  71. package/dist/runtime/serverOptions/index.js +0 -2
  72. package/dist/types.d.ts +0 -7
@@ -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;
@@ -1,4 +1,9 @@
1
1
  import { CLIENT_CONTEXT_PREFIX } from "../settings/index.js";
2
+ import { GraphqlMiddlewareCache } from "./ClientCache.js";
3
+ import {
4
+ clientCacheEnabledAtBuild,
5
+ importMetaServer
6
+ } from "#nuxt-graphql-middleware/config";
2
7
  export function encodeContext(context) {
3
8
  return Object.entries(context).reduce(
4
9
  (acc, [key, value]) => {
@@ -10,3 +15,25 @@ export function encodeContext(context) {
10
15
  {}
11
16
  );
12
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
+ export function getOrCreateClientCache(app, config) {
27
+ if (importMetaServer || !clientCacheEnabledAtBuild) {
28
+ return;
29
+ }
30
+ if (!config.graphqlMiddleware.clientCacheEnabled) {
31
+ return;
32
+ }
33
+ if (!app.$graphqlCache) {
34
+ app.$graphqlCache = new GraphqlMiddlewareCache(
35
+ config.graphqlMiddleware.clientCacheMaxSize
36
+ );
37
+ }
38
+ return app.$graphqlCache;
39
+ }
@@ -5,7 +5,3 @@
5
5
  * items.filter(falsy)
6
6
  */
7
7
  export declare function falsy<T>(value: T): value is NonNullable<T>;
8
- /**
9
- * Get the parameters for the GraphQL middleware query.
10
- */
11
- export declare function buildRequestParams(variables?: Record<string, any> | undefined | null): Record<string, any>;
@@ -1,16 +1,3 @@
1
1
  export function falsy(value) {
2
2
  return value !== null && value !== void 0;
3
3
  }
4
- export function buildRequestParams(variables) {
5
- if (typeof variables !== "object" || !variables) {
6
- return {};
7
- }
8
- for (const key in variables) {
9
- if (typeof variables[key] !== "string") {
10
- return {
11
- __variables: JSON.stringify(variables)
12
- };
13
- }
14
- }
15
- return variables;
16
- }
@@ -0,0 +1,11 @@
1
+ export declare function encodeVariables(variables?: Record<string, any> | null): Record<string, any>;
2
+ /**
3
+ * Get the variables from query parameters.
4
+ *
5
+ * For simple cases with type prefixes:
6
+ * ?name=Jon&n:age=20&b:isUser=false
7
+ *
8
+ * In complex cases, the entire variables are sent as a JSON encoded string:
9
+ * ?__variables=%7B%22foobar%22:%7B%22path%22:%22%22%7D%7D
10
+ */
11
+ export declare function decodeVariables(query: Record<string, any>, validKeys?: string[]): any;
@@ -0,0 +1,89 @@
1
+ import { experimentalQueryParamEncoding } from "#nuxt-graphql-middleware/config";
2
+ import { CLIENT_CONTEXT_PREFIX, OPERATION_HASH_PREFIX } from "../settings/index.js";
3
+ export function encodeVariables(variables) {
4
+ if (typeof variables !== "object" || !variables) {
5
+ return {};
6
+ }
7
+ if (!experimentalQueryParamEncoding) {
8
+ for (const key in variables) {
9
+ if (typeof variables[key] !== "string") {
10
+ return {
11
+ __variables: JSON.stringify(variables)
12
+ };
13
+ }
14
+ }
15
+ return variables;
16
+ }
17
+ const result = {};
18
+ let needsFallback = false;
19
+ for (const key in variables) {
20
+ const value = variables[key];
21
+ const type = typeof value;
22
+ if (type === "string") {
23
+ result[key] = value;
24
+ } else if (type === "number") {
25
+ result[`n:${key}`] = String(value);
26
+ } else if (type === "boolean") {
27
+ result[`b:${key}`] = String(value);
28
+ } else {
29
+ needsFallback = true;
30
+ break;
31
+ }
32
+ }
33
+ if (needsFallback) {
34
+ return {
35
+ __variables: JSON.stringify(variables)
36
+ };
37
+ }
38
+ return result;
39
+ }
40
+ function filterValidKeys(validKeys, obj) {
41
+ return validKeys.reduce((acc, key) => {
42
+ const value = obj[key];
43
+ if (value !== void 0 && value !== null) {
44
+ acc[key] = value;
45
+ }
46
+ return acc;
47
+ }, {});
48
+ }
49
+ export function decodeVariables(query, validKeys) {
50
+ try {
51
+ if (query.__variables && typeof query.__variables === "string") {
52
+ return JSON.parse(query.__variables);
53
+ }
54
+ } catch {
55
+ }
56
+ if (!experimentalQueryParamEncoding) {
57
+ if (validKeys) {
58
+ return filterValidKeys(validKeys, query);
59
+ }
60
+ return query;
61
+ }
62
+ if (validKeys && validKeys.length === 0) {
63
+ return {};
64
+ }
65
+ const result = {};
66
+ for (const key in query) {
67
+ if (key.startsWith(CLIENT_CONTEXT_PREFIX) || key.startsWith(OPERATION_HASH_PREFIX)) {
68
+ continue;
69
+ } else if (key.startsWith("n:")) {
70
+ const actualKey = key.substring(2);
71
+ if (validKeys && !validKeys.includes(actualKey)) {
72
+ continue;
73
+ }
74
+ result[actualKey] = Number(query[key]);
75
+ } else if (key.startsWith("b:")) {
76
+ const actualKey = key.substring(2);
77
+ if (validKeys && !validKeys.includes(actualKey)) {
78
+ continue;
79
+ }
80
+ result[actualKey] = query[key] === "true";
81
+ } else if (key !== "__variables") {
82
+ if (validKeys && !validKeys.includes(key)) {
83
+ continue;
84
+ }
85
+ result[key] = query[key];
86
+ }
87
+ }
88
+ return result;
89
+ }
@@ -1,6 +1,7 @@
1
1
  import { defineNuxtPlugin, useState } from "#imports";
2
2
  import { createApp } from "vue";
3
3
  import DevModeOverlay from "../components/DevModeOverlay.vue";
4
+ import { importMetaClient } from "#nuxt-graphql-middleware/config";
4
5
  export default defineNuxtPlugin({
5
6
  name: "nuxt-graphql-middleware:dev-mode",
6
7
  setup(nuxtApp) {
@@ -11,7 +12,7 @@ export default defineNuxtPlugin({
11
12
  nuxtApp.hook("nuxt-graphql-middleware:errors", (value) => {
12
13
  errors.value.push(value);
13
14
  });
14
- if (import.meta.client) {
15
+ if (importMetaClient) {
15
16
  nuxtApp.hook("app:mounted", () => {
16
17
  const container = document.createElement("div");
17
18
  document.body.appendChild(container);
@@ -20,7 +20,8 @@ export default defineEventHandler(async (event) => {
20
20
  {
21
21
  query: operationDocument,
22
22
  variables,
23
- operation: GraphqlMiddlewareOperation.Mutation
23
+ operation: GraphqlMiddlewareOperation.Mutation,
24
+ operationName
24
25
  },
25
26
  context,
26
27
  event
@@ -1,13 +1,10 @@
1
1
  import { defineEventHandler, getQuery, getRouterParam } from "h3";
2
- import {
3
- queryParamToVariables,
4
- extractRequestContext,
5
- isValidQuery,
6
- throwError
7
- } from "./../helpers/index.js";
2
+ import { extractRequestContext, isValidQuery, throwError } from "./../helpers/index.js";
8
3
  import { GraphqlMiddlewareOperation } from "./../../settings/index.js";
9
4
  import { documents } from "#nuxt-graphql-middleware/documents";
10
5
  import { doGraphqlRequest } from "../utils/doGraphqlRequest.js";
6
+ import { decodeVariables } from "../../helpers/queryEncoding.js";
7
+ import { operationVariables } from "#nuxt-graphql-middleware/operation-variables";
11
8
  export default defineEventHandler(async (event) => {
12
9
  const operationName = getRouterParam(event, "name");
13
10
  if (!isValidQuery(operationName)) {
@@ -16,12 +13,14 @@ export default defineEventHandler(async (event) => {
16
13
  const operationDocument = documents.query[operationName];
17
14
  const queryParams = getQuery(event);
18
15
  const context = extractRequestContext(queryParams);
19
- const variables = queryParamToVariables(queryParams);
16
+ const validVariableKeys = operationVariables[operationName];
17
+ const variables = decodeVariables(queryParams, validVariableKeys);
20
18
  return doGraphqlRequest(
21
19
  {
22
20
  query: operationDocument,
23
21
  variables,
24
- operation: GraphqlMiddlewareOperation.Query
22
+ operation: GraphqlMiddlewareOperation.Query,
23
+ operationName
25
24
  },
26
25
  context,
27
26
  event
@@ -8,9 +8,7 @@ import {
8
8
  onServerResponse
9
9
  } from "../helpers/index.js";
10
10
  export async function doGraphqlRequest(body, context = null, providedEvent = null) {
11
- const operationName = body.operationName || null;
12
11
  const event = providedEvent ?? useEvent();
13
- const runtimeConfig = useRuntimeConfig().graphqlMiddleware;
14
12
  if (serverOptions.doGraphqlRequest) {
15
13
  return serverOptions.doGraphqlRequest({
16
14
  event,
@@ -21,6 +19,9 @@ export async function doGraphqlRequest(body, context = null, providedEvent = nul
21
19
  context: context || {}
22
20
  });
23
21
  }
22
+ const operation = body.operation || null;
23
+ const operationName = body.operationName || null;
24
+ const runtimeConfig = useRuntimeConfig().graphqlMiddleware;
24
25
  const endpoint = await getEndpoint(
25
26
  runtimeConfig,
26
27
  serverOptions,
@@ -50,7 +51,7 @@ export async function doGraphqlRequest(body, context = null, providedEvent = nul
50
51
  serverOptions,
51
52
  event,
52
53
  response,
53
- null,
54
+ operation,
54
55
  operationName,
55
56
  context
56
57
  );
@@ -59,7 +60,7 @@ export async function doGraphqlRequest(body, context = null, providedEvent = nul
59
60
  serverOptions,
60
61
  event,
61
62
  error,
62
- null,
63
+ operation,
63
64
  operationName,
64
65
  context
65
66
  );
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  encodeContext
3
3
  } from "./../../helpers/composables.js";
4
- import { buildRequestParams } from "./../../helpers/index.js";
4
+ import { encodeVariables } from "./../../helpers/queryEncoding.js";
5
5
  import { performRequest } from "./index.js";
6
6
  export function useGraphqlQuery(...args) {
7
7
  const [name, variables, fetchOptions = {}, clientContext = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.clientContext] : [
@@ -14,7 +14,7 @@ export function useGraphqlQuery(...args) {
14
14
  ...fetchOptions,
15
15
  params: {
16
16
  ...fetchOptions.params || {},
17
- ...buildRequestParams(variables),
17
+ ...encodeVariables(variables),
18
18
  ...encodeContext(clientContext)
19
19
  }
20
20
  });
@@ -3,3 +3,4 @@ export declare enum GraphqlMiddlewareOperation {
3
3
  Mutation = "mutation"
4
4
  }
5
5
  export declare const CLIENT_CONTEXT_PREFIX = "__gqlc_";
6
+ export declare const OPERATION_HASH_PREFIX = "__gqlh";
@@ -4,3 +4,4 @@ export var GraphqlMiddlewareOperation = /* @__PURE__ */ ((GraphqlMiddlewareOpera
4
4
  return GraphqlMiddlewareOperation2;
5
5
  })(GraphqlMiddlewareOperation || {});
6
6
  export const CLIENT_CONTEXT_PREFIX = "__gqlc_";
7
+ export const OPERATION_HASH_PREFIX = "__gqlh";
@@ -88,8 +88,8 @@ export type GraphqlClientOptions<T extends ContextType = ContextType> = {
88
88
  export type GraphqlMiddlewareRequestContext<C extends ContextType = ContextType> = {
89
89
  client?: Partial<C>;
90
90
  };
91
- export type GraphqlMiddlewareGraphqlEndpointMethod<C extends ContextType> = (event?: H3Event, operation?: string | null, operationName?: string | null, context?: GraphqlMiddlewareRequestContext<C> | null) => string | Promise<string> | undefined;
92
- export type GraphqlMiddlewareServerFetchOptionsMethod<C extends ContextType> = (event?: H3Event, operation?: string | null, operationName?: string | null, context?: GraphqlMiddlewareRequestContext<C> | null) => FetchOptions | Promise<FetchOptions>;
91
+ export type GraphqlMiddlewareGraphqlEndpointMethod<C extends ContextType> = (event: H3Event, operation?: string | null, operationName?: string | null, context?: GraphqlMiddlewareRequestContext<C> | null) => string | Promise<string> | undefined;
92
+ export type GraphqlMiddlewareServerFetchOptionsMethod<C extends ContextType> = (event: H3Event, operation?: string | null, operationName?: string | null, context?: GraphqlMiddlewareRequestContext<C> | null) => FetchOptions | Promise<FetchOptions>;
93
93
  export type GraphqlMiddlewareOnServerResponseMethod<ServerReponse, T, C extends ContextType> = (event: H3Event, response: FetchResponse<ServerReponse>, operation?: string | null, operationName?: string | null, context?: GraphqlMiddlewareRequestContext<C> | null) => T | Promise<T>;
94
94
  export type GraphqlMiddlewareOnServerErrorMethod<C extends ContextType> = (event: H3Event, error: FetchError, operation?: string | null, operationName?: string | null, context?: GraphqlMiddlewareRequestContext<C> | null) => any | Promise<any>;
95
95
  export type GraphqlMiddlewareDoRequestMethodContext<C extends ContextType> = {
@@ -0,0 +1,8 @@
1
+ import { GraphqlClientContext } from '#nuxt-graphql-middleware/client-options';
2
+ import { GraphqlMiddlewareResponseUnion } from '#nuxt-graphql-middleware/response';
3
+ import { GraphqlMiddlewareServerOptions } from '../dist/runtime/types.js';
4
+ export { GraphqlMiddlewareServerOptions } from '../dist/runtime/types.js';
5
+
6
+ declare function defineGraphqlServerOptions<T extends object = object>(options: GraphqlMiddlewareServerOptions<T, GraphqlMiddlewareResponseUnion, GraphqlClientContext>): GraphqlMiddlewareServerOptions<T, GraphqlMiddlewareResponseUnion, GraphqlClientContext>;
7
+
8
+ export { defineGraphqlServerOptions };
@@ -0,0 +1,5 @@
1
+ function defineGraphqlServerOptions(options) {
2
+ return options;
3
+ }
4
+
5
+ export { defineGraphqlServerOptions };