refactor: update to nuxt-graphql 0.5.x
This commit is contained in:
@@ -1,11 +1,6 @@
|
||||
import { defineGraphQLContext } from "@lewebsimple/nuxt-graphql/helpers";
|
||||
import type { AuthLoginMutation } from "#graphql/typed-documents";
|
||||
|
||||
export default defineGraphQLContext(async (event) => {
|
||||
const authToken = await getAuthToken(event);
|
||||
|
||||
return {
|
||||
authToken,
|
||||
handleLogin: async (loginData: AuthLoginMutation) => handleLogin(event, loginData),
|
||||
};
|
||||
});
|
||||
|
||||
11
wp-content/themes/moonshine/server/graphql/wp-hooks.ts
Normal file
11
wp-content/themes/moonshine/server/graphql/wp-hooks.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export default defineRemoteExecutorHooks({
|
||||
onRequest(request) {
|
||||
if (request.context.authToken) {
|
||||
request.extensions ??= {};
|
||||
request.extensions.headers = {
|
||||
...request.extensions.headers,
|
||||
Authorization: `Bearer ${request.context.authToken}`,
|
||||
};
|
||||
}
|
||||
},
|
||||
});
|
||||
@@ -1,20 +0,0 @@
|
||||
import type { AuthLoginMutation } from "#build/graphql/typed-documents";
|
||||
import { defineRemoteExecMiddleware } from "@lewebsimple/nuxt-graphql/helpers";
|
||||
|
||||
export default defineRemoteExecMiddleware({
|
||||
onRequest({ context, fetchOptions }) {
|
||||
// Attach auth token from context to request headers
|
||||
if (context.authToken) {
|
||||
fetchOptions.headers.set("Authorization", `Bearer ${context.authToken}`);
|
||||
}
|
||||
},
|
||||
async onResponse({ operationName, response, context }) {
|
||||
// Save auth token in user session
|
||||
if (operationName === "AuthLogin") {
|
||||
const { data } = await response.json() as { data?: AuthLoginMutation };
|
||||
if (data) {
|
||||
await context.handleLogin(data);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user