nuxt-graphql-middleware 3.0.2 → 3.1.0-beta.0
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 +66 -53
- package/dist/module.json +1 -1
- package/dist/module.mjs +138 -11
- package/dist/runtime/serverHandler/helpers/index.d.ts +12 -4
- package/dist/runtime/serverHandler/helpers/index.mjs +32 -12
- package/dist/runtime/serverHandler/index.mjs +23 -25
- package/dist/types.d.ts +1 -1
- package/package.json +1 -1
- package/dist/runtime/serverHandler/helpers/getModuleConfig.d.ts +0 -6
- package/dist/runtime/serverHandler/helpers/getModuleConfig.mjs +0 -15
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
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
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
|
-
* @
|
|
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
|
-
|
|
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?:
|
|
156
|
+
serverFetchOptions?: GraphqlMiddlewareServerFetchOptionsMethod;
|
|
111
157
|
/**
|
|
112
158
|
* Handle the response from the GraphQL server.
|
|
113
159
|
*
|
|
@@ -165,45 +211,12 @@ 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
|
-
|
|
220
|
+
declare function defineGraphqlServerOptions(options: GraphqlMiddlewareServerOptions): GraphqlMiddlewareServerOptions;
|
|
221
|
+
|
|
222
|
+
export { GraphqlMiddlewareServerOptions, ModuleHooks, ModuleOptions, _default as default, defineGraphqlServerOptions };
|
package/dist/module.json
CHANGED
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.
|
|
20
|
+
const version = "3.1.0-beta.0";
|
|
21
21
|
|
|
22
22
|
var GraphqlMiddlewareTemplate = /* @__PURE__ */ ((GraphqlMiddlewareTemplate2) => {
|
|
23
23
|
GraphqlMiddlewareTemplate2["OperationTypes"] = "graphql-operations.d.ts";
|
|
@@ -241,16 +241,109 @@ 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'
|
|
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'
|
|
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'
|
|
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'
|
|
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
|
+
}
|
|
332
|
+
throw new TypeError("Invalid configuration for graphqlMiddleware." + key);
|
|
333
|
+
}
|
|
334
|
+
});
|
|
335
|
+
}
|
|
244
336
|
function validateOptions(options) {
|
|
245
337
|
if (!options.graphqlEndpoint) {
|
|
246
338
|
throw new Error("Missing graphqlEndpoint.");
|
|
247
339
|
}
|
|
340
|
+
validateDeprecated(options);
|
|
248
341
|
}
|
|
249
342
|
async function getSchemaPath(options, resolver, writeToDisk = false) {
|
|
250
343
|
const dest = resolver(options.schemaPath);
|
|
251
344
|
if (!options.downloadSchema) {
|
|
252
|
-
const
|
|
253
|
-
if (!
|
|
345
|
+
const fileExists2 = await promises.access(dest).then(() => true).catch(() => false);
|
|
346
|
+
if (!fileExists2) {
|
|
254
347
|
logger.error(
|
|
255
348
|
'"downloadSchema" is set to false but no schema exists at ' + dest
|
|
256
349
|
);
|
|
@@ -261,8 +354,7 @@ async function getSchemaPath(options, resolver, writeToDisk = false) {
|
|
|
261
354
|
if (!options.graphqlEndpoint) {
|
|
262
355
|
throw new Error("Missing graphqlEndpoint config.");
|
|
263
356
|
}
|
|
264
|
-
|
|
265
|
-
await generateSchema(graphqlEndpoint, dest, writeToDisk);
|
|
357
|
+
await generateSchema(options.graphqlEndpoint, dest, writeToDisk);
|
|
266
358
|
return dest;
|
|
267
359
|
}
|
|
268
360
|
async function autoImportDocuments(patterns = [], srcResolver) {
|
|
@@ -385,6 +477,17 @@ async function generate(options, schemaPath, resolver, srcDir, logEverything = f
|
|
|
385
477
|
logger.info("Finished GraphQL code generation.");
|
|
386
478
|
return { templates, hasErrors };
|
|
387
479
|
}
|
|
480
|
+
const fileExists = (path, extensions = ["js", "ts"]) => {
|
|
481
|
+
if (!path) {
|
|
482
|
+
return null;
|
|
483
|
+
} else if (existsSync(path)) {
|
|
484
|
+
return path;
|
|
485
|
+
}
|
|
486
|
+
const extension = extensions.find(
|
|
487
|
+
(extension2) => existsSync(`${path}.${extension2}`)
|
|
488
|
+
);
|
|
489
|
+
return extension ? `${path}.${extension}` : null;
|
|
490
|
+
};
|
|
388
491
|
|
|
389
492
|
const module = defineNuxtModule({
|
|
390
493
|
meta: {
|
|
@@ -399,7 +502,6 @@ const module = defineNuxtModule({
|
|
|
399
502
|
async setup(passedOptions, nuxt) {
|
|
400
503
|
const options = defu({}, passedOptions, defaultOptions);
|
|
401
504
|
validateOptions(options);
|
|
402
|
-
const rootDir = nuxt.options.rootDir;
|
|
403
505
|
const moduleResolver = createResolver(import.meta.url).resolve;
|
|
404
506
|
const srcDir = nuxt.options.srcDir;
|
|
405
507
|
const srcResolver = createResolver(srcDir).resolve;
|
|
@@ -462,7 +564,7 @@ const module = defineNuxtModule({
|
|
|
462
564
|
serverApiPrefix: options.serverApiPrefix
|
|
463
565
|
};
|
|
464
566
|
nuxt.options.runtimeConfig.graphqlMiddleware = {
|
|
465
|
-
|
|
567
|
+
graphqlEndpoint: options.graphqlEndpoint || ""
|
|
466
568
|
};
|
|
467
569
|
if (options.includeComposables) {
|
|
468
570
|
addImportsDir(moduleResolver("runtime/composables"));
|
|
@@ -508,6 +610,28 @@ declare module '#graphql-documents' {
|
|
|
508
610
|
`;
|
|
509
611
|
}
|
|
510
612
|
});
|
|
613
|
+
const extensions = ["js", "mjs", "ts"];
|
|
614
|
+
const resolvedPath = "~/app/graphqlMiddleware.serverOptions".replace(/^(~~|@@)/, nuxt.options.rootDir).replace(/^(~|@)/, nuxt.options.srcDir);
|
|
615
|
+
const template = (() => {
|
|
616
|
+
const resolvedFilename = `graphqlMiddleware.serverOptions.ts`;
|
|
617
|
+
const maybeUserFile = fileExists(resolvedPath, extensions);
|
|
618
|
+
if (maybeUserFile) {
|
|
619
|
+
return addTemplate({
|
|
620
|
+
filename: resolvedFilename,
|
|
621
|
+
write: true,
|
|
622
|
+
getContents: () => `export { default } from '${resolvedPath}'`
|
|
623
|
+
});
|
|
624
|
+
}
|
|
625
|
+
return addTemplate({
|
|
626
|
+
filename: resolvedFilename,
|
|
627
|
+
write: true,
|
|
628
|
+
getContents: () => "export default {}"
|
|
629
|
+
});
|
|
630
|
+
})();
|
|
631
|
+
nuxt.options.nitro.externals = nuxt.options.nitro.externals || {};
|
|
632
|
+
nuxt.options.nitro.externals.inline = nuxt.options.nitro.externals.inline || [];
|
|
633
|
+
nuxt.options.nitro.externals.inline.push(template.dst);
|
|
634
|
+
nuxt.options.alias["#graphql-middleware-server-options"] = template.dst;
|
|
511
635
|
addServerHandler({
|
|
512
636
|
handler: moduleResolver("./runtime/serverHandler/index"),
|
|
513
637
|
route: options.serverApiPrefix + "/:operation/:name"
|
|
@@ -531,8 +655,8 @@ declare module '#graphql-documents' {
|
|
|
531
655
|
}
|
|
532
656
|
await generateHandler();
|
|
533
657
|
await updateTemplates({
|
|
534
|
-
filter: (
|
|
535
|
-
return
|
|
658
|
+
filter: (template2) => {
|
|
659
|
+
return template2.options && template2.options.nuxtGraphqlMiddleware;
|
|
536
660
|
}
|
|
537
661
|
});
|
|
538
662
|
await nitro.hooks.callHook("dev:reload");
|
|
@@ -541,5 +665,8 @@ declare module '#graphql-documents' {
|
|
|
541
665
|
}
|
|
542
666
|
}
|
|
543
667
|
});
|
|
668
|
+
function defineGraphqlServerOptions(options) {
|
|
669
|
+
return options;
|
|
670
|
+
}
|
|
544
671
|
|
|
545
|
-
export { module as default };
|
|
672
|
+
export { module as default, defineGraphqlServerOptions };
|
|
@@ -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 {
|
|
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(
|
|
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(
|
|
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(
|
|
13
|
-
if (
|
|
14
|
-
|
|
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 (
|
|
22
|
-
return
|
|
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(
|
|
28
|
-
if (
|
|
29
|
-
return
|
|
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
|
|
24
|
-
const endpoint = getEndpoint(
|
|
25
|
-
|
|
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
|
-
|
|
36
|
-
|
|
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
|
});
|
package/dist/types.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -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
|
-
}
|