nuxt-graphql-middleware 3.1.0-beta.3 → 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.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "nuxt-graphql-middleware",
3
3
  "configKey": "graphqlMiddleware",
4
- "version": "3.1.0-beta.3",
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.3";
20
+ const version = "3.1.0-beta.4";
21
21
 
22
22
  var GraphqlMiddlewareTemplate = /* @__PURE__ */ ((GraphqlMiddlewareTemplate2) => {
23
23
  GraphqlMiddlewareTemplate2["OperationTypes"] = "graphql-operations.d.ts";
@@ -230,7 +230,7 @@ const defaultOptions = {
230
230
  debug: false,
231
231
  includeComposables: true,
232
232
  documents: [],
233
- autoImportPatterns: ["**/*.{gql,graphql}", "!node_modules"]
233
+ autoImportPatterns: []
234
234
  };
235
235
  function inlineFragments(source, resolver) {
236
236
  return fragmentImport(source, {
@@ -501,6 +501,9 @@ const module = defineNuxtModule({
501
501
  defaults: defaultOptions,
502
502
  async setup(passedOptions, nuxt) {
503
503
  const options = defu({}, passedOptions, defaultOptions);
504
+ if (!passedOptions.autoImportPatterns) {
505
+ options.autoImportPatterns = ["**/*.{gql,graphql}", "!node_modules"];
506
+ }
504
507
  validateOptions(options);
505
508
  const moduleResolver = createResolver(import.meta.url).resolve;
506
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.3",
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",