nuxt-graphql-middleware 3.1.0-beta.1 → 3.1.0-beta.3

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.ts CHANGED
@@ -1,13 +1,112 @@
1
- import { NuxtModule } from '@nuxt/schema';
1
+ import * as _nuxt_schema from '@nuxt/schema';
2
+ import { Types } from '@graphql-codegen/plugin-helpers';
3
+ import { SchemaASTConfig } from '@graphql-codegen/schema-ast';
4
+ import { TypeScriptDocumentsPluginConfig } from '@graphql-codegen/typescript-operations';
2
5
  import { H3Event } from 'h3';
3
6
  import { FetchOptions, FetchResponse, FetchError } from 'ofetch';
4
- import { TypeScriptDocumentsPluginConfig } from '@graphql-codegen/typescript-operations';
5
7
 
6
8
  type GraphqlMiddlewareGraphqlEndpointMethod = (event?: H3Event, operation?: string, operationName?: string) => string | Promise<string> | void;
7
9
  type GraphqlMiddlewareServerFetchOptionsMethod = (event?: H3Event, operation?: string, operationName?: string) => FetchOptions | Promise<FetchOptions>;
8
10
  type GraphqlMiddlewareOnServerResponseMethod = (event: H3Event, response: FetchResponse<any>, operation?: string, operationName?: string) => any | Promise<any>;
9
11
  type GraphqlMiddlewareOnServerErrorMethod = (event: H3Event, error: FetchError, operation?: string, operationName?: string) => any | Promise<any>;
10
- interface GraphqlMiddlewareConfig {
12
+ /**
13
+ * Configuration options during runtime.
14
+ */
15
+ type GraphqlMiddlewareServerOptions = {
16
+ /**
17
+ * Custom callback to return the GraphQL endpoint per request.
18
+ *
19
+ * @default undefined
20
+ *
21
+ * @example
22
+ * ```ts
23
+ * function graphqlEndpoint(event, operation, operationName) {
24
+ * const language = getLanguageFromRequest(event)
25
+ * return `https://api.example.com/${language}/graphql`
26
+ * }
27
+ * ```
28
+ */
29
+ graphqlEndpoint?: GraphqlMiddlewareGraphqlEndpointMethod;
30
+ /**
31
+ * Provide the options for the ofetch request to the GraphQL server.
32
+ *
33
+ * @default undefined
34
+ *
35
+ * @example
36
+ * ```ts
37
+ * import { getHeader } from 'h3'
38
+ *
39
+ * // Pass the cookie from the client request to the GraphQL request.
40
+ * function serverFetchOptions(event, operation, operationName) {
41
+ * return {
42
+ * headers: {
43
+ * Cookie: getHeader(event, 'cookie')
44
+ * }
45
+ * }
46
+ * }
47
+ * ```
48
+ */
49
+ serverFetchOptions?: GraphqlMiddlewareServerFetchOptionsMethod;
50
+ /**
51
+ * Handle the response from the GraphQL server.
52
+ *
53
+ * You can alter the response, add additional properties to the data, get
54
+ * and set headers, etc.
55
+ *
56
+ * ```ts
57
+ * import type { H3Event } from 'h3'
58
+ * import type { FetchResponse } from 'ofetch'
59
+ *
60
+ * function onServerResponse(event: H3Event, graphqlResponse: FetchResponse) {
61
+ * // Set a static header.
62
+ * event.node.res.setHeader('x-nuxt-custom-header', 'A custom header value')
63
+ *
64
+ * // Pass the set-cookie header from the GraphQL response to the client.
65
+ * const setCookie = graphqlResponse.headers.get('set-cookie')
66
+ * if (setCookie) {
67
+ * event.node.res.setHeader('set-cookie', setCookie)
68
+ * }
69
+ *
70
+ * // Add additional properties to the response.
71
+ * graphqlResponse._data.__customProperty = ['My', 'values']
72
+ *
73
+ * // Return the GraphQL response.
74
+ * return graphqlResponse._data
75
+ * }
76
+ * ```
77
+ */
78
+ onServerResponse?: GraphqlMiddlewareOnServerResponseMethod;
79
+ /**
80
+ * Handle a fetch error from the GraphQL request.
81
+ *
82
+ * Note that errors are only thrown for responses that are not status
83
+ * 200-299. See https://github.com/unjs/ofetch#%EF%B8%8F-handling-errors for
84
+ * more information.
85
+ *
86
+ * ```ts
87
+ * import { createError } from 'h3'
88
+ * import type { H3Event } from 'h3'
89
+ * import type { FetchError } from 'ofetch'
90
+ *
91
+ * function onServerError(
92
+ * event: H3Event,
93
+ * error: FetchError,
94
+ * operation: string,
95
+ * operationName: string,
96
+ * ) {
97
+ * // Throw a h3 error.
98
+ * throw createError({
99
+ * statusCode: 500,
100
+ * statusMessage: `Couldn't execute GraphQL ${operation} "${operationName}".`,
101
+ * data: error.message
102
+ * })
103
+ * }
104
+ * ```
105
+ */
106
+ onServerError?: GraphqlMiddlewareOnServerErrorMethod;
107
+ };
108
+
109
+ interface ModuleOptions {
11
110
  /**
12
111
  * File glob patterns for the auto import feature.
13
112
  *
@@ -96,7 +195,8 @@ interface GraphqlMiddlewareConfig {
96
195
  */
97
196
  schemaPath?: string;
98
197
  /**
99
- * These options are passed to the graphql-codegen method when generating the operations types.
198
+ * These options are passed to the graphql-codegen method when generating the
199
+ * TypeScript operations types.
100
200
  *
101
201
  * {@link https://www.the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations}
102
202
  * @default
@@ -115,106 +215,33 @@ interface GraphqlMiddlewareConfig {
115
215
  * ```
116
216
  */
117
217
  codegenConfig?: TypeScriptDocumentsPluginConfig;
118
- }
119
- /**
120
- * Configuration options during runtime.
121
- */
122
- type GraphqlMiddlewareServerOptions = {
123
- /**
124
- * Custom callback to return the GraphQL endpoint per request.
125
- *
126
- * @default undefined
127
- *
128
- * @example
129
- * ```ts
130
- * function graphqlEndpoint(event, operation, operationName) {
131
- * const language = getLanguageFromRequest(event)
132
- * return `https://api.example.com/${language}/graphql`
133
- * }
134
- * ```
135
- */
136
- graphqlEndpoint?: GraphqlMiddlewareGraphqlEndpointMethod;
137
- /**
138
- * Provide the options for the ofetch request to the GraphQL server.
139
- *
140
- * @default undefined
141
- *
142
- * @example
143
- * ```ts
144
- * import { getHeader } from 'h3'
145
- *
146
- * // Pass the cookie from the client request to the GraphQL request.
147
- * function serverFetchOptions(event, operation, operationName) {
148
- * return {
149
- * headers: {
150
- * Cookie: getHeader(event, 'cookie')
151
- * }
152
- * }
153
- * }
154
- * ```
155
- */
156
- serverFetchOptions?: GraphqlMiddlewareServerFetchOptionsMethod;
157
- /**
158
- * Handle the response from the GraphQL server.
159
- *
160
- * You can alter the response, add additional properties to the data, get
161
- * and set headers, etc.
162
- *
163
- * ```ts
164
- * import type { H3Event } from 'h3'
165
- * import type { FetchResponse } from 'ofetch'
166
- *
167
- * function onServerResponse(event: H3Event, graphqlResponse: FetchResponse) {
168
- * // Set a static header.
169
- * event.node.res.setHeader('x-nuxt-custom-header', 'A custom header value')
170
- *
171
- * // Pass the set-cookie header from the GraphQL response to the client.
172
- * const setCookie = graphqlResponse.headers.get('set-cookie')
173
- * if (setCookie) {
174
- * event.node.res.setHeader('set-cookie', setCookie)
175
- * }
176
- *
177
- * // Add additional properties to the response.
178
- * graphqlResponse._data.__customProperty = ['My', 'values']
179
- *
180
- * // Return the GraphQL response.
181
- * return graphqlResponse._data
182
- * }
183
- * ```
184
- */
185
- onServerResponse?: GraphqlMiddlewareOnServerResponseMethod;
186
218
  /**
187
- * Handle a fetch error from the GraphQL request.
188
- *
189
- * Note that errors are only thrown for responses that are not status
190
- * 200-299. See https://github.com/unjs/ofetch#%EF%B8%8F-handling-errors for
191
- * more information.
192
- *
193
- * ```ts
194
- * import { createError } from 'h3'
195
- * import type { H3Event } from 'h3'
196
- * import type { FetchError } from 'ofetch'
197
- *
198
- * function onServerError(
199
- * event: H3Event,
200
- * error: FetchError,
201
- * operation: string,
202
- * operationName: string,
203
- * ) {
204
- * // Throw a h3 error.
205
- * throw createError({
206
- * statusCode: 500,
207
- * statusMessage: `Couldn't execute GraphQL ${operation} "${operationName}".`,
208
- * data: error.message
209
- * })
210
- * }
211
- * ```
219
+ * Configuration for graphql-codegen when downloading the schema.
212
220
  */
213
- onServerError?: GraphqlMiddlewareOnServerErrorMethod;
214
- };
215
-
216
- type ModuleOptions = GraphqlMiddlewareConfig;
221
+ codegenSchemaConfig?: {
222
+ /**
223
+ * Configure how the schema.graphql file should be generated.
224
+ */
225
+ schemaAstConfig?: SchemaASTConfig;
226
+ /**
227
+ * Configure how the schema-ast introspection request should be made.
228
+ *
229
+ * Usually this is where you can provide a custom authentication header:
230
+ *
231
+ * ```typescript
232
+ * const codegenSchemaConfig = {
233
+ * urlSchemaOptions: {
234
+ * headers: {
235
+ * authentication: 'foobar',
236
+ * }
237
+ * }
238
+ * }
239
+ * ```
240
+ */
241
+ urlSchemaOptions?: Types.UrlSchemaOptions;
242
+ };
243
+ }
217
244
  type ModuleHooks = {};
218
- declare const _default: NuxtModule<GraphqlMiddlewareConfig>;
245
+ declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
219
246
 
220
247
  export { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, _default as default };
package/dist/module.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
3
  "configKey": "graphqlMiddleware",
4
- "version": "3.1.0-beta.1",
4
+ "version": "3.1.0-beta.3",
5
5
  "compatibility": {
6
- "nuxt": "^3.0.0"
6
+ "nuxt": "^3.1.0"
7
7
  }
8
8
  }
package/dist/module.mjs CHANGED
@@ -17,7 +17,7 @@ import { oldVisit } from '@graphql-codegen/plugin-helpers';
17
17
  import { pascalCase } from 'change-case-all';
18
18
 
19
19
  const name = "nuxt-graphql-middleware";
20
- const version = "3.1.0-beta.1";
20
+ const version = "3.1.0-beta.3";
21
21
 
22
22
  var GraphqlMiddlewareTemplate = /* @__PURE__ */ ((GraphqlMiddlewareTemplate2) => {
23
23
  GraphqlMiddlewareTemplate2["OperationTypes"] = "graphql-operations.d.ts";
@@ -165,24 +165,25 @@ function pluginLoader(name) {
165
165
  return Promise.resolve(PluginSchemaAst);
166
166
  }
167
167
  }
168
- function generateSchema(url, dest, writeToDisk) {
169
- return generate$1(
170
- {
171
- schema: url,
172
- pluginLoader,
173
- silent: true,
174
- errorsOnly: true,
175
- generates: {
176
- [dest]: {
177
- plugins: ["schema-ast"],
178
- config: {
179
- sort: true
180
- }
181
- }
168
+ function generateSchema(moduleOptions, dest, writeToDisk) {
169
+ const pluginConfig = moduleOptions.codegenSchemaConfig?.urlSchemaOptions;
170
+ const schemaAstConfig = moduleOptions.codegenSchemaConfig?.schemaAstConfig || {
171
+ sort: true
172
+ };
173
+ const config = {
174
+ schema: moduleOptions.graphqlEndpoint,
175
+ pluginLoader,
176
+ silent: true,
177
+ errorsOnly: true,
178
+ config: pluginConfig,
179
+ generates: {
180
+ [dest]: {
181
+ plugins: ["schema-ast"],
182
+ config: schemaAstConfig
182
183
  }
183
- },
184
- writeToDisk
185
- ).then((v) => v[0]);
184
+ }
185
+ };
186
+ return generate$1(config, writeToDisk).then((v) => v[0]);
186
187
  }
187
188
  function generateTemplates(documents, schemaPath, options) {
188
189
  return executeCodegen({
@@ -255,7 +256,7 @@ function validateDeprecated(options) {
255
256
  );
256
257
  if (key === "graphqlEndpoint") {
257
258
  logger.info(`
258
- import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/server'
259
+ import { defineGraphqlServerOptions } from '#graphql-server-options'
259
260
  import { getHeader } from 'h3'
260
261
  import acceptLanguageParser from 'accept-language-parser';
261
262
 
@@ -273,7 +274,7 @@ export default defineGraphqlServerOptions({
273
274
  }
274
275
  if (key === "serverFetchOptions") {
275
276
  logger.info(`
276
- import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/server'
277
+ import { defineGraphqlServerOptions } from '#graphql-server-options'
277
278
  import { getHeader } from 'h3'
278
279
 
279
280
  // Pass the cookie from the client request to the GraphQL request.
@@ -289,7 +290,7 @@ export default defineGraphqlServerOptions({
289
290
  }
290
291
  if (key === "onServerResponse") {
291
292
  logger.info(`
292
- import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/server'
293
+ import { defineGraphqlServerOptions } from '#graphql-server-options'
293
294
  import type { H3Event } from 'h3'
294
295
  import type { FetchResponse } from 'ofetch'
295
296
 
@@ -314,7 +315,7 @@ export default defineGraphqlServerOptions({
314
315
  }
315
316
  if (key === "onServerError") {
316
317
  logger.info(`
317
- import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/server'
318
+ import { defineGraphqlServerOptions } from '#graphql-server-options'
318
319
  import type { H3Event } from 'h3'
319
320
  import type { FetchError } from 'ofetch'
320
321
 
@@ -353,7 +354,7 @@ async function getSchemaPath(options, resolver, writeToDisk = false) {
353
354
  if (!options.graphqlEndpoint) {
354
355
  throw new Error("Missing graphqlEndpoint config.");
355
356
  }
356
- await generateSchema(options.graphqlEndpoint, dest, writeToDisk);
357
+ await generateSchema(options, dest, writeToDisk);
357
358
  return dest;
358
359
  }
359
360
  async function autoImportDocuments(patterns = [], srcResolver) {
@@ -494,7 +495,7 @@ const module = defineNuxtModule({
494
495
  configKey: "graphqlMiddleware",
495
496
  version,
496
497
  compatibility: {
497
- nuxt: "^3.0.0"
498
+ nuxt: "^3.1.0"
498
499
  }
499
500
  },
500
501
  defaults: defaultOptions,
@@ -609,6 +610,9 @@ declare module '#graphql-documents' {
609
610
  `;
610
611
  }
611
612
  });
613
+ nuxt.options.alias["#graphql-server-options"] = moduleResolver(
614
+ "runtime/serverOptions"
615
+ );
612
616
  const extensions = ["js", "mjs", "ts"];
613
617
  const resolvedPath = "~/app/graphqlMiddleware.serverOptions".replace(/^(~~|@@)/, nuxt.options.rootDir).replace(/^(~|@)/, nuxt.options.srcDir);
614
618
  const template = (() => {
@@ -630,7 +634,7 @@ declare module '#graphql-documents' {
630
634
  nuxt.options.nitro.externals = nuxt.options.nitro.externals || {};
631
635
  nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || [];
632
636
  nuxt.options.nitro.externals.inline.push(template.dst);
633
- nuxt.options.alias["#graphql-middleware-server-options"] = template.dst;
637
+ nuxt.options.alias["#graphql-middleware-server-options-build"] = template.dst;
634
638
  addServerHandler({
635
639
  handler: moduleResolver("./runtime/serverHandler/index"),
636
640
  route: options.serverApiPrefix + "/:operation/:name"
@@ -18,6 +18,7 @@ export function useGraphqlQuery(...args) {
18
18
  const state2 = useGraphqlState();
19
19
  return $fetch(getEndpoint("query", name), {
20
20
  params: buildRequestParams(args[1]),
21
+ // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
21
22
  ...state2.fetchOptions
22
23
  });
23
24
  }
@@ -29,6 +30,7 @@ export function useGraphqlMutation(...args) {
29
30
  return Promise.reject(new Error("Invalid mutation name"));
30
31
  }
31
32
  return $fetch(getEndpoint("mutation", name), {
33
+ // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
32
34
  method: "post",
33
35
  body,
34
36
  ...state2.fetchOptions
@@ -10,7 +10,7 @@ import {
10
10
  import { GraphqlMiddlewareOperation } from "./../settings/index.mjs";
11
11
  import { documents } from "#graphql-documents";
12
12
  import { useRuntimeConfig } from "#imports";
13
- import serverOptions from "#graphql-middleware-server-options";
13
+ import serverOptions from "#graphql-middleware-server-options-build";
14
14
  export default defineEventHandler(async (event) => {
15
15
  const method = getMethod(event);
16
16
  const operation = event.context.params.operation;
@@ -33,6 +33,7 @@ export default defineEventHandler(async (event) => {
33
33
  );
34
34
  const variables = operation === GraphqlMiddlewareOperation.Query ? queryParamToVariables(getQuery(event)) : await readBody(event);
35
35
  return $fetch.raw(endpoint, {
36
+ // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
36
37
  method: "POST",
37
38
  body: {
38
39
  query,
@@ -0,0 +1,2 @@
1
+ import { GraphqlMiddlewareServerOptions } from './../../types';
2
+ export declare function defineGraphqlServerOptions(options: GraphqlMiddlewareServerOptions): GraphqlMiddlewareServerOptions;
@@ -0,0 +1,3 @@
1
+ export function defineGraphqlServerOptions(options) {
2
+ return options;
3
+ }
@@ -1,2 +1,2 @@
1
- import type { GraphqlMiddlewareServerOptions } from './../../types';
2
- export declare function defineGraphqlServerOptions(options: GraphqlMiddlewareServerOptions): GraphqlMiddlewareServerOptions;
1
+ import { defineGraphqlServerOptions } from './defineGraphqlServerOptions';
2
+ export { defineGraphqlServerOptions };
@@ -1,3 +1,2 @@
1
- export function defineGraphqlServerOptions(options) {
2
- return options;
3
- }
1
+ import { defineGraphqlServerOptions } from "./defineGraphqlServerOptions.mjs";
2
+ export { defineGraphqlServerOptions };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
- "version": "3.1.0-beta.1",
3
+ "version": "3.1.0-beta.3",
4
4
  "description": "Module to perform GraphQL requests as a server middleware.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,9 +12,7 @@
12
12
  ".": {
13
13
  "import": "./dist/module.mjs",
14
14
  "require": "./dist/module.cjs"
15
- },
16
- "./server": "./dist/runtime/serverOptions/index.mjs",
17
- "./server/*": "./dist/runtime/serverOptions/*.mjs"
15
+ }
18
16
  },
19
17
  "main": "./dist/module.cjs",
20
18
  "types": "./dist/types.d.ts",
@@ -37,11 +35,11 @@
37
35
  "test:coverage": "vitest run --coverage"
38
36
  },
39
37
  "dependencies": {
40
- "@graphql-codegen/cli": "^2.15.0",
41
- "@graphql-codegen/schema-ast": "^2.5.1",
42
- "@graphql-codegen/typescript": "^2.8.3",
38
+ "@graphql-codegen/cli": "^2.16.4",
39
+ "@graphql-codegen/schema-ast": "^2.6.1",
40
+ "@graphql-codegen/typescript": "^2.8.7",
43
41
  "@graphql-codegen/typescript-generic-sdk": "^3.0.4",
44
- "@graphql-codegen/typescript-operations": "^2.5.8",
42
+ "@graphql-codegen/typescript-operations": "^2.5.12",
45
43
  "@graphql-fragment-import/lib": "^2.0.0",
46
44
  "@graphql-tools/utils": "^9.1.1",
47
45
  "@nuxt/kit": "^3.0.0",
@@ -51,21 +49,21 @@
51
49
  "devDependencies": {
52
50
  "@nuxt/module-builder": "^0.2.1",
53
51
  "@nuxt/schema": "^3.0.0",
54
- "@nuxt/test-utils": "^3.0.0",
52
+ "@nuxt/test-utils": "^3.1.1",
55
53
  "@nuxtjs/eslint-config-typescript": "^12.0.0",
56
54
  "@types/capture-console": "^1.0.1",
57
55
  "@types/cli-table": "^0.3.1",
58
56
  "@types/inquirer": "^9.0.3",
59
- "@vitest/coverage-c8": "^0.25.3",
57
+ "@vitest/coverage-c8": "^0.25.8",
60
58
  "cypress": "^11.2.0",
61
- "eslint": "^8.29.0",
62
- "eslint-config-prettier": "^8.5.0",
59
+ "eslint": "^8.33.0",
60
+ "eslint-config-prettier": "^8.6.0",
63
61
  "eslint-plugin-prettier": "^4.2.1",
64
62
  "jsdoc-to-markdown": "^8.0.0",
65
- "nuxt": "^3.0.0",
66
- "prettier": "^2.8.0",
63
+ "nuxt": "^3.1.1",
64
+ "prettier": "^2.8.3",
67
65
  "strip-ansi": "^7.0.1",
68
- "typedoc": "^0.23.21",
66
+ "typedoc": "^0.23.24",
69
67
  "typedoc-plugin-markdown": "^3.14.0",
70
68
  "vitepress": "^1.0.0-alpha.30",
71
69
  "vitest": "^0.25.3"