nuxt-graphql-middleware 5.0.0-alpha.0 → 5.0.0-alpha.10
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.
- package/dist/client/200.html +11 -0
- package/dist/client/404.html +11 -0
- package/dist/client/_nuxt/BLvMh1Ga.js +1 -0
- package/dist/client/_nuxt/C9pb_2rp.js +2 -0
- package/dist/client/_nuxt/CBwfSTyQ.js +1 -0
- package/dist/client/_nuxt/CPyoLiCY.js +1 -0
- package/dist/client/_nuxt/VpkRx2_e.js +25 -0
- package/dist/client/_nuxt/builds/latest.json +1 -0
- package/dist/client/_nuxt/builds/meta/074548b5-7348-4637-940b-ab6e87b521a5.json +1 -0
- package/dist/client/_nuxt/entry.Cn9qfNGa.css +1 -0
- package/dist/client/_nuxt/error-404.BJkSn6RI.css +1 -0
- package/dist/client/_nuxt/error-500.TOCKLquH.css +1 -0
- package/dist/client/_nuxt/index.DGEN-H8t.css +1 -0
- package/dist/client/index.html +11 -0
- package/dist/module.d.mts +57 -205
- package/dist/module.d.ts +57 -205
- package/dist/module.json +2 -2
- package/dist/module.mjs +1132 -565
- package/dist/runtime/components/CodeFrame.vue +61 -0
- package/dist/runtime/components/DevModeOverlay.vue +60 -0
- package/dist/runtime/components/ErrorExtensions.vue +23 -0
- package/dist/runtime/components/ErrorGroup.vue +89 -0
- package/dist/runtime/composables/nuxtApp.d.ts +2 -2
- package/dist/runtime/composables/nuxtApp.js +21 -1
- package/dist/runtime/composables/useAsyncGraphqlQuery.d.ts +7 -7
- package/dist/runtime/composables/useAsyncGraphqlQuery.js +10 -2
- package/dist/runtime/composables/useGraphqlMutation.d.ts +4 -4
- package/dist/runtime/composables/useGraphqlMutation.js +1 -1
- package/dist/runtime/composables/useGraphqlQuery.d.ts +4 -4
- package/dist/runtime/composables/useGraphqlQuery.js +1 -1
- package/dist/runtime/composables/useGraphqlState.d.ts +1 -1
- package/dist/runtime/composables/useGraphqlState.js +1 -1
- package/dist/runtime/composables/useGraphqlUploadMutation.d.ts +4 -4
- package/dist/runtime/composables/useGraphqlUploadMutation.js +2 -2
- package/dist/runtime/css/output.css +1 -0
- package/dist/runtime/helpers/composables.d.ts +17 -20
- package/dist/runtime/helpers/composables.js +0 -5
- package/dist/runtime/plugins/devMode.d.ts +2 -0
- package/dist/runtime/plugins/devMode.js +23 -0
- package/dist/runtime/plugins/provideState.d.ts +1 -1
- package/dist/runtime/{serverHandler → server/api}/debug.js +3 -7
- package/dist/runtime/server/api/mutation.js +28 -0
- package/dist/runtime/server/api/query.js +29 -0
- package/dist/runtime/server/api/upload.d.ts +2 -0
- package/dist/runtime/{serverHandler → server/api}/upload.js +13 -11
- package/dist/runtime/{serverHandler → server}/helpers/index.d.ts +10 -12
- package/dist/runtime/{serverHandler → server}/helpers/index.js +9 -26
- package/dist/runtime/server/utils/doGraphqlRequest.d.ts +18 -0
- package/dist/runtime/server/utils/doGraphqlRequest.js +67 -0
- package/dist/runtime/server/utils/index.d.ts +1 -1
- package/dist/runtime/server/utils/index.js +1 -1
- package/dist/runtime/server/utils/useGraphqlMutation.d.ts +4 -4
- package/dist/runtime/server/utils/useGraphqlQuery.d.ts +4 -4
- package/dist/runtime/serverOptions/defineGraphqlServerOptions.d.ts +4 -3
- package/dist/runtime/settings/index.d.ts +0 -14
- package/dist/runtime/settings/index.js +0 -6
- package/dist/runtime/types.d.ts +204 -3
- package/dist/types.d.mts +5 -5
- package/dist/types.d.ts +5 -5
- package/package.json +39 -36
- package/dist/runtime/serverHandler/index.js +0 -78
- package/dist/runtime/serverHandler/tsconfig.json +0 -3
- /package/dist/runtime/{serverHandler → server/api}/debug.d.ts +0 -0
- /package/dist/runtime/{serverHandler/index.d.ts → server/api/mutation.d.ts} +0 -0
- /package/dist/runtime/{serverHandler/upload.d.ts → server/api/query.d.ts} +0 -0
|
@@ -0,0 +1,29 @@
|
|
|
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
|
+
},
|
|
26
|
+
context,
|
|
27
|
+
event
|
|
28
|
+
);
|
|
29
|
+
});
|
|
@@ -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
|
-
|
|
15
|
-
|
|
16
|
-
import {
|
|
17
|
-
import { serverOptions } from "#graphql-middleware
|
|
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
|
|
33
|
-
|
|
34
|
-
|
|
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 {
|
|
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 '
|
|
5
|
-
import { GraphqlMiddlewareOperation } from './../../settings/index.js';
|
|
6
|
-
import {
|
|
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
|
|
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,67 @@
|
|
|
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 operationName = body.operationName || null;
|
|
12
|
+
const event = providedEvent ?? useEvent();
|
|
13
|
+
const runtimeConfig = useRuntimeConfig().graphqlMiddleware;
|
|
14
|
+
if (serverOptions.doGraphqlRequest) {
|
|
15
|
+
return serverOptions.doGraphqlRequest({
|
|
16
|
+
event,
|
|
17
|
+
operation: body.operation,
|
|
18
|
+
operationName: body.operationName,
|
|
19
|
+
operationDocument: body.query,
|
|
20
|
+
variables: body.variables || {},
|
|
21
|
+
context: context || {}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
const endpoint = await getEndpoint(
|
|
25
|
+
runtimeConfig,
|
|
26
|
+
serverOptions,
|
|
27
|
+
event,
|
|
28
|
+
null,
|
|
29
|
+
operationName,
|
|
30
|
+
context || null
|
|
31
|
+
);
|
|
32
|
+
const fetchOptions = await getFetchOptions(
|
|
33
|
+
serverOptions,
|
|
34
|
+
event,
|
|
35
|
+
null,
|
|
36
|
+
body.operationName || null,
|
|
37
|
+
context
|
|
38
|
+
);
|
|
39
|
+
return $fetch.raw(endpoint, {
|
|
40
|
+
// @ts-expect-error Not yet been fixed in nitro.
|
|
41
|
+
method: "POST",
|
|
42
|
+
body: {
|
|
43
|
+
query: body.query,
|
|
44
|
+
variables: body.variables,
|
|
45
|
+
operationName: body.operationName
|
|
46
|
+
},
|
|
47
|
+
...fetchOptions
|
|
48
|
+
}).then((response) => {
|
|
49
|
+
return onServerResponse(
|
|
50
|
+
serverOptions,
|
|
51
|
+
event,
|
|
52
|
+
response,
|
|
53
|
+
null,
|
|
54
|
+
operationName,
|
|
55
|
+
context
|
|
56
|
+
);
|
|
57
|
+
}).catch((error) => {
|
|
58
|
+
return onServerError(
|
|
59
|
+
serverOptions,
|
|
60
|
+
event,
|
|
61
|
+
error,
|
|
62
|
+
null,
|
|
63
|
+
operationName,
|
|
64
|
+
context
|
|
65
|
+
);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { FetchOptions } from 'ofetch';
|
|
2
|
-
import type { GraphqlResponse } from '#graphql-middleware
|
|
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 "
|
|
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
|
|
2
|
-
import { type
|
|
3
|
-
import type {
|
|
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<
|
|
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
|
|
2
|
-
import { type
|
|
3
|
-
import type {
|
|
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<
|
|
7
|
+
export declare function useGraphqlQuery<K extends keyof Query, R extends GetQueryResult<K>>(...args: GetQueryArgs<K> | [QueryObjectArgs<K>]): Promise<GraphqlResponse<R>>;
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import type { GraphqlClientContext } from '#graphql-middleware
|
|
2
|
-
import {
|
|
3
|
-
|
|
1
|
+
import type { GraphqlClientContext } from '#nuxt-graphql-middleware/client-options';
|
|
2
|
+
import type { GraphqlMiddlewareResponseUnion } from '#nuxt-graphql-middleware/response';
|
|
3
|
+
import type { GraphqlMiddlewareServerOptions } from './../types.js';
|
|
4
|
+
export declare function defineGraphqlServerOptions<T extends object>(options: GraphqlMiddlewareServerOptions<T, GraphqlMiddlewareResponseUnion, GraphqlClientContext>): GraphqlMiddlewareServerOptions<T, GraphqlMiddlewareResponseUnion, GraphqlClientContext>;
|
|
@@ -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.d.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.d.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";
|
package/dist/runtime/types.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type {
|
|
3
|
-
import type {
|
|
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
|
+
};
|
package/dist/types.d.mts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NuxtModule } from '@nuxt/schema'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
interface NuxtHooks extends ModuleHooks {}
|
|
5
|
-
}
|
|
3
|
+
import type { default as Module } from './module.js'
|
|
6
4
|
|
|
7
|
-
export
|
|
5
|
+
export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
|
|
6
|
+
|
|
7
|
+
export { type GraphqlMiddlewareServerOptions } from './module.js'
|
package/dist/types.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { NuxtModule } from '@nuxt/schema'
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
interface NuxtHooks extends ModuleHooks {}
|
|
5
|
-
}
|
|
3
|
+
import type { default as Module } from './module'
|
|
6
4
|
|
|
7
|
-
export
|
|
5
|
+
export type ModuleOptions = typeof Module extends NuxtModule<infer O> ? Partial<O> : Record<string, any>
|
|
6
|
+
|
|
7
|
+
export { type GraphqlMiddlewareServerOptions } from './module'
|