2020-02-18 07:57:00 +00:00
|
|
|
import Vue from "vue";
|
|
|
|
import VueApollo from "vue-apollo";
|
|
|
|
import { ApolloLink, Observable, split } from "apollo-link";
|
|
|
|
import {
|
|
|
|
defaultDataIdFromObject,
|
|
|
|
InMemoryCache,
|
|
|
|
NormalizedCacheObject,
|
|
|
|
} from "apollo-cache-inmemory";
|
|
|
|
import { onError } from "apollo-link-error";
|
|
|
|
import { createLink } from "apollo-absinthe-upload-link";
|
|
|
|
import { ApolloClient } from "apollo-client";
|
|
|
|
import buildCurrentUserResolver from "@/apollo/user";
|
|
|
|
import { isServerError } from "@/types/apollo";
|
|
|
|
import { AUTH_ACCESS_TOKEN } from "@/constants";
|
|
|
|
import { logout } from "@/utils/auth";
|
|
|
|
import { SnackbarProgrammatic as Snackbar } from "buefy";
|
|
|
|
import { Socket as PhoenixSocket } from "phoenix";
|
|
|
|
import * as AbsintheSocket from "@absinthe/socket";
|
|
|
|
import { createAbsintheSocketLink } from "@absinthe/socket-apollo-link";
|
|
|
|
import { getMainDefinition } from "apollo-utilities";
|
|
|
|
import { GRAPHQL_API_ENDPOINT, GRAPHQL_API_FULL_PATH } from "./api/_entrypoint";
|
2020-09-29 07:53:48 +00:00
|
|
|
import { fragmentMatcher, refreshAccessToken } from "./apollo/utils";
|
2018-11-06 09:30:27 +00:00
|
|
|
|
|
|
|
// Install the vue plugin
|
|
|
|
Vue.use(VueApollo);
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
let refreshingTokenPromise: Promise<boolean> | undefined;
|
|
|
|
let alreadyRefreshedToken = false;
|
|
|
|
|
2019-12-03 10:29:51 +00:00
|
|
|
// Endpoints
|
2020-02-18 07:57:00 +00:00
|
|
|
const httpServer = GRAPHQL_API_ENDPOINT || "http://localhost:4000";
|
2018-11-15 16:35:47 +00:00
|
|
|
const httpEndpoint = GRAPHQL_API_FULL_PATH || `${httpServer}/api`;
|
2020-02-18 07:57:00 +00:00
|
|
|
const webSocketPrefix = process.env.NODE_ENV === "production" ? "wss" : "ws";
|
|
|
|
const wsEndpoint = `${webSocketPrefix}${httpServer.substring(
|
|
|
|
httpServer.indexOf(":")
|
|
|
|
)}/graphql_socket`;
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
function generateTokenHeader() {
|
|
|
|
const token = localStorage.getItem(AUTH_ACCESS_TOKEN);
|
|
|
|
|
|
|
|
return token ? `Bearer ${token}` : null;
|
|
|
|
}
|
2018-11-06 09:30:27 +00:00
|
|
|
|
|
|
|
const authMiddleware = new ApolloLink((operation, forward) => {
|
|
|
|
// add the authorization to the headers
|
|
|
|
operation.setContext({
|
|
|
|
headers: {
|
2019-08-12 14:04:16 +00:00
|
|
|
authorization: generateTokenHeader(),
|
2018-11-06 09:30:27 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-12-28 14:41:32 +00:00
|
|
|
if (forward) return forward(operation);
|
2018-12-21 14:41:34 +00:00
|
|
|
|
|
|
|
return null;
|
2018-11-06 09:30:27 +00:00
|
|
|
});
|
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
const uploadLink = createLink({
|
|
|
|
uri: httpEndpoint,
|
|
|
|
});
|
|
|
|
|
|
|
|
const phoenixSocket = new PhoenixSocket(wsEndpoint, {
|
|
|
|
params: () => {
|
|
|
|
const token = localStorage.getItem(AUTH_ACCESS_TOKEN);
|
|
|
|
if (token) {
|
|
|
|
return { token };
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const absintheSocket = AbsintheSocket.create(phoenixSocket);
|
|
|
|
const wsLink = createAbsintheSocketLink(absintheSocket);
|
|
|
|
|
|
|
|
const link = split(
|
|
|
|
// split based on operation type
|
|
|
|
({ query }) => {
|
|
|
|
const definition = getMainDefinition(query);
|
|
|
|
return definition.kind === "OperationDefinition" && definition.operation === "subscription";
|
|
|
|
},
|
|
|
|
wsLink,
|
|
|
|
uploadLink
|
|
|
|
);
|
|
|
|
|
2019-08-12 14:04:16 +00:00
|
|
|
const errorLink = onError(({ graphQLErrors, networkError, forward, operation }) => {
|
|
|
|
if (isServerError(networkError) && networkError.statusCode === 401 && !alreadyRefreshedToken) {
|
2020-02-18 07:57:00 +00:00
|
|
|
if (!refreshingTokenPromise) refreshingTokenPromise = refreshAccessToken(apolloClient);
|
2019-08-12 14:04:16 +00:00
|
|
|
|
|
|
|
return promiseToObservable(refreshingTokenPromise).flatMap(() => {
|
|
|
|
refreshingTokenPromise = undefined;
|
|
|
|
alreadyRefreshedToken = true;
|
|
|
|
|
|
|
|
const context = operation.getContext();
|
|
|
|
const oldHeaders = context.headers;
|
|
|
|
|
|
|
|
operation.setContext({
|
|
|
|
headers: {
|
|
|
|
...oldHeaders,
|
|
|
|
authorization: generateTokenHeader(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
return forward(operation);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (graphQLErrors) {
|
2020-09-29 07:53:48 +00:00
|
|
|
graphQLErrors.map(({ message, locations, path }) =>
|
|
|
|
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`)
|
|
|
|
);
|
2019-08-12 14:04:16 +00:00
|
|
|
}
|
|
|
|
|
2019-10-13 11:56:24 +00:00
|
|
|
if (networkError) {
|
|
|
|
console.log(`[Network error]: ${networkError}`);
|
2020-09-29 07:53:48 +00:00
|
|
|
Snackbar.open({
|
|
|
|
message: "Please refresh the page and retry.",
|
|
|
|
type: "is-danger",
|
|
|
|
position: "is-bottom",
|
|
|
|
});
|
2019-10-13 11:56:24 +00:00
|
|
|
}
|
2019-01-18 13:47:10 +00:00
|
|
|
});
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
const fullLink = authMiddleware.concat(errorLink).concat(link);
|
2019-08-12 14:04:16 +00:00
|
|
|
|
2019-09-09 09:21:42 +00:00
|
|
|
const cache = new InMemoryCache({
|
|
|
|
fragmentMatcher,
|
2020-02-18 07:57:00 +00:00
|
|
|
dataIdFromObject: (object: any) => {
|
|
|
|
if (object.__typename === "Address") {
|
2019-11-08 18:37:14 +00:00
|
|
|
return object.origin_id;
|
|
|
|
}
|
|
|
|
return defaultDataIdFromObject(object);
|
|
|
|
},
|
2019-09-09 09:21:42 +00:00
|
|
|
});
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
const apolloClient = new ApolloClient<NormalizedCacheObject>({
|
2018-12-21 14:41:34 +00:00
|
|
|
cache,
|
2019-12-03 10:29:51 +00:00
|
|
|
link: fullLink,
|
2018-12-21 14:41:34 +00:00
|
|
|
connectToDevTools: true,
|
2019-09-02 12:35:50 +00:00
|
|
|
resolvers: buildCurrentUserResolver(cache),
|
2019-01-18 13:47:10 +00:00
|
|
|
});
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2020-02-18 07:57:00 +00:00
|
|
|
export default new VueApollo({
|
2019-01-18 13:47:10 +00:00
|
|
|
defaultClient: apolloClient,
|
|
|
|
errorHandler(error) {
|
2018-11-06 09:30:27 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2020-02-18 07:57:00 +00:00
|
|
|
console.log(
|
|
|
|
"%cError",
|
|
|
|
"background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;",
|
|
|
|
error.message
|
|
|
|
);
|
2019-01-18 13:47:10 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2019-08-12 14:04:16 +00:00
|
|
|
// Thanks: https://github.com/apollographql/apollo-link/issues/747#issuecomment-502676676
|
2020-02-18 07:57:00 +00:00
|
|
|
const promiseToObservable = <T>(promise: Promise<T>) =>
|
|
|
|
new Observable<T>((subscriber) => {
|
2019-08-12 14:04:16 +00:00
|
|
|
promise.then(
|
|
|
|
(value) => {
|
|
|
|
if (subscriber.closed) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
subscriber.next(value);
|
|
|
|
subscriber.complete();
|
|
|
|
},
|
|
|
|
(err) => {
|
2020-02-18 07:57:00 +00:00
|
|
|
console.error("Cannot refresh token.", err);
|
2019-08-12 14:04:16 +00:00
|
|
|
|
|
|
|
subscriber.error(err);
|
|
|
|
logout(apolloClient);
|
2020-02-18 07:57:00 +00:00
|
|
|
}
|
2019-08-12 14:04:16 +00:00
|
|
|
);
|
|
|
|
});
|
2020-02-18 07:57:00 +00:00
|
|
|
|
|
|
|
// Manually call this when user log in
|
|
|
|
// export function onLogin(apolloClient) {
|
|
|
|
// if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient);
|
|
|
|
// }
|
|
|
|
|
|
|
|
// Manually call this when user log out
|
|
|
|
// export async function onLogout() {
|
|
|
|
// if (apolloClient.wsClient) restartWebsockets(apolloClient.wsClient);
|
|
|
|
// We don't reset store because we rely on currentUser & currentActor
|
|
|
|
// which are in the cache (even null). Maybe try to rerun cache init after resetStore?
|
|
|
|
// try {
|
|
|
|
// await apolloClient.resetStore();
|
|
|
|
// } catch (e) {
|
|
|
|
// // eslint-disable-next-line no-console
|
|
|
|
// console.log('%cError on cache reset (logout)', 'color: orange;', e.message);
|
|
|
|
// }
|
|
|
|
// }
|