nuxt-graphql-middleware 4.0.0-beta.7 → 4.0.0-beta.9

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/module.d.mts CHANGED
@@ -9,6 +9,32 @@ type GraphqlMiddlewareGraphqlEndpointMethod = (event?: H3Event, operation?: stri
9
9
  type GraphqlMiddlewareServerFetchOptionsMethod = (event?: H3Event, operation?: string, operationName?: string) => FetchOptions | Promise<FetchOptions>;
10
10
  type GraphqlMiddlewareOnServerResponseMethod = (event: H3Event, response: FetchResponse<any>, operation?: string, operationName?: string) => any | Promise<any>;
11
11
  type GraphqlMiddlewareOnServerErrorMethod = (event: H3Event, error: FetchError, operation?: string, operationName?: string) => any | Promise<any>;
12
+ type GraphqlMiddlewareDoRequestMethodContext = {
13
+ /**
14
+ * The incoming request event from H3.
15
+ */
16
+ event: H3Event;
17
+ /**
18
+ * The type of operation.
19
+ */
20
+ operation: 'query' | 'mutation';
21
+ /**
22
+ * The name of the operation.
23
+ */
24
+ operationName: string;
25
+ /**
26
+ * The operation document (the raw GraphQL query/mutation as a string).
27
+ */
28
+ operationDocument: string;
29
+ /**
30
+ * Variables for the operation.
31
+ */
32
+ variables: Record<string, any>;
33
+ };
34
+ type GraphqlMiddlewareDoRequestMethod = (context: GraphqlMiddlewareDoRequestMethodContext) => Promise<{
35
+ data: any;
36
+ errors?: any[];
37
+ }>;
12
38
  /**
13
39
  * Configuration options during runtime.
14
40
  */
@@ -104,6 +130,42 @@ type GraphqlMiddlewareServerOptions = {
104
130
  * ```
105
131
  */
106
132
  onServerError?: GraphqlMiddlewareOnServerErrorMethod;
133
+ /**
134
+ * Provide a custom fetch method for requests to the GraphQL server.
135
+ *
136
+ * This can be used if onServerError, onServerResponse, serverFetchOptions
137
+ * and graphqlEndpoint are not enough to meet your requirements.
138
+ *
139
+ * The method will be called in the /api/graphql server route and should
140
+ * perform the GraphQL request and return the response.
141
+ *
142
+ * An example use case might be to handle expired tokens.
143
+ *
144
+ * * ```ts
145
+ * async function doGraphqlRequest({
146
+ * event,
147
+ * operation,
148
+ * operationName,
149
+ * operationDocument,
150
+ * variables,
151
+ * }) {
152
+ * const result = await $fetch.raw('https://example.com/graphql', {
153
+ * method: 'POST'
154
+ * body: {
155
+ * query: operationDocument,
156
+ * variables,
157
+ * operationName
158
+ * },
159
+ * headers: {
160
+ * 'custom-header': 'foobar'
161
+ * }
162
+ * })
163
+ *
164
+ * return result._data
165
+ * }
166
+ * ```
167
+ */
168
+ doGraphqlRequest?: GraphqlMiddlewareDoRequestMethod;
107
169
  };
108
170
 
109
171
  interface ModuleOptions {
package/dist/module.d.ts CHANGED
@@ -9,6 +9,32 @@ type GraphqlMiddlewareGraphqlEndpointMethod = (event?: H3Event, operation?: stri
9
9
  type GraphqlMiddlewareServerFetchOptionsMethod = (event?: H3Event, operation?: string, operationName?: string) => FetchOptions | Promise<FetchOptions>;
10
10
  type GraphqlMiddlewareOnServerResponseMethod = (event: H3Event, response: FetchResponse<any>, operation?: string, operationName?: string) => any | Promise<any>;
11
11
  type GraphqlMiddlewareOnServerErrorMethod = (event: H3Event, error: FetchError, operation?: string, operationName?: string) => any | Promise<any>;
12
+ type GraphqlMiddlewareDoRequestMethodContext = {
13
+ /**
14
+ * The incoming request event from H3.
15
+ */
16
+ event: H3Event;
17
+ /**
18
+ * The type of operation.
19
+ */
20
+ operation: 'query' | 'mutation';
21
+ /**
22
+ * The name of the operation.
23
+ */
24
+ operationName: string;
25
+ /**
26
+ * The operation document (the raw GraphQL query/mutation as a string).
27
+ */
28
+ operationDocument: string;
29
+ /**
30
+ * Variables for the operation.
31
+ */
32
+ variables: Record<string, any>;
33
+ };
34
+ type GraphqlMiddlewareDoRequestMethod = (context: GraphqlMiddlewareDoRequestMethodContext) => Promise<{
35
+ data: any;
36
+ errors?: any[];
37
+ }>;
12
38
  /**
13
39
  * Configuration options during runtime.
14
40
  */
@@ -104,6 +130,42 @@ type GraphqlMiddlewareServerOptions = {
104
130
  * ```
105
131
  */
106
132
  onServerError?: GraphqlMiddlewareOnServerErrorMethod;
133
+ /**
134
+ * Provide a custom fetch method for requests to the GraphQL server.
135
+ *
136
+ * This can be used if onServerError, onServerResponse, serverFetchOptions
137
+ * and graphqlEndpoint are not enough to meet your requirements.
138
+ *
139
+ * The method will be called in the /api/graphql server route and should
140
+ * perform the GraphQL request and return the response.
141
+ *
142
+ * An example use case might be to handle expired tokens.
143
+ *
144
+ * * ```ts
145
+ * async function doGraphqlRequest({
146
+ * event,
147
+ * operation,
148
+ * operationName,
149
+ * operationDocument,
150
+ * variables,
151
+ * }) {
152
+ * const result = await $fetch.raw('https://example.com/graphql', {
153
+ * method: 'POST'
154
+ * body: {
155
+ * query: operationDocument,
156
+ * variables,
157
+ * operationName
158
+ * },
159
+ * headers: {
160
+ * 'custom-header': 'foobar'
161
+ * }
162
+ * })
163
+ *
164
+ * return result._data
165
+ * }
166
+ * ```
167
+ */
168
+ doGraphqlRequest?: GraphqlMiddlewareDoRequestMethod;
107
169
  };
108
170
 
109
171
  interface ModuleOptions {
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
3
  "configKey": "graphqlMiddleware",
4
- "version": "4.0.0-beta.7",
4
+ "version": "4.0.0-beta.9",
5
5
  "compatibility": {
6
6
  "nuxt": "^3.1.0"
7
7
  }
package/dist/module.mjs CHANGED
@@ -20,7 +20,7 @@ import * as PluginSchemaAst from '@graphql-codegen/schema-ast';
20
20
  import { pascalCase } from 'change-case-all';
21
21
 
22
22
  const name = "nuxt-graphql-middleware";
23
- const version = "4.0.0-beta.7";
23
+ const version = "4.0.0-beta.9";
24
24
 
25
25
  const DEVTOOLS_UI_ROUTE = "/__nuxt-graphql-middleware";
26
26
  const DEVTOOLS_UI_LOCAL_PORT = 3300;
@@ -303,7 +303,7 @@ function validateDeprecated(options) {
303
303
  );
304
304
  if (key === "graphqlEndpoint") {
305
305
  logger.info(`
306
- import { defineGraphqlServerOptions } from '#graphql-server-options'
306
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/dist/runtime/serverOptions'
307
307
  import { getHeader } from 'h3'
308
308
  import acceptLanguageParser from 'accept-language-parser';
309
309
 
@@ -321,7 +321,7 @@ export default defineGraphqlServerOptions({
321
321
  }
322
322
  if (key === "serverFetchOptions") {
323
323
  logger.info(`
324
- import { defineGraphqlServerOptions } from '#graphql-server-options'
324
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/dist/runtime/serverOptions'
325
325
  import { getHeader } from 'h3'
326
326
 
327
327
  // Pass the cookie from the client request to the GraphQL request.
@@ -337,7 +337,7 @@ export default defineGraphqlServerOptions({
337
337
  }
338
338
  if (key === "onServerResponse") {
339
339
  logger.info(`
340
- import { defineGraphqlServerOptions } from '#graphql-server-options'
340
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/dist/runtime/serverOptions'
341
341
  import type { H3Event } from 'h3'
342
342
  import type { FetchResponse } from 'ofetch'
343
343
 
@@ -362,7 +362,7 @@ export default defineGraphqlServerOptions({
362
362
  }
363
363
  if (key === "onServerError") {
364
364
  logger.info(`
365
- import { defineGraphqlServerOptions } from '#graphql-server-options'
365
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/dist/runtime/serverOptions'
366
366
  import type { H3Event } from 'h3'
367
367
  import type { FetchError } from 'ofetch'
368
368
 
@@ -557,7 +557,8 @@ async function generate(options, schemaPath, resolver, srcDir, logEverything = f
557
557
  const extracted = validated.filter(
558
558
  (v) => !v.operation
559
559
  );
560
- validated.forEach((v) => {
560
+ for (let i = 0; i < validated.length; i++) {
561
+ const v = validated[i];
561
562
  if (v.isValid) {
562
563
  try {
563
564
  const node = parse(v.content);
@@ -589,15 +590,20 @@ async function generate(options, schemaPath, resolver, srcDir, logEverything = f
589
590
  });
590
591
  } catch (e) {
591
592
  logger.error(e);
593
+ extracted.push(v);
594
+ break;
592
595
  }
596
+ } else {
597
+ extracted.push(v);
598
+ break;
593
599
  }
594
- });
600
+ }
595
601
  const templates = await generateTemplates(
596
602
  extracted.filter((v) => v.isValid).map((v) => v.content),
597
603
  schemaPath,
598
604
  options
599
605
  );
600
- const hasErrors = extracted.some((v) => !v.isValid);
606
+ const hasErrors = extracted.some((v) => !v.isValid) || validated.some((v) => !v.isValid);
601
607
  if (hasErrors || logEverything) {
602
608
  const table = new Table({
603
609
  head: ["Operation", "Name", "File", "Errors"].map((v) => chalk.white(v))
@@ -831,9 +837,6 @@ declare module '#graphql-documents' {
831
837
  `;
832
838
  }
833
839
  });
834
- nuxt.options.alias["#graphql-server-options"] = moduleResolver(
835
- "runtime/serverOptions"
836
- );
837
840
  const extensions = ["js", "mjs", "ts"];
838
841
  const resolvedPath = "~/app/graphqlMiddleware.serverOptions".replace(/^(~~|@@)/, nuxt.options.rootDir).replace(/^(~|@)/, nuxt.options.srcDir);
839
842
  const template = (() => {
@@ -1,4 +1,4 @@
1
- import { defineEventHandler, getQuery, getMethod, readBody } from "h3";
1
+ import { defineEventHandler, getQuery, readBody } from "h3";
2
2
  import {
3
3
  queryParamToVariables,
4
4
  getEndpoint,
@@ -12,38 +12,59 @@ import { documents } from "#graphql-documents";
12
12
  import serverOptions from "#graphql-middleware-server-options-build";
13
13
  import { useRuntimeConfig } from "#imports";
14
14
  export default defineEventHandler(async (event) => {
15
- const method = getMethod(event);
15
+ const method = event.method;
16
16
  const operation = event.context?.params?.operation;
17
- const name = event.context?.params?.name;
18
- validateRequest(method, operation, name, documents);
19
- const query = documents[operation][name];
17
+ const operationName = event.context?.params?.name;
18
+ validateRequest(method, operation, operationName, documents);
19
+ const operationDocument = documents[operation][operationName];
20
+ const variables = operation === GraphqlMiddlewareOperation.Query ? queryParamToVariables(getQuery(event)) : await readBody(event);
21
+ if (serverOptions.doGraphqlRequest) {
22
+ return serverOptions.doGraphqlRequest({
23
+ event,
24
+ operation,
25
+ operationName,
26
+ operationDocument,
27
+ variables
28
+ });
29
+ }
20
30
  const runtimeConfig = useRuntimeConfig().graphqlMiddleware;
21
31
  const endpoint = await getEndpoint(
22
32
  runtimeConfig,
23
33
  serverOptions,
24
34
  event,
25
35
  operation,
26
- name
36
+ operationName
27
37
  );
28
38
  const fetchOptions = await getFetchOptions(
29
39
  serverOptions,
30
40
  event,
31
41
  operation,
32
- name
42
+ operationName
33
43
  );
34
- const variables = operation === GraphqlMiddlewareOperation.Query ? queryParamToVariables(getQuery(event)) : await readBody(event);
35
44
  return $fetch.raw(endpoint, {
36
45
  // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
37
46
  method: "POST",
38
47
  body: {
39
- query,
48
+ query: operationDocument,
40
49
  variables,
41
- operationName: name
50
+ operationName
42
51
  },
43
52
  ...fetchOptions
44
53
  }).then((response) => {
45
- return onServerResponse(serverOptions, event, response, operation, name);
54
+ return onServerResponse(
55
+ serverOptions,
56
+ event,
57
+ response,
58
+ operation,
59
+ operationName
60
+ );
46
61
  }).catch((error) => {
47
- return onServerError(serverOptions, event, error, operation, name);
62
+ return onServerError(
63
+ serverOptions,
64
+ event,
65
+ error,
66
+ operation,
67
+ operationName
68
+ );
48
69
  });
49
70
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
- "version": "4.0.0-beta.7",
3
+ "version": "4.0.0-beta.9",
4
4
  "description": "Module to perform GraphQL requests as a server middleware.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,7 +12,8 @@
12
12
  ".": {
13
13
  "import": "./dist/module.mjs",
14
14
  "require": "./dist/module.cjs"
15
- }
15
+ },
16
+ "./dist/runtime/serverOptions": "./dist/runtime/serverOptions/index.mjs"
16
17
  },
17
18
  "main": "./dist/module.cjs",
18
19
  "types": "./dist/types.d.ts",
@@ -59,12 +60,10 @@
59
60
  "@nuxt/devtools-ui-kit": "0.8.5",
60
61
  "@nuxt/module-builder": "^0.5.2",
61
62
  "@nuxt/schema": "^3.7.4",
62
- "@nuxt/test-utils": "^3.7.4",
63
63
  "@nuxtjs/eslint-config-typescript": "^12.1.0",
64
64
  "@types/capture-console": "^1.0.2",
65
65
  "@types/cli-table": "^0.3.2",
66
66
  "@types/inquirer": "^9.0.3",
67
- "@vitest/coverage-c8": "^0.30.1",
68
67
  "cypress": "^13.3.0",
69
68
  "eslint": "^8.50.0",
70
69
  "eslint-config-prettier": "^9.0.0",