101 lines
3.2 KiB
JavaScript
101 lines
3.2 KiB
JavaScript
import {ApolloClient, ApolloLink, fromPromise, InMemoryCache} from '@apollo/client/core'
|
|
import {onError} from "@apollo/client/link/error";
|
|
import {useUserStore} from "stores/User";
|
|
import {useAppStore} from "stores/App";
|
|
import {Notify} from "quasar";
|
|
import {errorObject, warningObject} from "src/composables/dialog";
|
|
import {createUploadLink} from "apollo-upload-client";
|
|
// import createUploadLink from "apollo-upload-client/createUploadLink.mjs";
|
|
|
|
const handleRefreshToken = (store, router, forward, operation) => {
|
|
if (!operation.query?._refreshUnauthorizedUser) return;
|
|
|
|
console.log("refreshing token, ", operation.query);
|
|
return fromPromise(useUserStore(store).refreshAccessToken()
|
|
.catch((error) => {
|
|
Notify.create(warningObject("Tvoje přihlášení vypřešlo. Přihlaš se prosím znovu."));
|
|
router.push({name: "login"})
|
|
}))
|
|
.filter((value) => Boolean(value))
|
|
.flatMap((accessToken) => {
|
|
const oldHeaders = operation.getContext().headers;
|
|
operation.setContext({
|
|
headers: {
|
|
...oldHeaders,
|
|
authorization: `Bearer ${accessToken}`,
|
|
},
|
|
});
|
|
|
|
// retry the request, returning the new observable
|
|
return forward(operation);
|
|
});
|
|
}
|
|
|
|
export const apolloWrapper = {};
|
|
export default (({store, router}) => {
|
|
const uploadLink = createUploadLink({
|
|
uri: process.env.API_URL + '/graphql',
|
|
credentials: 'include'
|
|
})
|
|
|
|
const authLink = new ApolloLink((operation, forward) => {
|
|
const accessToken = useUserStore(store).getAccessToken();
|
|
|
|
if (accessToken) {
|
|
operation.setContext({
|
|
headers: {"Authorization": `Bearer ${accessToken}`},
|
|
});
|
|
}
|
|
return forward(operation);
|
|
});
|
|
|
|
const errorLink = onError(({graphQLErrors, networkError, operation, forward}) => {
|
|
console.log(networkError, graphQLErrors);
|
|
if (networkError?.statusCode === 401) {
|
|
return handleRefreshToken(store, router, forward, operation);
|
|
}
|
|
|
|
if (graphQLErrors) {
|
|
for (const error of graphQLErrors) {
|
|
if (error.message === "Not found") {
|
|
useAppStore(store).appStatus = 404;
|
|
} else if (error.message.includes("401") || error.message.includes("Not authorized")) {
|
|
return handleRefreshToken(store, router, forward, operation);
|
|
} else {
|
|
Notify.create(errorObject("API error: " + error.message));
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
const apolloClient = new ApolloClient({
|
|
link: ApolloLink.from([authLink, errorLink, uploadLink]), cache: new InMemoryCache(), connectToDevTools: true,
|
|
});
|
|
|
|
apolloWrapper.query = (query, variables = {}) => {
|
|
return apolloClient.query({
|
|
query: query,
|
|
fetchPolicy: 'no-cache',
|
|
variables: variables
|
|
})
|
|
.then((response) => response)
|
|
.catch(err => {
|
|
if (!err.toString().includes("401") && !err.toString().includes("Not authorized"))
|
|
console.error("Error during query: ", err)
|
|
return err;
|
|
})
|
|
};
|
|
apolloWrapper.mutate = (mutation, variables) => {
|
|
return apolloClient
|
|
.mutate({
|
|
mutation: mutation,
|
|
fetchPolicy: 'no-cache',
|
|
variables: variables,
|
|
})
|
|
.then((response) => response)
|
|
.catch(err => {
|
|
console.error("Error during mutation: ", err);
|
|
})
|
|
};
|
|
});
|