@lewebsimple/nuxt-graphql 0.6.6 → 0.6.8
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 +1 -1
- package/dist/module.mjs +1 -1
- package/dist/runtime/app/composables/useAsyncGraphQLQuery.d.ts +3 -2
- package/dist/runtime/app/composables/useAsyncGraphQLQuery.js +3 -2
- package/dist/runtime/server/api/graphql.js +5 -10
- package/dist/runtime/server/lib/yoga.d.ts +2 -1
- package/dist/runtime/shared/lib/error.js +11 -3
- package/package.json +1 -1
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { type AsyncData, type AsyncDataOptions } from "#app";
|
|
2
2
|
import type { QueryName, ResultOf, VariablesOf } from "#graphql/registry";
|
|
3
3
|
import { type MaybeRefOrGetter } from "#imports";
|
|
4
|
+
import type { NormalizedError } from "../../shared/lib/error.js";
|
|
4
5
|
import type { CacheConfig, IsEmptyObject } from "../../shared/lib/types.js";
|
|
5
|
-
type UseAsyncGraphQLQueryOptions<TName extends QueryName, TTransformed = ResultOf<TName>> = Omit<AsyncDataOptions<ResultOf<TName>>, "transform"> & {
|
|
6
|
+
type UseAsyncGraphQLQueryOptions<TName extends QueryName, TTransformed = ResultOf<TName>> = Omit<AsyncDataOptions<ResultOf<TName>>, "transform" | "pick"> & {
|
|
6
7
|
transform?: (input: ResultOf<TName>) => TTransformed;
|
|
7
8
|
cache?: Partial<CacheConfig>;
|
|
8
9
|
};
|
|
@@ -13,5 +14,5 @@ type UseAsyncGraphQLQueryOptions<TName extends QueryName, TTransformed = ResultO
|
|
|
13
14
|
* @param args Operation variables (if any) and optional HTTP headers.
|
|
14
15
|
* @returns Nuxt AsyncData wrapper for the query result.
|
|
15
16
|
*/
|
|
16
|
-
export declare function useAsyncGraphQLQuery<TName extends QueryName, TTransformed = ResultOf<TName>>(operationName: TName, ...args: IsEmptyObject<VariablesOf<TName>> extends true ? [variables?: MaybeRefOrGetter<VariablesOf<TName>>, options?: UseAsyncGraphQLQueryOptions<TName, TTransformed>] : [variables: MaybeRefOrGetter<VariablesOf<TName>>, options?: UseAsyncGraphQLQueryOptions<TName, TTransformed>]): AsyncData<TTransformed |
|
|
17
|
+
export declare function useAsyncGraphQLQuery<TName extends QueryName, TTransformed = ResultOf<TName>>(operationName: TName, ...args: IsEmptyObject<VariablesOf<TName>> extends true ? [variables?: MaybeRefOrGetter<VariablesOf<TName>>, options?: UseAsyncGraphQLQueryOptions<TName, TTransformed>] : [variables: MaybeRefOrGetter<VariablesOf<TName>>, options?: UseAsyncGraphQLQueryOptions<TName, TTransformed>]): AsyncData<TTransformed | undefined, NormalizedError | undefined>;
|
|
17
18
|
export {};
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { useAsyncData, useNuxtApp, useNuxtData, useRuntimeConfig } from "#app";
|
|
2
2
|
import { computed, toValue } from "#imports";
|
|
3
|
+
import { normalizeError } from "../../shared/lib/error.js";
|
|
3
4
|
import { getOperationDocument } from "../../shared/lib/registry.js";
|
|
4
5
|
import { getCacheKeyParts, resolveCacheConfig } from "../lib/cache.js";
|
|
5
6
|
import { getInFlightRequests } from "../lib/in-flight.js";
|
|
@@ -61,7 +62,7 @@ export function useAsyncGraphQLQuery(operationName, ...args) {
|
|
|
61
62
|
if (cachedValue !== void 0) {
|
|
62
63
|
return cachedValue;
|
|
63
64
|
}
|
|
64
|
-
throw error;
|
|
65
|
+
throw normalizeError(error);
|
|
65
66
|
}
|
|
66
67
|
}
|
|
67
68
|
// Stale-while-revalidate: return cached value if exists, then revalidate in background
|
|
@@ -78,7 +79,7 @@ export function useAsyncGraphQLQuery(operationName, ...args) {
|
|
|
78
79
|
return await fetchAndPersist();
|
|
79
80
|
}
|
|
80
81
|
default:
|
|
81
|
-
throw new Error(`Unknown cache policy: ${cacheConfig.policy}`);
|
|
82
|
+
throw normalizeError(new Error(`Unknown cache policy: ${cacheConfig.policy}`));
|
|
82
83
|
}
|
|
83
84
|
}
|
|
84
85
|
return useAsyncData(cacheKey, asyncDataHandler, { ...asyncDataOptions, transform });
|
|
@@ -2,14 +2,9 @@ import { defineEventHandler, sendWebResponse, toWebRequest } from "h3";
|
|
|
2
2
|
import { createContext } from "#graphql/context";
|
|
3
3
|
import { getYogaInstance } from "../lib/yoga.js";
|
|
4
4
|
export default defineEventHandler(async (event) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
return sendWebResponse(event, response);
|
|
11
|
-
} catch (error) {
|
|
12
|
-
console.error("GraphQL Yoga error:", error);
|
|
13
|
-
throw error;
|
|
14
|
-
}
|
|
5
|
+
const yoga = getYogaInstance();
|
|
6
|
+
const request = toWebRequest(event);
|
|
7
|
+
const context = await createContext(event);
|
|
8
|
+
const response = await yoga.handleRequest(request, context);
|
|
9
|
+
return sendWebResponse(event, response);
|
|
15
10
|
});
|
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import type { GraphQLContext } from "#graphql/context";
|
|
1
2
|
/**
|
|
2
3
|
* Get or create the singleton GraphQL Yoga instance.
|
|
3
4
|
*
|
|
4
5
|
* @returns GraphQL Yoga server instance.
|
|
5
6
|
*/
|
|
6
|
-
export declare function getYogaInstance(): import("graphql-yoga").YogaServerInstance<
|
|
7
|
+
export declare function getYogaInstance(): import("graphql-yoga").YogaServerInstance<GraphQLContext, {}>;
|
|
@@ -14,6 +14,9 @@ function isGraphQLError(error) {
|
|
|
14
14
|
function isGraphQLErrorArray(error) {
|
|
15
15
|
return Array.isArray(error) && error.every(isGraphQLError);
|
|
16
16
|
}
|
|
17
|
+
function isGraphQLExecutionError(error) {
|
|
18
|
+
return typeof error === "object" && error !== null && "errors" in error && isGraphQLErrorArray(error.errors);
|
|
19
|
+
}
|
|
17
20
|
export function extractCode(error) {
|
|
18
21
|
const code = error?.extensions?.code;
|
|
19
22
|
return typeof code === "string" ? code : void 0;
|
|
@@ -29,16 +32,21 @@ export function normalizeError(error) {
|
|
|
29
32
|
code: extractCode(error)
|
|
30
33
|
});
|
|
31
34
|
}
|
|
32
|
-
if (
|
|
33
|
-
const errors = error
|
|
35
|
+
if (isGraphQLExecutionError(error)) {
|
|
36
|
+
const { errors } = error;
|
|
34
37
|
return new NormalizedError({
|
|
35
38
|
message: errors.map(({ message }) => message).join("\n"),
|
|
36
39
|
errors,
|
|
37
40
|
code: extractCode(errors[0])
|
|
38
41
|
});
|
|
39
42
|
}
|
|
43
|
+
if (error instanceof Error) {
|
|
44
|
+
return new NormalizedError({
|
|
45
|
+
message: error.message
|
|
46
|
+
});
|
|
47
|
+
}
|
|
40
48
|
return new NormalizedError({
|
|
41
|
-
message:
|
|
49
|
+
message: JSON.stringify(error),
|
|
42
50
|
code: "INTERNAL_ERROR"
|
|
43
51
|
});
|
|
44
52
|
}
|