nuxt-graphql-middleware 4.2.0 → 4.3.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 (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/172a2fbf-9aed-4d11-81c4-ac46ab9b664c.json +1 -0
  10. package/dist/client/index.html +9 -9
  11. package/dist/module.d.mts +18 -14
  12. package/dist/module.d.ts +18 -14
  13. package/dist/module.json +1 -1
  14. package/dist/module.mjs +72 -36
  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,6 +1,12 @@
1
+ import {
2
+ encodeContext
3
+ } from "./../helpers/composables.js";
1
4
  import { isRef, unref } from "vue";
2
5
  import { buildRequestParams } from "./../helpers/index.js";
3
6
  import { performRequest } from "./nuxtApp.js";
7
+ import {
8
+ clientOptions
9
+ } from "#graphql-middleware-client-options";
4
10
  import { useAsyncData, useAppConfig, useNuxtApp } from "#imports";
5
11
  import { hash } from "ohash";
6
12
  export function useAsyncGraphqlQuery(name, ...args) {
@@ -28,16 +34,26 @@ export function useAsyncGraphqlQuery(name, ...args) {
28
34
  }
29
35
  return useAsyncData(
30
36
  key,
31
- () => performRequest(
32
- "query",
33
- name,
34
- "get",
35
- {
36
- params: buildRequestParams(unref(variables)),
37
- ...fetchOptions
38
- },
39
- asyncDataOptions.graphqlCaching
40
- ),
37
+ () => {
38
+ const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
39
+ return performRequest(
40
+ "query",
41
+ 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
55
+ );
56
+ },
41
57
  asyncDataOptions
42
58
  );
43
59
  }
@@ -1,5 +1,5 @@
1
- import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './shared.js';
2
- import type { GraphqlMiddlewareMutation } from '#build/nuxt-graphql-middleware';
1
+ import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './../helpers/composables.js';
2
+ import type { GraphqlMiddlewareMutation } from '#nuxt-graphql-middleware/generated-types';
3
3
  import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
4
4
  /**
5
5
  * Performs a GraphQL mutation.
@@ -1,8 +1,25 @@
1
+ import {
2
+ encodeContext
3
+ } from "./../helpers/composables.js";
1
4
  import { performRequest } from "./nuxtApp.js";
5
+ import { clientOptions } from "#graphql-middleware-client-options";
2
6
  export function useGraphqlMutation(...args) {
3
- const [name, body, fetchOptions = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions] : [args[0].name, args[0].variables, args[0].fetchOptions];
7
+ const [name, body, fetchOptions = {}, overrideClientContext = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.clientContext] : [
8
+ args[0].name,
9
+ args[0].variables,
10
+ args[0].fetchOptions,
11
+ args[0].clientContext
12
+ ];
13
+ const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
4
14
  return performRequest("mutation", name, "post", {
15
+ ...fetchOptions,
5
16
  body,
6
- ...fetchOptions
17
+ params: {
18
+ ...fetchOptions.params || {},
19
+ ...encodeContext({
20
+ ...globalClientContext,
21
+ ...overrideClientContext
22
+ })
23
+ }
7
24
  });
8
25
  }
@@ -1,5 +1,5 @@
1
- import { type GraphqlMiddlewareQueryName, type GetQueryArgs, type QueryObjectArgs, type GetQueryResult } from './shared.js';
2
- import type { GraphqlMiddlewareQuery } from '#build/nuxt-graphql-middleware';
1
+ import { type GraphqlMiddlewareQueryName, type GetQueryArgs, type QueryObjectArgs, type GetQueryResult } from './../helpers/composables.js';
2
+ import type { GraphqlMiddlewareQuery } from '#nuxt-graphql-middleware/generated-types';
3
3
  import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
4
4
  /**
5
5
  * Performs a GraphQL query.
@@ -1,19 +1,44 @@
1
+ import {
2
+ encodeContext
3
+ } from "./../helpers/composables.js";
1
4
  import { buildRequestParams } from "./../helpers/index.js";
2
5
  import { performRequest } from "./nuxtApp.js";
6
+ import { clientOptions } from "#graphql-middleware-client-options";
3
7
  export function useGraphqlQuery(...args) {
4
- const [name, variables, fetchOptions = {}, graphqlCaching = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.graphqlCaching] : [
8
+ const [
9
+ name,
10
+ variables,
11
+ fetchOptions = {},
12
+ graphqlCaching = {},
13
+ overrideClientContext = {}
14
+ ] = typeof args[0] === "string" ? [
15
+ args[0],
16
+ args[1],
17
+ args[2]?.fetchOptions,
18
+ args[2]?.graphqlCaching,
19
+ args[2]?.clientContext
20
+ ] : [
5
21
  args[0].name,
6
22
  args[0].variables,
7
23
  args[0].fetchOptions,
8
- args[0].graphqlCaching
24
+ args[0].graphqlCaching,
25
+ args[0].clientContext
9
26
  ];
27
+ const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
10
28
  return performRequest(
11
29
  "query",
12
30
  name,
13
31
  "get",
14
32
  {
15
- params: buildRequestParams(variables),
16
- ...fetchOptions
33
+ ...fetchOptions,
34
+ params: {
35
+ ...fetchOptions.params || {},
36
+ ...buildRequestParams(variables),
37
+ ...encodeContext({
38
+ ...globalClientContext,
39
+ ...overrideClientContext
40
+ })
41
+ }
17
42
  },
18
43
  graphqlCaching
19
44
  );
@@ -1,5 +1,5 @@
1
- import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './shared.js';
2
- import type { GraphqlMiddlewareMutation } from '#build/nuxt-graphql-middleware';
1
+ import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './../helpers/composables.js';
2
+ import type { GraphqlMiddlewareMutation } from '#nuxt-graphql-middleware/generated-types';
3
3
  import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
4
4
  /**
5
5
  * Performs a GraphQL upload mutation.
@@ -1,6 +1,8 @@
1
1
  import {
2
- getEndpoint
3
- } from "./shared.js";
2
+ getEndpoint,
3
+ encodeContext
4
+ } from "./../helpers/composables.js";
5
+ import { clientOptions } from "#graphql-middleware-client-options";
4
6
  import { useGraphqlState } from "#imports";
5
7
  function createFormData(variables) {
6
8
  const formData = new FormData();
@@ -34,7 +36,12 @@ function createFormData(variables) {
34
36
  return formData;
35
37
  }
36
38
  export function useGraphqlUploadMutation(...args) {
37
- const [name, variables, fetchOptions = {}] = typeof args[0] === "string" ? [args[0], args[1]] : [args[0].name, args[0].variables, args[0].fetchOptions];
39
+ const [name, variables, fetchOptions = {}, overrideClientContext = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.clientContext] : [
40
+ args[0].name,
41
+ args[0].variables,
42
+ args[0].fetchOptions,
43
+ args[0].clientContext
44
+ ];
38
45
  if (!variables) {
39
46
  throw new Error(
40
47
  'Using "useGraphqlUploadMutation" without variables is not supported.'
@@ -42,9 +49,18 @@ export function useGraphqlUploadMutation(...args) {
42
49
  }
43
50
  const state = useGraphqlState();
44
51
  const formData = createFormData(variables);
52
+ const globalClientContext = clientOptions.buildClientContext ? clientOptions.buildClientContext() : {};
53
+ const clientContext = encodeContext({
54
+ ...globalClientContext,
55
+ ...overrideClientContext
56
+ });
45
57
  return $fetch(getEndpoint("upload", name), {
46
58
  ...state && state.fetchOptions ? state.fetchOptions : {},
47
59
  ...fetchOptions || {},
60
+ params: {
61
+ ...clientContext,
62
+ ...fetchOptions.params || {}
63
+ },
48
64
  method: "POST",
49
65
  body: formData
50
66
  }).then((v) => {
@@ -1,11 +1,13 @@
1
1
  import type { FetchOptions } from 'ofetch';
2
- import type { GraphqlMiddlewareQuery, GraphqlMiddlewareMutation } from '#build/nuxt-graphql-middleware';
2
+ import type { GraphqlMiddlewareQuery, GraphqlMiddlewareMutation } from '#nuxt-graphql-middleware/generated-types';
3
3
  import type { RequestCacheOptions } from '#graphql-middleware/types';
4
+ import type { GraphqlClientContext } from '#graphql-middleware-client-options';
4
5
  export type GraphqlMiddlewareQueryName = keyof GraphqlMiddlewareQuery;
5
6
  export type GraphqlMiddlewareMutationName = keyof GraphqlMiddlewareMutation;
6
7
  export type GraphqlComposableOptions = {
7
8
  fetchOptions?: FetchOptions;
8
9
  graphqlCaching?: RequestCacheOptions;
10
+ clientContext?: Partial<GraphqlClientContext>;
9
11
  };
10
12
  export type GetQueryArgs<T extends GraphqlMiddlewareQueryName, M extends GraphqlMiddlewareQuery> = M[T][0] extends null ? [T, (null | undefined)?, GraphqlComposableOptions?] : M[T][1] extends false ? [T, M[T][0], GraphqlComposableOptions?] : [T, M[T][0]?, GraphqlComposableOptions?];
11
13
  export type GetMutationArgs<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T][0] extends null ? [T, (null | undefined)?, GraphqlComposableOptions?] : M[T][1] extends false ? [T, M[T][0], GraphqlComposableOptions?] : [T, M[T][0]?, GraphqlComposableOptions?];
@@ -16,21 +18,26 @@ export type QueryObjectArgs<T extends GraphqlMiddlewareQueryName, M extends Grap
16
18
  name: T;
17
19
  fetchOptions?: FetchOptions;
18
20
  graphqlCaching?: RequestCacheOptions;
21
+ clientContext?: Partial<GraphqlClientContext>;
19
22
  variables?: null;
20
23
  } : {
21
24
  name: T;
22
25
  variables: M[T][0];
23
26
  fetchOptions?: FetchOptions;
24
27
  graphqlCaching?: RequestCacheOptions;
28
+ clientContext?: Partial<GraphqlClientContext>;
25
29
  };
26
30
  export type MutationObjectArgs<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T][0] extends null ? {
27
31
  name: T;
28
32
  variables?: null;
29
33
  fetchOptions?: FetchOptions;
34
+ clientContext?: Partial<GraphqlClientContext>;
30
35
  } : {
31
36
  name: T;
32
37
  variables: M[T][0];
33
38
  fetchOptions?: FetchOptions;
39
+ clientContext?: Partial<GraphqlClientContext>;
34
40
  };
35
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;
36
42
  export type KeysOf<T> = Array<T extends T ? (keyof T extends string ? keyof T : never) : never>;
43
+ export declare function encodeContext(context: Record<string, string | null | undefined>): Record<string, string>;
@@ -0,0 +1,17 @@
1
+ import { useRuntimeConfig } from "#imports";
2
+ import { CLIENT_CONTEXT_PREFIX } from "../settings/index.js";
3
+ export function getEndpoint(operation, operationName) {
4
+ const config = useRuntimeConfig();
5
+ return `${config?.public?.["nuxt-graphql-middleware"]?.serverApiPrefix}/${operation}/${operationName}`;
6
+ }
7
+ export function encodeContext(context) {
8
+ return Object.entries(context).reduce(
9
+ (acc, [key, value]) => {
10
+ if (typeof value === "string") {
11
+ acc[CLIENT_CONTEXT_PREFIX + key] = value;
12
+ }
13
+ return acc;
14
+ },
15
+ {}
16
+ );
17
+ }
@@ -0,0 +1,3 @@
1
+ {
2
+ "extends": "../../../.nuxt/tsconfig.server.json"
3
+ }
@@ -0,0 +1,3 @@
1
+ import type { FetchOptions } from 'ofetch';
2
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
3
+ export declare function performRequest<T>(operation: string, operationName: string, method: 'get' | 'post', options: FetchOptions): Promise<GraphqlResponse<T>>;
@@ -0,0 +1,12 @@
1
+ import { getEndpoint } from "./../../helpers/composables.js";
2
+ export function performRequest(operation, operationName, method, options) {
3
+ return $fetch(getEndpoint(operation, operationName), {
4
+ ...options,
5
+ method
6
+ }).then((v) => {
7
+ return {
8
+ data: v.data,
9
+ errors: v.errors || []
10
+ };
11
+ });
12
+ }
@@ -0,0 +1,7 @@
1
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
2
+ import { type GraphqlMiddlewareMutationName, type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './../../helpers/composables.js';
3
+ import type { GraphqlMiddlewareMutation } from '#nuxt-graphql-middleware/generated-types';
4
+ /**
5
+ * Performs a GraphQL mutation.
6
+ */
7
+ export declare function useGraphqlMutation<T extends GraphqlMiddlewareMutationName, R extends GetMutationResult<T, GraphqlMiddlewareMutation>>(...args: GetMutationArgs<T, GraphqlMiddlewareMutation> | [MutationObjectArgs<T, GraphqlMiddlewareMutation>]): Promise<GraphqlResponse<R>>;
@@ -0,0 +1,20 @@
1
+ import {
2
+ encodeContext
3
+ } from "./../../helpers/composables.js";
4
+ import { performRequest } from "./index.js";
5
+ export function useGraphqlMutation(...args) {
6
+ const [name, body, fetchOptions = {}, clientContext = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.clientContext] : [
7
+ args[0].name,
8
+ args[0].variables,
9
+ args[0].fetchOptions,
10
+ args[0].clientContext
11
+ ];
12
+ return performRequest("mutation", name, "post", {
13
+ ...fetchOptions,
14
+ body,
15
+ params: {
16
+ ...encodeContext(clientContext),
17
+ ...fetchOptions.params || {}
18
+ }
19
+ });
20
+ }
@@ -0,0 +1,7 @@
1
+ import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
2
+ import { type GraphqlMiddlewareQueryName, type GetQueryArgs, type QueryObjectArgs, type GetQueryResult } from './../../helpers/composables.js';
3
+ import type { GraphqlMiddlewareQuery } from '#nuxt-graphql-middleware/generated-types';
4
+ /**
5
+ * Performs a GraphQL query.
6
+ */
7
+ export declare function useGraphqlQuery<T extends GraphqlMiddlewareQueryName, R extends GetQueryResult<T, GraphqlMiddlewareQuery>>(...args: GetQueryArgs<T, GraphqlMiddlewareQuery> | [QueryObjectArgs<T, GraphqlMiddlewareQuery>]): Promise<GraphqlResponse<R>>;
@@ -0,0 +1,21 @@
1
+ import {
2
+ encodeContext
3
+ } from "./../../helpers/composables.js";
4
+ import { buildRequestParams } from "./../../helpers/index.js";
5
+ import { performRequest } from "./index.js";
6
+ export function useGraphqlQuery(...args) {
7
+ const [name, variables, fetchOptions = {}, clientContext = {}] = typeof args[0] === "string" ? [args[0], args[1], args[2]?.fetchOptions, args[2]?.clientContext] : [
8
+ args[0].name,
9
+ args[0].variables,
10
+ args[0].fetchOptions,
11
+ args[0].clientContext
12
+ ];
13
+ return performRequest("query", name, "get", {
14
+ ...fetchOptions,
15
+ params: {
16
+ ...fetchOptions.params || {},
17
+ ...buildRequestParams(variables),
18
+ ...encodeContext(clientContext)
19
+ }
20
+ });
21
+ }
@@ -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_";