nuxt-graphql-middleware 5.0.0-alpha.2 → 5.0.0-alpha.20

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 (93) hide show
  1. package/README.md +101 -19
  2. package/dist/client/200.html +10 -10
  3. package/dist/client/404.html +10 -10
  4. package/dist/client/_nuxt/BM34SYth.js +1 -0
  5. package/dist/client/_nuxt/CROlboVl.js +1 -0
  6. package/dist/client/_nuxt/D5hBL5aZ.js +25 -0
  7. package/dist/client/_nuxt/FTbv7CO6.js +1 -0
  8. package/dist/client/_nuxt/builds/latest.json +1 -1
  9. package/dist/client/_nuxt/builds/meta/de61b0f7-ec5c-4f2f-addb-b5017c30afb1.json +1 -0
  10. package/dist/client/_nuxt/entry.Cn9qfNGa.css +1 -0
  11. package/dist/client/_nuxt/error-404.ehK72JOs.css +1 -0
  12. package/dist/client/_nuxt/error-500._g0akJim.css +1 -0
  13. package/dist/client/_nuxt/index.DGEN-H8t.css +1 -0
  14. package/dist/client/_nuxt/lIgCBhS_.js +2 -0
  15. package/dist/client/index.html +10 -10
  16. package/dist/client-options.d.mts +6 -0
  17. package/dist/client-options.mjs +5 -0
  18. package/dist/module.d.mts +74 -340
  19. package/dist/module.json +4 -4
  20. package/dist/module.mjs +1384 -589
  21. package/dist/runtime/components/CodeFrame.vue +52 -0
  22. package/dist/runtime/components/CodeFrame.vue.d.ts +7 -0
  23. package/dist/runtime/components/DevModeOverlay.vue +52 -0
  24. package/dist/runtime/components/DevModeOverlay.vue.d.ts +3 -0
  25. package/dist/runtime/components/ErrorExtensions.vue +21 -0
  26. package/dist/runtime/components/ErrorExtensions.vue.d.ts +5 -0
  27. package/dist/runtime/components/ErrorGroup.vue +78 -0
  28. package/dist/runtime/components/ErrorGroup.vue.d.ts +9 -0
  29. package/dist/runtime/composables/nuxtApp.d.ts +2 -2
  30. package/dist/runtime/composables/nuxtApp.js +21 -1
  31. package/dist/runtime/composables/useAsyncGraphqlQuery.d.ts +7 -7
  32. package/dist/runtime/composables/useAsyncGraphqlQuery.js +10 -2
  33. package/dist/runtime/composables/useGraphqlMutation.d.ts +4 -4
  34. package/dist/runtime/composables/useGraphqlMutation.js +1 -1
  35. package/dist/runtime/composables/useGraphqlQuery.d.ts +4 -4
  36. package/dist/runtime/composables/useGraphqlQuery.js +1 -1
  37. package/dist/runtime/composables/useGraphqlState.d.ts +1 -1
  38. package/dist/runtime/composables/useGraphqlState.js +1 -1
  39. package/dist/runtime/composables/useGraphqlUploadMutation.d.ts +4 -4
  40. package/dist/runtime/composables/useGraphqlUploadMutation.js +2 -2
  41. package/dist/runtime/css/output.css +1 -0
  42. package/dist/runtime/helpers/composables.d.ts +17 -20
  43. package/dist/runtime/helpers/composables.js +0 -5
  44. package/dist/runtime/plugins/devMode.d.ts +2 -0
  45. package/dist/runtime/plugins/devMode.js +23 -0
  46. package/dist/runtime/plugins/provideState.d.ts +1 -1
  47. package/dist/runtime/{serverHandler → server/api}/debug.js +3 -7
  48. package/dist/runtime/server/api/mutation.js +29 -0
  49. package/dist/runtime/server/api/query.js +30 -0
  50. package/dist/runtime/server/api/upload.d.ts +2 -0
  51. package/dist/runtime/{serverHandler → server/api}/upload.js +13 -11
  52. package/dist/runtime/{serverHandler → server}/helpers/index.d.ts +10 -12
  53. package/dist/runtime/{serverHandler → server}/helpers/index.js +9 -26
  54. package/dist/runtime/server/utils/doGraphqlRequest.d.ts +18 -0
  55. package/dist/runtime/server/utils/doGraphqlRequest.js +68 -0
  56. package/dist/runtime/server/utils/index.d.ts +1 -1
  57. package/dist/runtime/server/utils/index.js +1 -1
  58. package/dist/runtime/server/utils/useGraphqlMutation.d.ts +4 -4
  59. package/dist/runtime/server/utils/useGraphqlQuery.d.ts +4 -4
  60. package/dist/runtime/settings/index.d.ts +0 -14
  61. package/dist/runtime/settings/index.js +0 -6
  62. package/dist/runtime/types.d.ts +204 -3
  63. package/dist/server-options.d.mts +8 -0
  64. package/dist/server-options.mjs +5 -0
  65. package/dist/shared/nuxt-graphql-middleware.xfMm4rGk.d.mts +523 -0
  66. package/dist/types.d.mts +1 -7
  67. package/dist/utils.d.mts +15 -0
  68. package/dist/utils.mjs +18 -0
  69. package/package.json +64 -57
  70. package/dist/client/_nuxt/BS583yk8.js +0 -25
  71. package/dist/client/_nuxt/CZ2Qwgdk.js +0 -2
  72. package/dist/client/_nuxt/DpxjPVZy.js +0 -1
  73. package/dist/client/_nuxt/GOrnHr4p.js +0 -1
  74. package/dist/client/_nuxt/builds/meta/f823ebfd-daab-468f-8f6f-07a236da64bd.json +0 -1
  75. package/dist/client/_nuxt/entry.AjgXSF89.css +0 -1
  76. package/dist/client/_nuxt/error-404.BJkSn6RI.css +0 -1
  77. package/dist/client/_nuxt/error-500.TOCKLquH.css +0 -1
  78. package/dist/client/_nuxt/exxdaCPN.js +0 -1
  79. package/dist/client/_nuxt/index.D19Q16VT.css +0 -1
  80. package/dist/module.cjs +0 -5
  81. package/dist/module.d.ts +0 -358
  82. package/dist/runtime/clientOptions/index.d.ts +0 -2
  83. package/dist/runtime/clientOptions/index.js +0 -3
  84. package/dist/runtime/serverHandler/index.js +0 -78
  85. package/dist/runtime/serverHandler/tsconfig.json +0 -3
  86. package/dist/runtime/serverOptions/defineGraphqlServerOptions.d.ts +0 -3
  87. package/dist/runtime/serverOptions/defineGraphqlServerOptions.js +0 -3
  88. package/dist/runtime/serverOptions/index.d.ts +0 -2
  89. package/dist/runtime/serverOptions/index.js +0 -2
  90. package/dist/types.d.ts +0 -7
  91. /package/dist/runtime/{serverHandler → server/api}/debug.d.ts +0 -0
  92. /package/dist/runtime/{serverHandler/index.d.ts → server/api/mutation.d.ts} +0 -0
  93. /package/dist/runtime/{serverHandler/upload.d.ts → server/api/query.d.ts} +0 -0
@@ -0,0 +1,29 @@
1
+ import { defineEventHandler, getQuery, getRouterParam, readBody } from "h3";
2
+ import {
3
+ extractRequestContext,
4
+ isValidMutation,
5
+ throwError
6
+ } from "./../helpers/index.js";
7
+ import { GraphqlMiddlewareOperation } from "./../../settings/index.js";
8
+ import { documents } from "#nuxt-graphql-middleware/documents";
9
+ import { doGraphqlRequest } from "../utils/doGraphqlRequest.js";
10
+ export default defineEventHandler(async (event) => {
11
+ const operationName = getRouterParam(event, "name");
12
+ if (!isValidMutation(operationName)) {
13
+ return throwError("Invalid mutation name.");
14
+ }
15
+ const operationDocument = documents.mutation[operationName];
16
+ const queryParams = getQuery(event);
17
+ const context = extractRequestContext(queryParams);
18
+ const variables = await readBody(event);
19
+ return doGraphqlRequest(
20
+ {
21
+ query: operationDocument,
22
+ variables,
23
+ operation: GraphqlMiddlewareOperation.Mutation,
24
+ operationName
25
+ },
26
+ context,
27
+ event
28
+ );
29
+ });
@@ -0,0 +1,30 @@
1
+ import { defineEventHandler, getQuery, getRouterParam } from "h3";
2
+ import {
3
+ queryParamToVariables,
4
+ extractRequestContext,
5
+ isValidQuery,
6
+ throwError
7
+ } from "./../helpers/index.js";
8
+ import { GraphqlMiddlewareOperation } from "./../../settings/index.js";
9
+ import { documents } from "#nuxt-graphql-middleware/documents";
10
+ import { doGraphqlRequest } from "../utils/doGraphqlRequest.js";
11
+ export default defineEventHandler(async (event) => {
12
+ const operationName = getRouterParam(event, "name");
13
+ if (!isValidQuery(operationName)) {
14
+ return throwError("Invalid query name.");
15
+ }
16
+ const operationDocument = documents.query[operationName];
17
+ const queryParams = getQuery(event);
18
+ const context = extractRequestContext(queryParams);
19
+ const variables = queryParamToVariables(queryParams);
20
+ return doGraphqlRequest(
21
+ {
22
+ query: operationDocument,
23
+ variables,
24
+ operation: GraphqlMiddlewareOperation.Query,
25
+ operationName
26
+ },
27
+ context,
28
+ event
29
+ );
30
+ });
@@ -0,0 +1,2 @@
1
+ declare const _default: import("h3").EventHandler<import("h3").EventHandlerRequest, Promise<any>>;
2
+ export default _default;
@@ -1,21 +1,22 @@
1
1
  import {
2
2
  defineEventHandler,
3
3
  readMultipartFormData,
4
- getQuery
4
+ getQuery,
5
+ getRouterParam
5
6
  } from "h3";
6
7
  import {
7
8
  getEndpoint,
8
9
  getFetchOptions,
9
- validateRequest,
10
10
  onServerResponse,
11
11
  onServerError,
12
12
  throwError,
13
- extractRequestContext
14
- } from "./helpers/index.js";
15
- import { GraphqlMiddlewareOperation } from "./../settings/index.js";
16
- import { operations } from "#graphql-documents";
17
- import { serverOptions } from "#graphql-middleware-server-options-build";
13
+ extractRequestContext,
14
+ isValidMutation
15
+ } from "./../helpers/index.js";
16
+ import { documents } from "#nuxt-graphql-middleware/documents";
17
+ import { serverOptions } from "#nuxt-graphql-middleware/server-options";
18
18
  import { useRuntimeConfig } from "#imports";
19
+ import { GraphqlMiddlewareOperation } from "../../settings/index.js";
19
20
  function parseMultipart(data) {
20
21
  const files = data.filter((v) => !!v.filename);
21
22
  const variablesData = data.find((v) => v.name === "variables")?.data?.toString();
@@ -27,11 +28,12 @@ function parseMultipart(data) {
27
28
  return { files, map, variables };
28
29
  }
29
30
  export default defineEventHandler(async (event) => {
30
- const method = event.method;
31
31
  const operation = GraphqlMiddlewareOperation.Mutation;
32
- const operationName = event.context?.params?.name;
33
- validateRequest(method, operation, operationName, operations);
34
- const operationDocument = operations[operation][operationName];
32
+ const operationName = getRouterParam(event, "name");
33
+ if (!isValidMutation(operationName)) {
34
+ return throwError("Invalid mutation name.");
35
+ }
36
+ const operationDocument = documents.mutation[operationName];
35
37
  const multiPartData = await readMultipartFormData(event);
36
38
  if (!multiPartData) {
37
39
  return throwError("Failed to read multi part data.");
@@ -1,9 +1,11 @@
1
- import { type QueryObject } from 'ufo';
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 { GraphqlMiddlewareRequestContext, GraphqlMiddlewareRuntimeConfig, GraphqlMiddlewareServerOptions } from './../../../types.js';
5
- import { GraphqlMiddlewareOperation } from './../../settings/index.js';
6
- import { type Operations } from '#graphql-documents';
4
+ import type { GraphqlMiddlewareRequestContext, GraphqlMiddlewareRuntimeConfig, GraphqlMiddlewareServerOptions } from './../../types.js';
5
+ import { type GraphqlMiddlewareOperation } from './../../settings/index.js';
6
+ import type { Mutation, Query } from '#nuxt-graphql-middleware/operation-types';
7
+ export declare function isValidMutation(v?: string): v is keyof Mutation;
8
+ export declare function isValidQuery(v?: string): v is keyof Query;
7
9
  export declare function queryParamToVariables(query: QueryObject): any;
8
10
  /**
9
11
  * Extract the client context from the query params.
@@ -12,21 +14,17 @@ export declare function extractRequestContext(query: QueryObject): GraphqlMiddle
12
14
  /**
13
15
  * Get the URL of the GraphQL endpoint.
14
16
  */
15
- export declare function getEndpoint(config: GraphqlMiddlewareRuntimeConfig, serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, operation: GraphqlMiddlewareOperation, operationName: string, context: GraphqlMiddlewareRequestContext<any>): string | Promise<string>;
17
+ export declare function getEndpoint(config: GraphqlMiddlewareRuntimeConfig, serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, operation: GraphqlMiddlewareOperation | null, operationName: string | null, context: GraphqlMiddlewareRequestContext<any> | null): string | Promise<string>;
16
18
  /**
17
19
  * Get the options for the $fetch request to the GraphQL server.
18
20
  */
19
- export declare function getFetchOptions(serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, operation: GraphqlMiddlewareOperation, operationName: string, context: GraphqlMiddlewareRequestContext<any>): FetchOptions | Promise<FetchOptions>;
21
+ export declare function getFetchOptions(serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, operation: GraphqlMiddlewareOperation | null, operationName: string | null, context: GraphqlMiddlewareRequestContext<any> | null): FetchOptions | Promise<FetchOptions>;
20
22
  export declare function throwError(statusMessage: string, statusCode?: number): never;
21
- /**
22
- * Assure that the request is valid.
23
- */
24
- export declare function validateRequest(method?: string, operation?: GraphqlMiddlewareOperation | string, name?: string, documents?: Operations): void;
25
23
  /**
26
24
  * Handle GraphQL server response.
27
25
  */
28
- export declare function onServerResponse(serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, response: FetchResponse<any>, operation: string, operationName: string, context: GraphqlMiddlewareRequestContext<any>): any;
26
+ export declare function onServerResponse(serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, response: FetchResponse<any>, operation: string | null, operationName: string | null, context: GraphqlMiddlewareRequestContext<any> | null): any;
29
27
  /**
30
28
  * Handle GraphQL server errors.
31
29
  */
32
- export declare function onServerError(serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, error: FetchError, operation: string, operationName: string, context: GraphqlMiddlewareRequestContext<any>): any;
30
+ export declare function onServerError(serverOptions: GraphqlMiddlewareServerOptions<any, any>, event: H3Event, error: FetchError, operation: string | null, operationName: string | null, context: GraphqlMiddlewareRequestContext<any> | null): any;
@@ -1,14 +1,20 @@
1
1
  import { createError } from "h3";
2
2
  import {
3
- CLIENT_CONTEXT_PREFIX,
4
- GraphqlMiddlewareOperation
3
+ CLIENT_CONTEXT_PREFIX
5
4
  } from "./../../settings/index.js";
5
+ import { documents } from "#nuxt-graphql-middleware/documents";
6
+ export function isValidMutation(v) {
7
+ return !!v && Object.hasOwn(documents.mutation, v);
8
+ }
9
+ export function isValidQuery(v) {
10
+ return !!v && Object.hasOwn(documents.query, v);
11
+ }
6
12
  export function queryParamToVariables(query) {
7
13
  try {
8
14
  if (query.__variables && typeof query.__variables === "string") {
9
15
  return JSON.parse(query.__variables);
10
16
  }
11
- } catch (_e) {
17
+ } catch {
12
18
  }
13
19
  return query;
14
20
  }
@@ -61,29 +67,6 @@ export function throwError(statusMessage, statusCode = 400) {
61
67
  statusMessage
62
68
  });
63
69
  }
64
- export function validateRequest(method, operation, name, documents) {
65
- if (method !== "POST" && method !== "GET") {
66
- throwError("Method not allowed.", 405);
67
- }
68
- if (operation !== GraphqlMiddlewareOperation.Query && operation !== GraphqlMiddlewareOperation.Mutation) {
69
- throwError("Unknown operation.");
70
- }
71
- if (method === "POST" && operation !== GraphqlMiddlewareOperation.Mutation) {
72
- throwError("Queries must be a GET request.");
73
- }
74
- if (method === "GET" && operation !== GraphqlMiddlewareOperation.Query) {
75
- throwError("Mutations must be a POST request.");
76
- }
77
- if (!name) {
78
- throwError("Missing name for operation.");
79
- }
80
- if (!documents) {
81
- throwError("Failed to load GraphQL documents", 500);
82
- }
83
- if (!documents[operation][name]) {
84
- throwError(`Operation "${operation}" with name "${name}" not found.`);
85
- }
86
- }
87
70
  export function onServerResponse(serverOptions, event, response, operation, operationName, context) {
88
71
  if (serverOptions.onServerResponse) {
89
72
  return serverOptions.onServerResponse(
@@ -0,0 +1,18 @@
1
+ import type { H3Event } from 'h3';
2
+ import type { GraphqlMiddlewareRequestContext } from '../../types.js';
3
+ import type { GraphqlMiddlewareOperation } from '../../settings/index.js';
4
+ type RequestBody = {
5
+ query: string;
6
+ variables?: Record<string, any>;
7
+ operation?: GraphqlMiddlewareOperation;
8
+ operationName?: string;
9
+ };
10
+ /**
11
+ * Perform a raw GraphQL request.
12
+ *
13
+ * @param body - The request.
14
+ * @param context - The client context.
15
+ * @param event - The H3 event. If not provided, the util will try to get the event using useEvent().
16
+ */
17
+ export declare function doGraphqlRequest(body: RequestBody, context?: GraphqlMiddlewareRequestContext | null | undefined, providedEvent?: H3Event | null | undefined): Promise<any>;
18
+ export {};
@@ -0,0 +1,68 @@
1
+ import { serverOptions } from "#nuxt-graphql-middleware/server-options";
2
+ import { useRuntimeConfig } from "#imports";
3
+ import { useEvent } from "nitropack/runtime";
4
+ import {
5
+ getEndpoint,
6
+ getFetchOptions,
7
+ onServerError,
8
+ onServerResponse
9
+ } from "../helpers/index.js";
10
+ export async function doGraphqlRequest(body, context = null, providedEvent = null) {
11
+ const event = providedEvent ?? useEvent();
12
+ if (serverOptions.doGraphqlRequest) {
13
+ return serverOptions.doGraphqlRequest({
14
+ event,
15
+ operation: body.operation,
16
+ operationName: body.operationName,
17
+ operationDocument: body.query,
18
+ variables: body.variables || {},
19
+ context: context || {}
20
+ });
21
+ }
22
+ const operation = body.operation || null;
23
+ const operationName = body.operationName || null;
24
+ const runtimeConfig = useRuntimeConfig().graphqlMiddleware;
25
+ const endpoint = await getEndpoint(
26
+ runtimeConfig,
27
+ serverOptions,
28
+ event,
29
+ null,
30
+ operationName,
31
+ context || null
32
+ );
33
+ const fetchOptions = await getFetchOptions(
34
+ serverOptions,
35
+ event,
36
+ null,
37
+ body.operationName || null,
38
+ context
39
+ );
40
+ return $fetch.raw(endpoint, {
41
+ // @ts-expect-error Not yet been fixed in nitro.
42
+ method: "POST",
43
+ body: {
44
+ query: body.query,
45
+ variables: body.variables,
46
+ operationName: body.operationName
47
+ },
48
+ ...fetchOptions
49
+ }).then((response) => {
50
+ return onServerResponse(
51
+ serverOptions,
52
+ event,
53
+ response,
54
+ operation,
55
+ operationName,
56
+ context
57
+ );
58
+ }).catch((error) => {
59
+ return onServerError(
60
+ serverOptions,
61
+ event,
62
+ error,
63
+ operation,
64
+ operationName,
65
+ context
66
+ );
67
+ });
68
+ }
@@ -1,3 +1,3 @@
1
1
  import type { FetchOptions } from 'ofetch';
2
- import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
2
+ import type { GraphqlResponse } from '#nuxt-graphql-middleware/response';
3
3
  export declare function performRequest<T>(operation: string, operationName: string, method: 'get' | 'post', options: FetchOptions): Promise<GraphqlResponse<T>>;
@@ -1,4 +1,4 @@
1
- import { getEndpoint } from "./../../helpers/composables.js";
1
+ import { getEndpoint } from "#nuxt-graphql-middleware/helpers";
2
2
  export function performRequest(operation, operationName, method, options) {
3
3
  return $fetch(getEndpoint(operation, operationName), {
4
4
  ...options,
@@ -1,7 +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';
1
+ import type { GraphqlResponse } from '#nuxt-graphql-middleware/response';
2
+ import { type GetMutationArgs, type MutationObjectArgs, type GetMutationResult } from './../../helpers/composables.js';
3
+ import type { Mutation } from '#nuxt-graphql-middleware/operation-types';
4
4
  /**
5
5
  * Performs a GraphQL mutation.
6
6
  */
7
- export declare function useGraphqlMutation<T extends GraphqlMiddlewareMutationName, R extends GetMutationResult<T, GraphqlMiddlewareMutation>>(...args: GetMutationArgs<T, GraphqlMiddlewareMutation> | [MutationObjectArgs<T, GraphqlMiddlewareMutation>]): Promise<GraphqlResponse<R>>;
7
+ export declare function useGraphqlMutation<K extends keyof Mutation, R extends GetMutationResult<K>>(...args: GetMutationArgs<K> | [MutationObjectArgs<K>]): Promise<GraphqlResponse<R>>;
@@ -1,7 +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';
1
+ import type { GraphqlResponse } from '#nuxt-graphql-middleware/response';
2
+ import { type GetQueryArgs, type QueryObjectArgs, type GetQueryResult } from './../../helpers/composables.js';
3
+ import type { Query } from '#nuxt-graphql-middleware/operation-types';
4
4
  /**
5
5
  * Performs a GraphQL query.
6
6
  */
7
- export declare function useGraphqlQuery<T extends GraphqlMiddlewareQueryName, R extends GetQueryResult<T, GraphqlMiddlewareQuery>>(...args: GetQueryArgs<T, GraphqlMiddlewareQuery> | [QueryObjectArgs<T, GraphqlMiddlewareQuery>]): Promise<GraphqlResponse<R>>;
7
+ export declare function useGraphqlQuery<K extends keyof Query, R extends GetQueryResult<K>>(...args: GetQueryArgs<K> | [QueryObjectArgs<K>]): Promise<GraphqlResponse<R>>;
@@ -1,17 +1,3 @@
1
- export declare enum GraphqlMiddlewareTemplate {
2
- /**
3
- * Contains the TS definitions for all GraphQL queries, mutations and fragments.
4
- */
5
- OperationTypes = "graphql-operations.ts",
6
- /**
7
- * Signature for the GraphQL composable arguments and return types.
8
- */
9
- ComposableContext = "nuxt-graphql-middleware/generated-types.d.ts",
10
- /**
11
- * Exports a single opject containing the compiled queries and mutations.
12
- */
13
- Documents = "nuxt-graphql-middleware/graphql-documents.mjs"
14
- }
15
1
  export declare enum GraphqlMiddlewareOperation {
16
2
  Query = "query",
17
3
  Mutation = "mutation"
@@ -1,9 +1,3 @@
1
- export var GraphqlMiddlewareTemplate = /* @__PURE__ */ ((GraphqlMiddlewareTemplate2) => {
2
- GraphqlMiddlewareTemplate2["OperationTypes"] = "graphql-operations.ts";
3
- GraphqlMiddlewareTemplate2["ComposableContext"] = "nuxt-graphql-middleware/generated-types.d.ts";
4
- GraphqlMiddlewareTemplate2["Documents"] = "nuxt-graphql-middleware/graphql-documents.mjs";
5
- return GraphqlMiddlewareTemplate2;
6
- })(GraphqlMiddlewareTemplate || {});
7
1
  export var GraphqlMiddlewareOperation = /* @__PURE__ */ ((GraphqlMiddlewareOperation2) => {
8
2
  GraphqlMiddlewareOperation2["Query"] = "query";
9
3
  GraphqlMiddlewareOperation2["Mutation"] = "mutation";
@@ -1,6 +1,12 @@
1
- import type { FetchOptions, FetchContext } from 'ofetch';
2
- import type { GraphqlResponse } from '#graphql-middleware-server-options-build';
3
- import type { GraphqlMiddlewareResponseUnion } from '#nuxt-graphql-middleware/generated-types';
1
+ import type { GraphqlMiddlewareResponseUnion, GraphqlResponse } from '#nuxt-graphql-middleware/response';
2
+ import type { H3Event } from 'h3';
3
+ import type { FetchOptions, FetchResponse, FetchError, FetchContext } from 'ofetch';
4
+ export type OperationResponseError = {
5
+ operation: string;
6
+ operationName: string;
7
+ errors: GraphqlResponseError[];
8
+ stack?: string;
9
+ };
4
10
  export type GraphqlResponseErrorLocation = {
5
11
  line: number;
6
12
  column: number;
@@ -9,6 +15,7 @@ export type GraphqlResponseError = {
9
15
  message: string;
10
16
  locations: GraphqlResponseErrorLocation[];
11
17
  path: string[];
18
+ extensions?: Record<string, unknown>;
12
19
  };
13
20
  export type GraphqlServerResponse<T> = {
14
21
  data: T;
@@ -78,3 +85,197 @@ export type GraphqlClientOptions<T extends ContextType = ContextType> = {
78
85
  */
79
86
  buildClientContext?: () => T;
80
87
  };
88
+ export type GraphqlMiddlewareRequestContext<C extends ContextType = ContextType> = {
89
+ client?: Partial<C>;
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>;
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
+ export type GraphqlMiddlewareOnServerErrorMethod<C extends ContextType> = (event: H3Event, error: FetchError, operation?: string | null, operationName?: string | null, context?: GraphqlMiddlewareRequestContext<C> | null) => any | Promise<any>;
95
+ export type GraphqlMiddlewareDoRequestMethodContext<C extends ContextType> = {
96
+ /**
97
+ * The incoming request event from H3.
98
+ */
99
+ event: H3Event;
100
+ /**
101
+ * The type of operation.
102
+ */
103
+ operation?: 'query' | 'mutation';
104
+ /**
105
+ * The name of the operation.
106
+ */
107
+ operationName?: string;
108
+ /**
109
+ * The operation document (the raw GraphQL query/mutation as a string).
110
+ */
111
+ operationDocument: string;
112
+ /**
113
+ * Variables for the operation.
114
+ */
115
+ variables: Record<string, any>;
116
+ /**
117
+ * For file uploads (which are done using FormData), this contains the full
118
+ * form data with these keys:
119
+ *
120
+ * - operations: string
121
+ * A JSON string of an object with "query", "variables" and "operationName" properties.
122
+ * Example:
123
+ * operations='{ "query": "mutation ($id: String!, $file: Upload!) { uploadFile(id: $id, file: $file) { id filename } }", "variables": { "file": null, "id": "1" } }'
124
+ * - map: string
125
+ * A JSON string of an object whose keys are strings starting at '0' and the values being an array with a single string:
126
+ * Example:
127
+ * map='{ "0": ["variables.file"] }'
128
+ * - [number]
129
+ * For every file there is a FormData entry with a name that matches the key in `map`.
130
+ * Example:
131
+ * 0=[Binary File]
132
+ */
133
+ formData?: FormData;
134
+ context: GraphqlMiddlewareRequestContext<C>;
135
+ };
136
+ export type GraphqlMiddlewareDoRequestMethod<T, C extends ContextType> = (context: GraphqlMiddlewareDoRequestMethodContext<C>) => Promise<T>;
137
+ /**
138
+ * Configuration options during runtime.
139
+ */
140
+ export type GraphqlMiddlewareServerOptions<Additions extends object = object, ResponseUnion extends object = object, C extends ContextType = ContextType, CustomResponse = GraphqlServerResponse<ResponseUnion> & Additions> = {
141
+ /**
142
+ * Custom callback to return the GraphQL endpoint per request.
143
+ *
144
+ * The method is only called if no `doGraphqlRequest` method is implemented.
145
+ *
146
+ * @default undefined
147
+ *
148
+ * @example
149
+ * ```ts
150
+ * function graphqlEndpoint(event, operation, operationName) {
151
+ * const language = getLanguageFromRequest(event)
152
+ * return `https://api.example.com/${language}/graphql`
153
+ * }
154
+ * ```
155
+ */
156
+ graphqlEndpoint?: GraphqlMiddlewareGraphqlEndpointMethod<C>;
157
+ /**
158
+ * Provide the options for the ofetch request to the GraphQL server.
159
+ *
160
+ * The method is only called if no `doGraphqlRequest` method is implemented.
161
+ *
162
+ * @default undefined
163
+ *
164
+ * @example
165
+ * ```ts
166
+ * import { getHeader } from 'h3'
167
+ *
168
+ * // Pass the cookie from the client request to the GraphQL request.
169
+ * function serverFetchOptions(event, operation, operationName) {
170
+ * return {
171
+ * headers: {
172
+ * Cookie: getHeader(event, 'cookie')
173
+ * }
174
+ * }
175
+ * }
176
+ * ```
177
+ */
178
+ serverFetchOptions?: GraphqlMiddlewareServerFetchOptionsMethod<C>;
179
+ /**
180
+ * Handle the response from the GraphQL server.
181
+ *
182
+ * The method is only called if no `doGraphqlRequest` method is implemented.
183
+ *
184
+ * You can alter the response, add additional properties to the data, get
185
+ * and set headers, etc.
186
+ *
187
+ * ```ts
188
+ * import type { H3Event } from 'h3'
189
+ * import type { FetchResponse } from 'ofetch'
190
+ *
191
+ * function onServerResponse(event: H3Event, graphqlResponse: FetchResponse) {
192
+ * // Set a static header.
193
+ * event.node.res.setHeader('x-nuxt-custom-header', 'A custom header value')
194
+ *
195
+ * // Pass the set-cookie header from the GraphQL response to the client.
196
+ * const setCookie = graphqlResponse.headers.get('set-cookie')
197
+ * if (setCookie) {
198
+ * event.node.res.setHeader('set-cookie', setCookie)
199
+ * }
200
+ *
201
+ * // Add additional properties to the response.
202
+ * graphqlResponse._data.__customProperty = ['My', 'values']
203
+ *
204
+ * // Return the GraphQL response.
205
+ * return graphqlResponse._data
206
+ * }
207
+ * ```
208
+ */
209
+ onServerResponse?: GraphqlMiddlewareOnServerResponseMethod<GraphqlServerResponse<ResponseUnion>, CustomResponse, C>;
210
+ /**
211
+ * Handle a fetch error from the GraphQL request.
212
+ *
213
+ * The method is only called if no `doGraphqlRequest` method is implemented.
214
+ *
215
+ * Note that errors are only thrown for responses that are not status
216
+ * 200-299. See https://github.com/unjs/ofetch#%EF%B8%8F-handling-errors for
217
+ * more information.
218
+ *
219
+ * ```ts
220
+ * import { createError } from 'h3'
221
+ * import type { H3Event } from 'h3'
222
+ * import type { FetchError } from 'ofetch'
223
+ *
224
+ * function onServerError(
225
+ * event: H3Event,
226
+ * error: FetchError,
227
+ * operation: string,
228
+ * operationName: string,
229
+ * ) {
230
+ * // Throw a h3 error.
231
+ * throw createError({
232
+ * statusCode: 500,
233
+ * statusMessage: `Couldn't execute GraphQL ${operation} "${operationName}".`,
234
+ * data: error.message
235
+ * })
236
+ * }
237
+ * ```
238
+ */
239
+ onServerError?: GraphqlMiddlewareOnServerErrorMethod<C>;
240
+ /**
241
+ * Provide a custom fetch method for requests to the GraphQL server.
242
+ *
243
+ * This can be used if onServerError, onServerResponse, serverFetchOptions
244
+ * and graphqlEndpoint are not enough to meet your requirements.
245
+ *
246
+ * When this method is implemented, all other methods are not called.
247
+ *
248
+ * The method will be called in the /api/graphql server route and should
249
+ * perform the GraphQL request and return the response.
250
+ *
251
+ * An example use case might be to handle expired tokens.
252
+ *
253
+ * * ```ts
254
+ * async function doGraphqlRequest({
255
+ * event,
256
+ * operation,
257
+ * operationName,
258
+ * operationDocument,
259
+ * variables,
260
+ * }) {
261
+ * const result = await $fetch.raw('https://example.com/graphql', {
262
+ * method: 'POST'
263
+ * body: {
264
+ * query: operationDocument,
265
+ * variables,
266
+ * operationName
267
+ * },
268
+ * headers: {
269
+ * 'custom-header': 'foobar'
270
+ * }
271
+ * })
272
+ *
273
+ * return result._data
274
+ * }
275
+ * ```
276
+ */
277
+ doGraphqlRequest?: GraphqlMiddlewareDoRequestMethod<CustomResponse, C>;
278
+ };
279
+ export type GraphqlMiddlewareRuntimeConfig = {
280
+ graphqlEndpoint?: string;
281
+ };
@@ -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 };