@lewebsimple/nuxt-graphql 0.6.15 → 0.6.16
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/README.md
CHANGED
|
@@ -363,20 +363,34 @@ const result = await mutate({ title: "New Film" });
|
|
|
363
363
|
|
|
364
364
|
You can define custom logic around the remote executor for each remote schema by using the auto-imported `defineRemoteExecutorHooks` helper.
|
|
365
365
|
|
|
366
|
+
All hooks receive the GraphQL context as a second parameter for convenient access.
|
|
367
|
+
|
|
366
368
|
For the example configuration above, create [server/graphql/swapi-hooks.ts](server/graphql/swapi-hooks.ts):
|
|
367
369
|
|
|
368
370
|
```ts
|
|
369
371
|
import { defu } from "defu";
|
|
370
372
|
|
|
371
373
|
export default defineRemoteExecutorHooks({
|
|
372
|
-
onRequest(request) {
|
|
373
|
-
|
|
374
|
+
onRequest(request, context) {
|
|
375
|
+
// Context is available as second parameter
|
|
376
|
+
const { remoteAuthToken } = context || {};
|
|
374
377
|
request.extensions = defu(request.extensions, {
|
|
375
378
|
headers: {
|
|
376
379
|
"XAuthorization": `Bearer ${remoteAuthToken || ""}`,
|
|
377
380
|
},
|
|
378
381
|
});
|
|
379
382
|
},
|
|
383
|
+
|
|
384
|
+
onResult(result, context) {
|
|
385
|
+
// You can also access context in onResult
|
|
386
|
+
console.log("User from context:", context?.user);
|
|
387
|
+
console.log("Result:", result.data);
|
|
388
|
+
},
|
|
389
|
+
|
|
390
|
+
onError(error, context) {
|
|
391
|
+
// And in onError for logging/monitoring
|
|
392
|
+
console.error("Remote execution failed for user:", context?.user?.id);
|
|
393
|
+
},
|
|
380
394
|
});
|
|
381
395
|
```
|
|
382
396
|
|
package/dist/module.json
CHANGED
package/dist/module.mjs
CHANGED
|
@@ -13,9 +13,9 @@ type GraphQLExecutionResult<TData = unknown> = ExecutionResult<TData> & {
|
|
|
13
13
|
};
|
|
14
14
|
};
|
|
15
15
|
export type GraphQLRemoteExecHooks<TData = unknown> = {
|
|
16
|
-
onRequest?: (request: GraphQLExecutionRequest) => void | Promise<void>;
|
|
17
|
-
onResult?: (result: GraphQLExecutionResult<TData
|
|
18
|
-
onError?: (error: unknown) => void | Promise<void>;
|
|
16
|
+
onRequest?: (request: GraphQLExecutionRequest, context: GraphQLContext | undefined) => void | Promise<void>;
|
|
17
|
+
onResult?: (result: GraphQLExecutionResult<TData>, context: GraphQLContext | undefined) => void | Promise<void>;
|
|
18
|
+
onError?: (error: unknown, context: GraphQLContext | undefined) => void | Promise<void>;
|
|
19
19
|
};
|
|
20
20
|
export type RemoteExecutorInput = {
|
|
21
21
|
endpoint: string;
|
|
@@ -3,8 +3,9 @@ import { mergeHeaders } from "../../shared/lib/headers.js";
|
|
|
3
3
|
export function getRemoteExecutor({ endpoint, headers, hooks }) {
|
|
4
4
|
return async function execute(request) {
|
|
5
5
|
try {
|
|
6
|
+
const context = request.context;
|
|
6
7
|
for (const hook of hooks) {
|
|
7
|
-
await hook.onRequest?.(request);
|
|
8
|
+
await hook.onRequest?.(request, context);
|
|
8
9
|
}
|
|
9
10
|
const response = await fetch(endpoint, {
|
|
10
11
|
method: "POST",
|
|
@@ -27,12 +28,13 @@ export function getRemoteExecutor({ endpoint, headers, hooks }) {
|
|
|
27
28
|
headers: responseHeaders
|
|
28
29
|
};
|
|
29
30
|
for (const hook of hooks) {
|
|
30
|
-
await hook.onResult?.(result);
|
|
31
|
+
await hook.onResult?.(result, context);
|
|
31
32
|
}
|
|
32
33
|
return result;
|
|
33
34
|
} catch (error) {
|
|
35
|
+
const context = request.context;
|
|
34
36
|
for (const hook of hooks) {
|
|
35
|
-
await hook.onError?.(error);
|
|
37
|
+
await hook.onError?.(error, context);
|
|
36
38
|
}
|
|
37
39
|
throw error;
|
|
38
40
|
}
|