nuxt-graphql-middleware 3.1.0-beta.2 → 3.1.0-beta.4

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,4 +1,6 @@
1
1
  import * as _nuxt_schema from '@nuxt/schema';
2
+ import { Types } from '@graphql-codegen/plugin-helpers';
3
+ import { SchemaASTConfig } from '@graphql-codegen/schema-ast';
2
4
  import { TypeScriptDocumentsPluginConfig } from '@graphql-codegen/typescript-operations';
3
5
  import { H3Event } from 'h3';
4
6
  import { FetchOptions, FetchResponse, FetchError } from 'ofetch';
@@ -193,7 +195,8 @@ interface ModuleOptions {
193
195
  */
194
196
  schemaPath?: string;
195
197
  /**
196
- * 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.
197
200
  *
198
201
  * {@link https://www.the-guild.dev/graphql/codegen/plugins/typescript/typescript-operations}
199
202
  * @default
@@ -212,6 +215,31 @@ interface ModuleOptions {
212
215
  * ```
213
216
  */
214
217
  codegenConfig?: TypeScriptDocumentsPluginConfig;
218
+ /**
219
+ * Configuration for graphql-codegen when downloading the schema.
220
+ */
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
+ };
215
243
  }
216
244
  type ModuleHooks = {};
217
245
  declare const _default: _nuxt_schema.NuxtModule<ModuleOptions>;
package/dist/module.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
3
  "configKey": "graphqlMiddleware",
4
- "version": "3.1.0-beta.2",
4
+ "version": "3.1.0-beta.4",
5
5
  "compatibility": {
6
6
  "nuxt": "^3.1.0"
7
7
  }
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.2";
20
+ const version = "3.1.0-beta.4";
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({
@@ -229,7 +230,7 @@ const defaultOptions = {
229
230
  debug: false,
230
231
  includeComposables: true,
231
232
  documents: [],
232
- autoImportPatterns: ["**/*.{gql,graphql}", "!node_modules"]
233
+ autoImportPatterns: []
233
234
  };
234
235
  function inlineFragments(source, resolver) {
235
236
  return fragmentImport(source, {
@@ -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) {
@@ -500,6 +501,9 @@ const module = defineNuxtModule({
500
501
  defaults: defaultOptions,
501
502
  async setup(passedOptions, nuxt) {
502
503
  const options = defu({}, passedOptions, defaultOptions);
504
+ if (!passedOptions.autoImportPatterns) {
505
+ options.autoImportPatterns = ["**/*.{gql,graphql}", "!node_modules"];
506
+ }
503
507
  validateOptions(options);
504
508
  const moduleResolver = createResolver(import.meta.url).resolve;
505
509
  const srcDir = nuxt.options.srcDir;
@@ -1,3 +1,4 @@
1
+ import type { FetchOptions } from 'ofetch';
1
2
  import { GraphqlMiddlewareState } from './../../types';
2
3
  import type { GraphqlMiddlewareQuery, GraphqlMiddlewareMutation } from '#build/nuxt-graphql-middleware';
3
4
  type GraphqlMiddlewareQueryName = keyof GraphqlMiddlewareQuery;
@@ -6,10 +7,29 @@ type GetQueryArgs<T extends GraphqlMiddlewareQueryName, M extends GraphqlMiddlew
6
7
  type GetMutationArgs<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T][0] extends null ? [T] : M[T][1] extends false ? [T, M[T][0]] : [T, M[T][0]?];
7
8
  type GraphqlResponse<T> = {
8
9
  data: T;
10
+ errors: any[];
9
11
  };
10
12
  type GetQueryResult<T extends GraphqlMiddlewareQueryName, M extends GraphqlMiddlewareQuery> = M[T] extends undefined ? undefined : GraphqlResponse<M[T][2]>;
11
13
  type GetMutationResult<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T] extends undefined ? undefined : GraphqlResponse<M[T][2]>;
12
14
  export declare const useGraphqlState: () => GraphqlMiddlewareState;
13
- export declare function useGraphqlQuery<T extends GraphqlMiddlewareQueryName>(...args: GetQueryArgs<T, GraphqlMiddlewareQuery>): Promise<GetQueryResult<T, GraphqlMiddlewareQuery>>;
14
- export declare function useGraphqlMutation<T extends GraphqlMiddlewareMutationName>(...args: GetMutationArgs<T, GraphqlMiddlewareMutation>): Promise<GetMutationResult<T, GraphqlMiddlewareMutation>>;
15
+ type QueryObjectArgs<T extends GraphqlMiddlewareQueryName, M extends GraphqlMiddlewareQuery> = M[T][0] extends null ? {
16
+ name: T;
17
+ fetchOptions?: FetchOptions;
18
+ variables?: null;
19
+ } : {
20
+ name: T;
21
+ variables: M[T][0];
22
+ fetchOptions?: FetchOptions;
23
+ };
24
+ type MutationObjectArgs<T extends GraphqlMiddlewareMutationName, M extends GraphqlMiddlewareMutation> = M[T][0] extends null ? {
25
+ name: T;
26
+ variables?: null;
27
+ fetchOptions?: FetchOptions;
28
+ } : {
29
+ name: T;
30
+ variables: M[T][0];
31
+ fetchOptions?: FetchOptions;
32
+ };
33
+ export declare function useGraphqlQuery<T extends GraphqlMiddlewareQueryName>(...args: GetQueryArgs<T, GraphqlMiddlewareQuery> | [QueryObjectArgs<T, GraphqlMiddlewareQuery>]): Promise<GetQueryResult<T, GraphqlMiddlewareQuery>>;
34
+ export declare function useGraphqlMutation<T extends GraphqlMiddlewareMutationName>(...args: GetMutationArgs<T, GraphqlMiddlewareMutation> | [MutationObjectArgs<T, GraphqlMiddlewareMutation>]): Promise<GetMutationResult<T, GraphqlMiddlewareMutation>>;
15
35
  export {};
@@ -11,28 +11,34 @@ export const useGraphqlState = () => {
11
11
  return state;
12
12
  };
13
13
  export function useGraphqlQuery(...args) {
14
- const name = args[0];
15
- if (typeof name !== "string") {
16
- return Promise.reject(new Error("Invalid query name"));
17
- }
14
+ const [name, variables, fetchOptions = {}] = typeof args[0] === "string" ? [args[0], args[1]] : [args[0].name, args[0].variables, args[0].fetchOptions];
18
15
  const state2 = useGraphqlState();
19
16
  return $fetch(getEndpoint("query", name), {
20
- params: buildRequestParams(args[1]),
17
+ params: buildRequestParams(variables),
21
18
  // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
22
- ...state2.fetchOptions
19
+ ...state2.fetchOptions,
20
+ ...fetchOptions
21
+ }).then((v) => {
22
+ return {
23
+ data: v.data,
24
+ errors: v.errors || []
25
+ };
23
26
  });
24
27
  }
25
28
  export function useGraphqlMutation(...args) {
29
+ const [name, variables, fetchOptions = {}] = typeof args[0] === "string" ? [args[0], args[1]] : [args[0].name, args[0].variables, args[0].fetchOptions];
26
30
  const state2 = useGraphqlState();
27
- const name = args[0];
28
- const body = args[1] || {};
29
- if (typeof name !== "string") {
30
- return Promise.reject(new Error("Invalid mutation name"));
31
- }
32
31
  return $fetch(getEndpoint("mutation", name), {
33
32
  // @todo: Remove any once https://github.com/unjs/nitro/pull/883 is released.
34
33
  method: "post",
35
- body,
36
- ...state2.fetchOptions
34
+ body: variables,
35
+ ...state2.fetchOptions,
36
+ ...fetchOptions
37
+ }).then((v) => {
38
+ console.log(v);
39
+ return {
40
+ data: v.data,
41
+ errors: v.errors || []
42
+ };
37
43
  });
38
44
  }
@@ -75,9 +75,10 @@ export function onServerError(serverOptions, event, error, operation, operationN
75
75
  if (serverOptions.onServerError) {
76
76
  return serverOptions.onServerError(event, error, operation, operationName);
77
77
  }
78
+ const message = error && "message" in error ? error.message : "";
78
79
  throw createError({
79
80
  statusCode: 500,
80
- statusMessage: "Couldn't execute GraphQL query.",
81
+ statusMessage: "Couldn't execute GraphQL query: " + message,
81
82
  data: error && "message" in error ? error.message : error
82
83
  });
83
84
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
- "version": "3.1.0-beta.2",
3
+ "version": "3.1.0-beta.4",
4
4
  "description": "Module to perform GraphQL requests as a server middleware.",
5
5
  "repository": {
6
6
  "type": "git",