nuxt-graphql-middleware 4.2.0 → 4.3.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 (52) hide show
  1. package/dist/client/200.html +9 -9
  2. package/dist/client/404.html +9 -9
  3. package/dist/client/_nuxt/{BBbivCOF.js → B4FhP7a6.js} +1 -1
  4. package/dist/client/_nuxt/CH4m2wxh.js +25 -0
  5. package/dist/client/_nuxt/{DbuEOF3O.js → CPd6XBwJ.js} +1 -1
  6. package/dist/client/_nuxt/{Ch07F_Ul.js → GKcsigNx.js} +1 -1
  7. package/dist/client/_nuxt/{BKf42UCq.js → VR7nYXIq.js} +1 -1
  8. package/dist/client/_nuxt/builds/latest.json +1 -1
  9. package/dist/client/_nuxt/builds/meta/08eb555c-2460-4359-b00b-ff7301e8150d.json +1 -0
  10. package/dist/client/index.html +9 -9
  11. package/dist/module.d.mts +20 -15
  12. package/dist/module.d.ts +20 -15
  13. package/dist/module.json +1 -1
  14. package/dist/module.mjs +115 -54
  15. package/dist/runtime/clientOptions/index.d.ts +2 -0
  16. package/dist/runtime/clientOptions/index.js +3 -0
  17. package/dist/runtime/composables/nuxtApp.js +1 -1
  18. package/dist/runtime/composables/useAsyncGraphqlQuery.d.ts +4 -2
  19. package/dist/runtime/composables/useAsyncGraphqlQuery.js +26 -10
  20. package/dist/runtime/composables/useGraphqlMutation.d.ts +2 -2
  21. package/dist/runtime/composables/useGraphqlMutation.js +19 -2
  22. package/dist/runtime/composables/useGraphqlQuery.d.ts +2 -2
  23. package/dist/runtime/composables/useGraphqlQuery.js +29 -4
  24. package/dist/runtime/composables/useGraphqlUploadMutation.d.ts +2 -2
  25. package/dist/runtime/composables/useGraphqlUploadMutation.js +19 -3
  26. package/dist/runtime/{composables/shared.d.ts → helpers/composables.d.ts} +8 -1
  27. package/dist/runtime/helpers/composables.js +17 -0
  28. package/dist/runtime/server/tsconfig.json +3 -0
  29. package/dist/runtime/server/utils/index.d.ts +3 -0
  30. package/dist/runtime/server/utils/index.js +12 -0
  31. package/dist/runtime/server/utils/useGraphqlMutation.d.ts +7 -0
  32. package/dist/runtime/server/utils/useGraphqlMutation.js +20 -0
  33. package/dist/runtime/server/utils/useGraphqlQuery.d.ts +7 -0
  34. package/dist/runtime/server/utils/useGraphqlQuery.js +21 -0
  35. package/dist/runtime/serverHandler/helpers/index.d.ts +9 -5
  36. package/dist/runtime/serverHandler/helpers/index.js +40 -9
  37. package/dist/runtime/serverHandler/index.js +15 -7
  38. package/dist/runtime/serverHandler/upload.js +16 -7
  39. package/dist/runtime/serverOptions/defineGraphqlServerOptions.d.ts +2 -1
  40. package/dist/runtime/settings/index.d.ts +2 -1
  41. package/dist/runtime/settings/index.js +2 -1
  42. package/dist/runtime/types.d.ts +52 -1
  43. package/package.json +6 -2
  44. package/dist/client/_nuxt/CaoFd9E8.js +0 -25
  45. package/dist/client/_nuxt/builds/meta/fff2eb15-13a3-4063-86e7-990068006f7e.json +0 -1
  46. package/dist/runtime/composables/server.d.ts +0 -11
  47. package/dist/runtime/composables/server.js +0 -29
  48. package/dist/runtime/composables/shared.js +0 -5
  49. /package/dist/client/_nuxt/{entry.DlumAtbg.css → entry.D9ltLgme.css} +0 -0
  50. /package/dist/client/_nuxt/{error-404.C_4C5G96.css → error-404.SWzu_puR.css} +0 -0
  51. /package/dist/client/_nuxt/{error-500.CBAEdpZV.css → error-500.Bkv_zTjr.css} +0 -0
  52. /package/dist/client/_nuxt/{index.DCCKx2Zk.css → index.D19Q16VT.css} +0 -0
@@ -1,18 +1,22 @@
1
1
  import { type QueryObject } from 'ufo';
2
2
  import type { H3Event } from 'h3';
3
3
  import type { FetchOptions, FetchResponse, FetchError } from 'ofetch';
4
- import type { GraphqlMiddlewareRuntimeConfig, GraphqlMiddlewareServerOptions } from './../../../types.js';
4
+ import type { GraphqlMiddlewareRequestContext, GraphqlMiddlewareRuntimeConfig, GraphqlMiddlewareServerOptions } from './../../../types.js';
5
5
  import { GraphqlMiddlewareOperation } from './../../settings/index.js';
6
6
  import { type Documents } from '#graphql-documents';
7
7
  export declare function queryParamToVariables(query: QueryObject): any;
8
+ /**
9
+ * Extract the client context from the query params.
10
+ */
11
+ export declare function extractRequestContext(query: QueryObject): GraphqlMiddlewareRequestContext<any>;
8
12
  /**
9
13
  * Get the URL of the GraphQL endpoint.
10
14
  */
11
- export declare function getEndpoint(config: GraphqlMiddlewareRuntimeConfig, serverOptions: GraphqlMiddlewareServerOptions, event: H3Event, operation: GraphqlMiddlewareOperation, operationName: string): string | Promise<string>;
15
+ export declare function getEndpoint(config: GraphqlMiddlewareRuntimeConfig, serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, operation: GraphqlMiddlewareOperation, operationName: string, context: GraphqlMiddlewareRequestContext<any>): string | Promise<string>;
12
16
  /**
13
17
  * Get the options for the $fetch request to the GraphQL server.
14
18
  */
15
- export declare function getFetchOptions(serverOptions: GraphqlMiddlewareServerOptions, event: H3Event, operation: GraphqlMiddlewareOperation, operationName: string): FetchOptions | Promise<FetchOptions>;
19
+ export declare function getFetchOptions(serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, operation: GraphqlMiddlewareOperation, operationName: string, context: GraphqlMiddlewareRequestContext<any>): FetchOptions | Promise<FetchOptions>;
16
20
  export declare function throwError(statusMessage: string, statusCode?: number): never;
17
21
  /**
18
22
  * Assure that the request is valid.
@@ -21,8 +25,8 @@ export declare function validateRequest(method?: string, operation?: GraphqlMidd
21
25
  /**
22
26
  * Handle GraphQL server response.
23
27
  */
24
- export declare function onServerResponse(serverOptions: GraphqlMiddlewareServerOptions, event: H3Event, response: FetchResponse<any>, operation?: string, operationName?: string): any;
28
+ export declare function onServerResponse(serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, response: FetchResponse<any>, operation: string, operationName: string, context: GraphqlMiddlewareRequestContext<any>): any;
25
29
  /**
26
30
  * Handle GraphQL server errors.
27
31
  */
28
- export declare function onServerError(serverOptions: GraphqlMiddlewareServerOptions, event: H3Event, error: FetchError, operation?: string, operationName?: string): any;
32
+ export declare function onServerError(serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, error: FetchError, operation: string, operationName: string, context: GraphqlMiddlewareRequestContext<any>): any;
@@ -1,5 +1,8 @@
1
1
  import { createError } from "h3";
2
- import { GraphqlMiddlewareOperation } from "./../../settings/index.js";
2
+ import {
3
+ CLIENT_CONTEXT_PREFIX,
4
+ GraphqlMiddlewareOperation
5
+ } from "./../../settings/index.js";
3
6
  export function queryParamToVariables(query) {
4
7
  try {
5
8
  if (query.__variables && typeof query.__variables === "string") {
@@ -9,12 +12,28 @@ export function queryParamToVariables(query) {
9
12
  }
10
13
  return query;
11
14
  }
12
- export function getEndpoint(config, serverOptions, event, operation, operationName) {
15
+ export function extractRequestContext(query) {
16
+ const client = {};
17
+ for (const property in query) {
18
+ if (property.startsWith(CLIENT_CONTEXT_PREFIX)) {
19
+ const value = query[property];
20
+ if (typeof value === "string") {
21
+ const key = property.replace(CLIENT_CONTEXT_PREFIX, "");
22
+ client[key] = value;
23
+ }
24
+ }
25
+ }
26
+ return {
27
+ client
28
+ };
29
+ }
30
+ export function getEndpoint(config, serverOptions, event, operation, operationName, context) {
13
31
  if (serverOptions.graphqlEndpoint) {
14
32
  const result = serverOptions.graphqlEndpoint(
15
33
  event,
16
34
  operation,
17
- operationName
35
+ operationName,
36
+ context
18
37
  );
19
38
  if (result) {
20
39
  return Promise.resolve(result);
@@ -25,9 +44,14 @@ export function getEndpoint(config, serverOptions, event, operation, operationNa
25
44
  }
26
45
  throw new Error("Failed to determine endpoint for GraphQL server.");
27
46
  }
28
- export function getFetchOptions(serverOptions, event, operation, operationName) {
47
+ export function getFetchOptions(serverOptions, event, operation, operationName, context) {
29
48
  if (serverOptions.serverFetchOptions) {
30
- return serverOptions.serverFetchOptions(event, operation, operationName) || {};
49
+ return serverOptions.serverFetchOptions(
50
+ event,
51
+ operation,
52
+ operationName,
53
+ context
54
+ ) || {};
31
55
  }
32
56
  return {};
33
57
  }
@@ -60,20 +84,27 @@ export function validateRequest(method, operation, name, documents) {
60
84
  throwError(`Operation "${operation}" with name "${name}" not found.`);
61
85
  }
62
86
  }
63
- export function onServerResponse(serverOptions, event, response, operation, operationName) {
87
+ export function onServerResponse(serverOptions, event, response, operation, operationName, context) {
64
88
  if (serverOptions.onServerResponse) {
65
89
  return serverOptions.onServerResponse(
66
90
  event,
67
91
  response,
68
92
  operation,
69
- operationName
93
+ operationName,
94
+ context
70
95
  );
71
96
  }
72
97
  return response._data;
73
98
  }
74
- export function onServerError(serverOptions, event, error, operation, operationName) {
99
+ export function onServerError(serverOptions, event, error, operation, operationName, context) {
75
100
  if (serverOptions.onServerError) {
76
- return serverOptions.onServerError(event, error, operation, operationName);
101
+ return serverOptions.onServerError(
102
+ event,
103
+ error,
104
+ operation,
105
+ operationName,
106
+ context
107
+ );
77
108
  }
78
109
  const message = error && "message" in error ? error.message : "";
79
110
  throw createError({
@@ -5,7 +5,8 @@ import {
5
5
  getFetchOptions,
6
6
  validateRequest,
7
7
  onServerResponse,
8
- onServerError
8
+ onServerError,
9
+ extractRequestContext
9
10
  } from "./helpers/index.js";
10
11
  import { GraphqlMiddlewareOperation } from "./../settings/index.js";
11
12
  import { documents } from "#graphql-documents";
@@ -17,14 +18,17 @@ export default defineEventHandler(async (event) => {
17
18
  const operationName = event.context?.params?.name;
18
19
  validateRequest(method, operation, operationName, documents);
19
20
  const operationDocument = documents[operation][operationName];
20
- const variables = operation === GraphqlMiddlewareOperation.Query ? queryParamToVariables(getQuery(event)) : await readBody(event);
21
+ const queryParams = getQuery(event);
22
+ const context = extractRequestContext(queryParams);
23
+ const variables = operation === GraphqlMiddlewareOperation.Query ? queryParamToVariables(queryParams) : await readBody(event);
21
24
  if (serverOptions.doGraphqlRequest) {
22
25
  return serverOptions.doGraphqlRequest({
23
26
  event,
24
27
  operation,
25
28
  operationName,
26
29
  operationDocument,
27
- variables
30
+ variables,
31
+ context
28
32
  });
29
33
  }
30
34
  const runtimeConfig = useRuntimeConfig().graphqlMiddleware;
@@ -33,13 +37,15 @@ export default defineEventHandler(async (event) => {
33
37
  serverOptions,
34
38
  event,
35
39
  operation,
36
- operationName
40
+ operationName,
41
+ context
37
42
  );
38
43
  const fetchOptions = await getFetchOptions(
39
44
  serverOptions,
40
45
  event,
41
46
  operation,
42
- operationName
47
+ operationName,
48
+ context
43
49
  );
44
50
  return $fetch.raw(endpoint, {
45
51
  // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
@@ -56,7 +62,8 @@ export default defineEventHandler(async (event) => {
56
62
  event,
57
63
  response,
58
64
  operation,
59
- operationName
65
+ operationName,
66
+ context
60
67
  );
61
68
  }).catch((error) => {
62
69
  return onServerError(
@@ -64,7 +71,8 @@ export default defineEventHandler(async (event) => {
64
71
  event,
65
72
  error,
66
73
  operation,
67
- operationName
74
+ operationName,
75
+ context
68
76
  );
69
77
  });
70
78
  });
@@ -1,6 +1,7 @@
1
1
  import {
2
2
  defineEventHandler,
3
- readMultipartFormData
3
+ readMultipartFormData,
4
+ getQuery
4
5
  } from "h3";
5
6
  import {
6
7
  getEndpoint,
@@ -8,7 +9,8 @@ import {
8
9
  validateRequest,
9
10
  onServerResponse,
10
11
  onServerError,
11
- throwError
12
+ throwError,
13
+ extractRequestContext
12
14
  } from "./helpers/index.js";
13
15
  import { GraphqlMiddlewareOperation } from "./../settings/index.js";
14
16
  import { documents } from "#graphql-documents";
@@ -52,6 +54,8 @@ export default defineEventHandler(async (event) => {
52
54
  const blob = new Blob([file.data]);
53
55
  formData.append(file.name, blob, file.filename);
54
56
  }
57
+ const queryParams = getQuery(event);
58
+ const context = extractRequestContext(queryParams);
55
59
  if (serverOptions.doGraphqlRequest) {
56
60
  return serverOptions.doGraphqlRequest({
57
61
  event,
@@ -59,7 +63,8 @@ export default defineEventHandler(async (event) => {
59
63
  operationName,
60
64
  operationDocument,
61
65
  variables,
62
- formData
66
+ formData,
67
+ context
63
68
  });
64
69
  }
65
70
  const runtimeConfig = useRuntimeConfig().graphqlMiddleware;
@@ -68,13 +73,15 @@ export default defineEventHandler(async (event) => {
68
73
  serverOptions,
69
74
  event,
70
75
  operation,
71
- operationName
76
+ operationName,
77
+ context
72
78
  );
73
79
  const fetchOptions = await getFetchOptions(
74
80
  serverOptions,
75
81
  event,
76
82
  operation,
77
- operationName
83
+ operationName,
84
+ context
78
85
  );
79
86
  return $fetch.raw(endpoint, {
80
87
  ...fetchOptions,
@@ -86,7 +93,8 @@ export default defineEventHandler(async (event) => {
86
93
  event,
87
94
  response,
88
95
  operation,
89
- operationName
96
+ operationName,
97
+ context
90
98
  );
91
99
  }).catch((error) => {
92
100
  return onServerError(
@@ -94,7 +102,8 @@ export default defineEventHandler(async (event) => {
94
102
  event,
95
103
  error,
96
104
  operation,
97
- operationName
105
+ operationName,
106
+ context
98
107
  );
99
108
  });
100
109
  });
@@ -1,2 +1,3 @@
1
+ import type { GraphqlClientContext } from '#graphql-middleware-client-options';
1
2
  import { type GraphqlMiddlewareServerOptions } from './../../types.js';
2
- export declare function defineGraphqlServerOptions<T extends object>(options: GraphqlMiddlewareServerOptions<T>): GraphqlMiddlewareServerOptions<T>;
3
+ export declare function defineGraphqlServerOptions<T extends object>(options: GraphqlMiddlewareServerOptions<T, GraphqlClientContext>): GraphqlMiddlewareServerOptions<T, GraphqlClientContext>;
@@ -6,7 +6,7 @@ export declare enum GraphqlMiddlewareTemplate {
6
6
  /**
7
7
  * Signature for the GraphQL composable arguments and return types.
8
8
  */
9
- ComposableContext = "nuxt-graphql-middleware.d.ts",
9
+ ComposableContext = "nuxt-graphql-middleware/generated-types.d.ts",
10
10
  /**
11
11
  * Exports a single opject containing the compiled queries and mutations.
12
12
  */
@@ -16,3 +16,4 @@ export declare enum GraphqlMiddlewareOperation {
16
16
  Query = "query",
17
17
  Mutation = "mutation"
18
18
  }
19
+ export declare const CLIENT_CONTEXT_PREFIX = "__gqlc_";
@@ -1,6 +1,6 @@
1
1
  export var GraphqlMiddlewareTemplate = /* @__PURE__ */ ((GraphqlMiddlewareTemplate2) => {
2
2
  GraphqlMiddlewareTemplate2["OperationTypes"] = "graphql-operations.d.ts";
3
- GraphqlMiddlewareTemplate2["ComposableContext"] = "nuxt-graphql-middleware.d.ts";
3
+ GraphqlMiddlewareTemplate2["ComposableContext"] = "nuxt-graphql-middleware/generated-types.d.ts";
4
4
  GraphqlMiddlewareTemplate2["Documents"] = "graphql-documents.mjs";
5
5
  return GraphqlMiddlewareTemplate2;
6
6
  })(GraphqlMiddlewareTemplate || {});
@@ -9,3 +9,4 @@ export var GraphqlMiddlewareOperation = /* @__PURE__ */ ((GraphqlMiddlewareOpera
9
9
  GraphqlMiddlewareOperation2["Mutation"] = "mutation";
10
10
  return GraphqlMiddlewareOperation2;
11
11
  })(GraphqlMiddlewareOperation || {});
12
+ export const CLIENT_CONTEXT_PREFIX = "__gqlc_";
@@ -1,6 +1,6 @@
1
1
  import type { FetchOptions, FetchContext } from 'ofetch';
2
2
  import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
3
- import type { GraphqlMiddlewareResponseUnion } from '#build/nuxt-graphql-middleware';
3
+ import type { GraphqlMiddlewareResponseUnion } from '#nuxt-graphql-middleware/generated-types';
4
4
  export type GraphqlResponseErrorLocation = {
5
5
  line: number;
6
6
  column: number;
@@ -22,3 +22,54 @@ export interface GraphqlMiddlewareState {
22
22
  export type RequestCacheOptions = {
23
23
  client?: boolean;
24
24
  };
25
+ export type ContextType = {
26
+ [key: string]: string | null | undefined;
27
+ };
28
+ export type GraphqlClientOptions<T extends ContextType = ContextType> = {
29
+ /**
30
+ * Build the client context for this request.
31
+ *
32
+ * The method should return an object whose properties and values are strings.
33
+ * This object will be encoded as a query param when making the request to
34
+ * the GraphQL middleware. Each property name is prefixed when converted to a
35
+ * query param to prevent collisions.
36
+ *
37
+ * On the server, the context is reassembled and passed to methods in custom
38
+ * server options such as getEndpoint or serverFetchOptions.
39
+ *
40
+ * One use case would be to pass some state of the Nuxt app to your server
41
+ * options such as the current language.
42
+ *
43
+ * @example
44
+ * Define a context.
45
+ *
46
+ * ```typescript
47
+ * export default defineGraphqlClientOptions({
48
+ * buildClientContext() {
49
+ * // Pass the current language as context.
50
+ * const language = useCurrentLanguage()
51
+ * return {
52
+ * language: language.value,
53
+ * }
54
+ * },
55
+ * })
56
+ * ```
57
+ *
58
+ * Now when a GraphQL query is made with useGraphqlQuery the request URL will
59
+ * look like this:
60
+ * `/api/graphql_middleware/myQuery?queryVariable=foo&__gqlc_language=en`
61
+ * ^ your context
62
+ *
63
+ * Then in your serverOptions file, you can then access the context:
64
+ *
65
+ * ```typescript
66
+ * export default defineGraphqlServerOptions({
67
+ * graphqlEndpoint(event, operation, operationName, context) {
68
+ * const language = context?.client?.language || 'en'
69
+ * return `http://backend_server/${language}/graphql`
70
+ * },
71
+ * })
72
+ * ```
73
+ */
74
+ buildClientContext?: () => T;
75
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
- "version": "4.2.0",
3
+ "version": "4.3.1",
4
4
  "description": "Module to perform GraphQL requests as a server middleware.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -22,6 +22,10 @@
22
22
  "./dist/runtime/serverOptions": {
23
23
  "import": "./dist/runtime/serverOptions/index.js",
24
24
  "types": "./dist/runtime/serverOptions/index.d.ts"
25
+ },
26
+ "./dist/runtime/clientOptions": {
27
+ "import": "./dist/runtime/clientOptions/index.js",
28
+ "types": "./dist/runtime/clientOptions/index.d.ts"
25
29
  }
26
30
  },
27
31
  "main": "./dist/module.cjs",
@@ -90,7 +94,7 @@
90
94
  "strip-ansi": "^7.1.0",
91
95
  "typedoc": "^0.26.3",
92
96
  "typedoc-plugin-markdown": "^4.1.1",
93
- "vitepress": "^1.2.3",
97
+ "vitepress": "^1.5.0",
94
98
  "vitest": "^1.6.0",
95
99
  "vue-tsc": "^2.1.6"
96
100
  }