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

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
@@ -3,10 +3,10 @@ import { H3Event } from 'h3';
3
3
  import { FetchOptions, FetchResponse, FetchError } from 'ofetch';
4
4
  import { TypeScriptDocumentsPluginConfig } from '@graphql-codegen/typescript-operations';
5
5
 
6
- type GraphqlMiddlewareGraphqlEndpointMethod = (event?: H3Event, operation?: string, operationName?: string) => string;
7
- type GraphqlMiddlewareServerFetchOptionsMethod = (event?: H3Event, operation?: string, operationName?: string) => FetchOptions;
8
- type GraphqlMiddlewareOnServerResponseMethod = (event: H3Event, response: FetchResponse<any>, operation?: string, operationName?: string) => any;
9
- type GraphqlMiddlewareOnServerErrorMethod = (event: H3Event, error: FetchError, operation?: string, operationName?: string) => any;
6
+ type GraphqlMiddlewareGraphqlEndpointMethod = (event?: H3Event, operation?: string, operationName?: string) => string | Promise<string> | void;
7
+ type GraphqlMiddlewareServerFetchOptionsMethod = (event?: H3Event, operation?: string, operationName?: string) => FetchOptions | Promise<FetchOptions>;
8
+ type GraphqlMiddlewareOnServerResponseMethod = (event: H3Event, response: FetchResponse<any>, operation?: string, operationName?: string) => any | Promise<any>;
9
+ type GraphqlMiddlewareOnServerErrorMethod = (event: H3Event, error: FetchError, operation?: string, operationName?: string) => any | Promise<any>;
10
10
  interface GraphqlMiddlewareConfig {
11
11
  /**
12
12
  * File glob patterns for the auto import feature.
@@ -67,19 +67,17 @@ interface GraphqlMiddlewareConfig {
67
67
  /**
68
68
  * The URL of the GraphQL server.
69
69
  *
70
- * You can either provide a string or a method that returns a string.
71
- * If you provide a method it will be called everytime a GraphQL request is
72
- * made in the server API handler.
70
+ * For the runtime execution you can provide a method that determines the endpoint
71
+ * during runtime. See the app/graphqlMiddleware.serverOptions.ts documentation
72
+ * for more information.
73
+ */
74
+ graphqlEndpoint: string;
75
+ /**
76
+ * Download the GraphQL schema and store it on disk.
73
77
  *
74
- * @example
75
- * ```ts
76
- * function graphqlEndpoint(event, operation, operationName) {
77
- * const language = getLanguageFromRequest(event)
78
- * return `https://api.example.com/${language}/graphql`
79
- * }
80
- * ```
78
+ * @default true
81
79
  */
82
- graphqlEndpoint?: string | GraphqlMiddlewareGraphqlEndpointMethod;
80
+ downloadSchema?: boolean;
83
81
  /**
84
82
  * The prefix for the server route.
85
83
  *
@@ -88,6 +86,54 @@ interface GraphqlMiddlewareConfig {
88
86
  * ```
89
87
  */
90
88
  serverApiPrefix?: string;
89
+ /**
90
+ * Path to the GraphQL schema file.
91
+ *
92
+ * If `downloadSchema` is `true`, the downloaded schema is written to this specified path.
93
+ * If `downloadSchema` is `false`, this file must be present in order to generate types.
94
+ *
95
+ * @default './schema.graphql'
96
+ */
97
+ schemaPath?: string;
98
+ /**
99
+ * These options are passed to the graphql-codegen method when generating the operations types.
100
+ *
101
+ * {@link https://www.the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations}
102
+ * @default
103
+ * ```ts
104
+ * const codegenConfig = {
105
+ * exportFragmentSpreadSubTypes: true,
106
+ * preResolveTypes: true,
107
+ * skipTypeNameForRoot: true,
108
+ * skipTypename: true,
109
+ * useTypeImports: true,
110
+ * onlyOperationTypes: true,
111
+ * namingConvention: {
112
+ * enumValues: 'change-case-all#upperCaseFirst',
113
+ * },
114
+ * }
115
+ * ```
116
+ */
117
+ 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;
91
137
  /**
92
138
  * Provide the options for the ofetch request to the GraphQL server.
93
139
  *
@@ -107,7 +153,7 @@ interface GraphqlMiddlewareConfig {
107
153
  * }
108
154
  * ```
109
155
  */
110
- serverFetchOptions?: FetchOptions | GraphqlMiddlewareServerFetchOptionsMethod;
156
+ serverFetchOptions?: GraphqlMiddlewareServerFetchOptionsMethod;
111
157
  /**
112
158
  * Handle the response from the GraphQL server.
113
159
  *
@@ -165,45 +211,10 @@ interface GraphqlMiddlewareConfig {
165
211
  * ```
166
212
  */
167
213
  onServerError?: GraphqlMiddlewareOnServerErrorMethod;
168
- /**
169
- * Download the GraphQL schema and store it in the
170
- *
171
- * @default true
172
- */
173
- downloadSchema?: boolean;
174
- /**
175
- * Path to the GraphQL schema file.
176
- *
177
- * If `downloadSchema` is `true`, the downloaded schema is written to this specified path.
178
- * If `downloadSchema` is `false`, this file must be present in order to generate types.
179
- *
180
- * @default './schema.graphql'
181
- */
182
- schemaPath?: string;
183
- /**
184
- * These options are passed to the graphql-codegen method when generating the operations types.
185
- *
186
- * {@link https://www.the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations}
187
- * @default
188
- * ```ts
189
- * const codegenConfig = {
190
- * exportFragmentSpreadSubTypes: true,
191
- * preResolveTypes: true,
192
- * skipTypeNameForRoot: true,
193
- * skipTypename: true,
194
- * useTypeImports: true,
195
- * onlyOperationTypes: true,
196
- * namingConvention: {
197
- * enumValues: 'change-case-all#upperCaseFirst',
198
- * },
199
- * }
200
- * ```
201
- */
202
- codegenConfig?: TypeScriptDocumentsPluginConfig;
203
- }
214
+ };
204
215
 
205
216
  type ModuleOptions = GraphqlMiddlewareConfig;
206
217
  type ModuleHooks = {};
207
218
  declare const _default: NuxtModule<GraphqlMiddlewareConfig>;
208
219
 
209
- export { ModuleHooks, ModuleOptions, _default as default };
220
+ export { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, _default as default };
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
3
  "configKey": "graphqlMiddleware",
4
- "version": "3.0.2",
4
+ "version": "3.1.0-beta.1",
5
5
  "compatibility": {
6
6
  "nuxt": "^3.0.0"
7
7
  }
package/dist/module.mjs CHANGED
@@ -2,7 +2,7 @@ import { fileURLToPath } from 'url';
2
2
  import { defu } from 'defu';
3
3
  import { useLogger, resolveAlias, resolveFiles, defineNuxtModule, createResolver, addImportsDir, addTemplate, addServerHandler, updateTemplates } from '@nuxt/kit';
4
4
  import inquirer from 'inquirer';
5
- import { promises } from 'fs';
5
+ import { promises, existsSync } from 'node:fs';
6
6
  import fragmentImport from '@graphql-fragment-import/lib/inline-imports.js';
7
7
  import { validateGraphQlDocuments } from '@graphql-tools/utils';
8
8
  import { loadSchema } from '@graphql-tools/load';
@@ -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.0.2";
20
+ const version = "3.1.0-beta.1";
21
21
 
22
22
  var GraphqlMiddlewareTemplate = /* @__PURE__ */ ((GraphqlMiddlewareTemplate2) => {
23
23
  GraphqlMiddlewareTemplate2["OperationTypes"] = "graphql-operations.d.ts";
@@ -241,16 +241,108 @@ function inlineFragments(source, resolver) {
241
241
  }
242
242
  });
243
243
  }
244
+ function validateDeprecated(options) {
245
+ const deprecatedKeys = [
246
+ "graphqlEndpoint",
247
+ "serverFetchOptions",
248
+ "onServerResponse",
249
+ "onServerError"
250
+ ];
251
+ deprecatedKeys.forEach((key) => {
252
+ if (typeof options[key] === "function") {
253
+ logger.error(
254
+ `Providing a function for "${key}" via nuxt.config.ts has been removed. Please move the configuration to ~/app/graphqlMiddleware.serverOptions.ts.`
255
+ );
256
+ if (key === "graphqlEndpoint") {
257
+ logger.info(`
258
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/server'
259
+ import { getHeader } from 'h3'
260
+ import acceptLanguageParser from 'accept-language-parser';
261
+
262
+ export default defineGraphqlServerOptions({
263
+ graphqlEndpoint(event, operation, operationName) {
264
+ // Get accepted languages.
265
+ const acceptLanguage = getHeader('accept-language')
266
+ const languages = acceptLanguageParser.parse(acceptLanguage);
267
+
268
+ // Use first match or fallback to English.
269
+ const language = languages[0]?.code || 'en'
270
+ return \`https://api.example.com/\${language}/graphql\`
271
+ }
272
+ })`);
273
+ }
274
+ if (key === "serverFetchOptions") {
275
+ logger.info(`
276
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/server'
277
+ import { getHeader } from 'h3'
278
+
279
+ // Pass the cookie from the client request to the GraphQL request.
280
+ export default defineGraphqlServerOptions({
281
+ serverFetchOptions(event, operation, operationName) {
282
+ return {
283
+ headers: {
284
+ Cookie: getHeader(event, 'cookie')
285
+ }
286
+ }
287
+ }
288
+ })`);
289
+ }
290
+ if (key === "onServerResponse") {
291
+ logger.info(`
292
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/server'
293
+ import type { H3Event } from 'h3'
294
+ import type { FetchResponse } from 'ofetch'
295
+
296
+ export default defineGraphqlServerOptions({
297
+ onServerResponse(event, graphqlResponse) {
298
+ // Set a static header.
299
+ event.node.res.setHeader('x-nuxt-custom-header', 'A custom header value')
300
+
301
+ // Pass the set-cookie header from the GraphQL response to the client.
302
+ const setCookie = graphqlResponse.headers.get('set-cookie')
303
+ if (setCookie) {
304
+ event.node.res.setHeader('set-cookie', setCookie)
305
+ }
306
+
307
+ // Add additional properties to the response.
308
+ graphqlResponse._data.__customProperty = ['My', 'values']
309
+
310
+ // Return the GraphQL response.
311
+ return graphqlResponse._data
312
+ }
313
+ })`);
314
+ }
315
+ if (key === "onServerError") {
316
+ logger.info(`
317
+ import { defineGraphqlServerOptions } from 'nuxt-graphql-middleware/server'
318
+ import type { H3Event } from 'h3'
319
+ import type { FetchError } from 'ofetch'
320
+
321
+ export default defineGraphqlServerOptions({
322
+ onServerError( event, error, operation, operationName) {
323
+ event.setHeader('cache-control', 'no-cache')
324
+ return {
325
+ data: {},
326
+ errors: [error.message]
327
+ }
328
+ }
329
+ })`);
330
+ }
331
+ throw new TypeError("Invalid configuration for graphqlMiddleware." + key);
332
+ }
333
+ });
334
+ }
244
335
  function validateOptions(options) {
245
336
  if (!options.graphqlEndpoint) {
246
337
  throw new Error("Missing graphqlEndpoint.");
247
338
  }
339
+ validateDeprecated(options);
248
340
  }
249
341
  async function getSchemaPath(options, resolver, writeToDisk = false) {
250
342
  const dest = resolver(options.schemaPath);
251
343
  if (!options.downloadSchema) {
252
- const fileExists = await promises.access(dest).then(() => true).catch(() => false);
253
- if (!fileExists) {
344
+ const fileExists2 = await promises.access(dest).then(() => true).catch(() => false);
345
+ if (!fileExists2) {
254
346
  logger.error(
255
347
  '"downloadSchema" is set to false but no schema exists at ' + dest
256
348
  );
@@ -261,8 +353,7 @@ async function getSchemaPath(options, resolver, writeToDisk = false) {
261
353
  if (!options.graphqlEndpoint) {
262
354
  throw new Error("Missing graphqlEndpoint config.");
263
355
  }
264
- const graphqlEndpoint = typeof options.graphqlEndpoint === "string" ? options.graphqlEndpoint : options.graphqlEndpoint();
265
- await generateSchema(graphqlEndpoint, dest, writeToDisk);
356
+ await generateSchema(options.graphqlEndpoint, dest, writeToDisk);
266
357
  return dest;
267
358
  }
268
359
  async function autoImportDocuments(patterns = [], srcResolver) {
@@ -385,6 +476,17 @@ async function generate(options, schemaPath, resolver, srcDir, logEverything = f
385
476
  logger.info("Finished GraphQL code generation.");
386
477
  return { templates, hasErrors };
387
478
  }
479
+ const fileExists = (path, extensions = ["js", "ts"]) => {
480
+ if (!path) {
481
+ return null;
482
+ } else if (existsSync(path)) {
483
+ return path;
484
+ }
485
+ const extension = extensions.find(
486
+ (extension2) => existsSync(`${path}.${extension2}`)
487
+ );
488
+ return extension ? `${path}.${extension}` : null;
489
+ };
388
490
 
389
491
  const module = defineNuxtModule({
390
492
  meta: {
@@ -399,7 +501,6 @@ const module = defineNuxtModule({
399
501
  async setup(passedOptions, nuxt) {
400
502
  const options = defu({}, passedOptions, defaultOptions);
401
503
  validateOptions(options);
402
- const rootDir = nuxt.options.rootDir;
403
504
  const moduleResolver = createResolver(import.meta.url).resolve;
404
505
  const srcDir = nuxt.options.srcDir;
405
506
  const srcResolver = createResolver(srcDir).resolve;
@@ -462,7 +563,7 @@ const module = defineNuxtModule({
462
563
  serverApiPrefix: options.serverApiPrefix
463
564
  };
464
565
  nuxt.options.runtimeConfig.graphqlMiddleware = {
465
- rootDir
566
+ graphqlEndpoint: options.graphqlEndpoint || ""
466
567
  };
467
568
  if (options.includeComposables) {
468
569
  addImportsDir(moduleResolver("runtime/composables"));
@@ -508,6 +609,28 @@ declare module '#graphql-documents' {
508
609
  `;
509
610
  }
510
611
  });
612
+ const extensions = ["js", "mjs", "ts"];
613
+ const resolvedPath = "~/app/graphqlMiddleware.serverOptions".replace(/^(~~|@@)/, nuxt.options.rootDir).replace(/^(~|@)/, nuxt.options.srcDir);
614
+ const template = (() => {
615
+ const resolvedFilename = `graphqlMiddleware.serverOptions.ts`;
616
+ const maybeUserFile = fileExists(resolvedPath, extensions);
617
+ if (maybeUserFile) {
618
+ return addTemplate({
619
+ filename: resolvedFilename,
620
+ write: true,
621
+ getContents: () => `export { default } from '${resolvedPath}'`
622
+ });
623
+ }
624
+ return addTemplate({
625
+ filename: resolvedFilename,
626
+ write: true,
627
+ getContents: () => "export default {}"
628
+ });
629
+ })();
630
+ nuxt.options.nitro.externals = nuxt.options.nitro.externals || {};
631
+ nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || [];
632
+ nuxt.options.nitro.externals.inline.push(template.dst);
633
+ nuxt.options.alias["#graphql-middleware-server-options"] = template.dst;
511
634
  addServerHandler({
512
635
  handler: moduleResolver("./runtime/serverHandler/index"),
513
636
  route: options.serverApiPrefix + "/:operation/:name"
@@ -531,8 +654,8 @@ declare module '#graphql-documents' {
531
654
  }
532
655
  await generateHandler();
533
656
  await updateTemplates({
534
- filter: (template) => {
535
- return template.options && template.options.nuxtGraphqlMiddleware;
657
+ filter: (template2) => {
658
+ return template2.options && template2.options.nuxtGraphqlMiddleware;
536
659
  }
537
660
  });
538
661
  await nitro.hooks.callHook("dev:reload");
@@ -1,18 +1,26 @@
1
1
  import { QueryObject } from 'ufo';
2
2
  import type { H3Event } from 'h3';
3
- import type { FetchOptions } from 'ofetch';
4
- import type { GraphqlMiddlewareConfig } from './../../../types';
3
+ import type { FetchOptions, FetchResponse, FetchError } from 'ofetch';
4
+ import type { GraphqlMiddlewareRuntimeConfig, GraphqlMiddlewareServerOptions } from './../../../types';
5
5
  import { GraphqlMiddlewareOperation } from './../../settings';
6
6
  export declare function queryParamToVariables(query: QueryObject): any;
7
7
  /**
8
8
  * Get the URL of the GraphQL endpoint.
9
9
  */
10
- export declare function getEndpoint(moduleConfig: GraphqlMiddlewareConfig, event: H3Event, operation: GraphqlMiddlewareOperation, operationName: string): string;
10
+ export declare function getEndpoint(config: GraphqlMiddlewareRuntimeConfig, serverOptions: GraphqlMiddlewareServerOptions, event: H3Event, operation: GraphqlMiddlewareOperation, operationName: string): string | Promise<string>;
11
11
  /**
12
12
  * Get the options for the $fetch request to the GraphQL server.
13
13
  */
14
- export declare function getFetchOptions(moduleConfig: GraphqlMiddlewareConfig, event: H3Event, operation: GraphqlMiddlewareOperation, operationName: string): FetchOptions;
14
+ export declare function getFetchOptions(serverOptions: GraphqlMiddlewareServerOptions, event: H3Event, operation: GraphqlMiddlewareOperation, operationName: string): FetchOptions | Promise<FetchOptions>;
15
15
  /**
16
16
  * Assure that the request is valid.
17
17
  */
18
18
  export declare function validateRequest(method?: string, operation?: GraphqlMiddlewareOperation, name?: string, documents?: Record<string, Record<string, string>>): void;
19
+ /**
20
+ * Handle GraphQL server response.
21
+ */
22
+ export declare function onServerResponse(serverOptions: GraphqlMiddlewareServerOptions, event: H3Event, response: FetchResponse<any>, operation?: string, operationName?: string): any;
23
+ /**
24
+ * Handle GraphQL server errors.
25
+ */
26
+ export declare function onServerError(serverOptions: GraphqlMiddlewareServerOptions, event: H3Event, error: FetchError, operation?: string, operationName?: string): any;
@@ -9,26 +9,25 @@ export function queryParamToVariables(query) {
9
9
  }
10
10
  return query;
11
11
  }
12
- export function getEndpoint(moduleConfig, event, operation, operationName) {
13
- if (typeof moduleConfig.graphqlEndpoint === "string") {
14
- return moduleConfig.graphqlEndpoint;
15
- } else if (typeof moduleConfig.graphqlEndpoint === "function") {
16
- const endpoint = moduleConfig.graphqlEndpoint(
12
+ export function getEndpoint(config, serverOptions, event, operation, operationName) {
13
+ if (serverOptions.graphqlEndpoint) {
14
+ const result = serverOptions.graphqlEndpoint(
17
15
  event,
18
16
  operation,
19
17
  operationName
20
18
  );
21
- if (endpoint && typeof endpoint === "string") {
22
- return endpoint;
19
+ if (result) {
20
+ return Promise.resolve(result);
23
21
  }
24
22
  }
23
+ if (config.graphqlEndpoint) {
24
+ return config.graphqlEndpoint;
25
+ }
25
26
  throw new Error("Failed to determine endpoint for GraphQL server.");
26
27
  }
27
- export function getFetchOptions(moduleConfig, event, operation, operationName) {
28
- if (typeof moduleConfig.serverFetchOptions === "function") {
29
- return moduleConfig.serverFetchOptions(event, operation, operationName) || {};
30
- } else if (typeof moduleConfig.serverFetchOptions === "object") {
31
- return moduleConfig.serverFetchOptions;
28
+ export function getFetchOptions(serverOptions, event, operation, operationName) {
29
+ if (serverOptions.serverFetchOptions) {
30
+ return serverOptions.serverFetchOptions(event, operation, operationName) || {};
32
31
  }
33
32
  return {};
34
33
  }
@@ -61,3 +60,24 @@ export function validateRequest(method, operation, name, documents) {
61
60
  throwError(`Operation "${operation}" with name "${name}" not found.`);
62
61
  }
63
62
  }
63
+ export function onServerResponse(serverOptions, event, response, operation, operationName) {
64
+ if (serverOptions.onServerResponse) {
65
+ return serverOptions.onServerResponse(
66
+ event,
67
+ response,
68
+ operation,
69
+ operationName
70
+ );
71
+ }
72
+ return response._data;
73
+ }
74
+ export function onServerError(serverOptions, event, error, operation, operationName) {
75
+ if (serverOptions.onServerError) {
76
+ return serverOptions.onServerError(event, error, operation, operationName);
77
+ }
78
+ throw createError({
79
+ statusCode: 500,
80
+ statusMessage: "Couldn't execute GraphQL query.",
81
+ data: error && "message" in error ? error.message : error
82
+ });
83
+ }
@@ -1,28 +1,36 @@
1
- import {
2
- defineEventHandler,
3
- createError,
4
- getQuery,
5
- getMethod,
6
- readBody
7
- } from "h3";
1
+ import { defineEventHandler, getQuery, getMethod, readBody } from "h3";
8
2
  import {
9
3
  queryParamToVariables,
10
4
  getEndpoint,
11
5
  getFetchOptions,
12
- validateRequest
6
+ validateRequest,
7
+ onServerResponse,
8
+ onServerError
13
9
  } from "./helpers";
14
- import { getModuleConfig } from "./helpers/getModuleConfig.mjs";
15
10
  import { GraphqlMiddlewareOperation } from "./../settings/index.mjs";
16
11
  import { documents } from "#graphql-documents";
12
+ import { useRuntimeConfig } from "#imports";
13
+ import serverOptions from "#graphql-middleware-server-options";
17
14
  export default defineEventHandler(async (event) => {
18
15
  const method = getMethod(event);
19
16
  const operation = event.context.params.operation;
20
17
  const name = event.context.params.name;
21
18
  validateRequest(method, operation, name, documents);
22
19
  const query = documents[operation][name];
23
- const config = await getModuleConfig();
24
- const endpoint = getEndpoint(config, event, operation, name);
25
- const fetchOptions = getFetchOptions(config, event, operation, name);
20
+ const runtimeConfig = useRuntimeConfig().graphqlMiddleware;
21
+ const endpoint = await getEndpoint(
22
+ runtimeConfig,
23
+ serverOptions,
24
+ event,
25
+ operation,
26
+ name
27
+ );
28
+ const fetchOptions = await getFetchOptions(
29
+ serverOptions,
30
+ event,
31
+ operation,
32
+ name
33
+ );
26
34
  const variables = operation === GraphqlMiddlewareOperation.Query ? queryParamToVariables(getQuery(event)) : await readBody(event);
27
35
  return $fetch.raw(endpoint, {
28
36
  method: "POST",
@@ -32,18 +40,8 @@ export default defineEventHandler(async (event) => {
32
40
  },
33
41
  ...fetchOptions
34
42
  }).then((response) => {
35
- if (config.onServerResponse) {
36
- return config.onServerResponse(event, response, operation, name);
37
- }
38
- return response._data;
39
- }).catch((err) => {
40
- if (config.onServerError) {
41
- return config.onServerError(event, err, operation, name);
42
- }
43
- throw createError({
44
- statusCode: 500,
45
- statusMessage: "Couldn't execute GraphQL query.",
46
- data: err && "message" in err ? err.message : err
47
- });
43
+ return onServerResponse(serverOptions, event, response, operation, name);
44
+ }).catch((error) => {
45
+ return onServerError(serverOptions, event, error, operation, name);
48
46
  });
49
47
  });
@@ -0,0 +1,2 @@
1
+ import type { GraphqlMiddlewareServerOptions } from './../../types';
2
+ export declare function defineGraphqlServerOptions(options: GraphqlMiddlewareServerOptions): GraphqlMiddlewareServerOptions;
@@ -0,0 +1,3 @@
1
+ export function defineGraphqlServerOptions(options) {
2
+ return options;
3
+ }
package/dist/types.d.ts CHANGED
@@ -8,4 +8,4 @@ declare module '@nuxt/schema' {
8
8
  }
9
9
 
10
10
 
11
- export { ModuleHooks, ModuleOptions, default } from './module'
11
+ export { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, default } from './module'
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
- "version": "3.0.2",
3
+ "version": "3.1.0-beta.1",
4
4
  "description": "Module to perform GraphQL requests as a server middleware.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -12,7 +12,9 @@
12
12
  ".": {
13
13
  "import": "./dist/module.mjs",
14
14
  "require": "./dist/module.cjs"
15
- }
15
+ },
16
+ "./server": "./dist/runtime/serverOptions/index.mjs",
17
+ "./server/*": "./dist/runtime/serverOptions/*.mjs"
16
18
  },
17
19
  "main": "./dist/module.cjs",
18
20
  "types": "./dist/types.d.ts",
@@ -1,6 +0,0 @@
1
- import type { GraphqlMiddlewareConfig } from './../../../types';
2
- /**
3
- * Due to nuxt's architecture, we have to manually load the runtime configuration.
4
- * This is only done for the first time and we cache the config locally.
5
- */
6
- export declare function getModuleConfig(): Promise<GraphqlMiddlewareConfig>;
@@ -1,15 +0,0 @@
1
- import { loadNuxtConfig } from "@nuxt/kit";
2
- import { useRuntimeConfig } from "#imports";
3
- let moduleConfig = null;
4
- export function getModuleConfig() {
5
- if (moduleConfig) {
6
- return Promise.resolve(moduleConfig);
7
- }
8
- const { graphqlMiddleware } = useRuntimeConfig();
9
- return loadNuxtConfig({
10
- cwd: graphqlMiddleware.rootDir
11
- }).then((v) => {
12
- moduleConfig = v.graphqlMiddleware;
13
- return v.graphqlMiddleware;
14
- });
15
- }