2020-02-18 08:57:00 +01:00
|
|
|
import Vue from "vue";
|
2021-06-11 14:21:27 +02:00
|
|
|
import VueApollo from "@vue/apollo-option";
|
2021-05-12 18:10:07 +02:00
|
|
|
import { onError } from "@apollo/client/link/error";
|
|
|
|
import { createLink } from "apollo-absinthe-upload-link";
|
2020-02-18 08:57:00 +01:00
|
|
|
import {
|
2021-05-12 18:10:07 +02:00
|
|
|
ApolloClient,
|
|
|
|
ApolloLink,
|
2020-02-18 08:57:00 +01:00
|
|
|
defaultDataIdFromObject,
|
2021-07-27 19:55:17 +02:00
|
|
|
fromPromise,
|
2020-02-18 08:57:00 +01:00
|
|
|
InMemoryCache,
|
|
|
|
NormalizedCacheObject,
|
2021-05-12 18:10:07 +02:00
|
|
|
split,
|
|
|
|
} from "@apollo/client/core";
|
2021-08-13 17:47:26 +02:00
|
|
|
import { RetryLink } from "@apollo/client/link/retry";
|
2020-02-18 08:57:00 +01:00
|
|
|
import { Socket as PhoenixSocket } from "phoenix";
|
|
|
|
import * as AbsintheSocket from "@absinthe/socket";
|
|
|
|
import { createAbsintheSocketLink } from "@absinthe/socket-apollo-link";
|
2021-05-12 18:10:07 +02:00
|
|
|
import { getMainDefinition } from "@apollo/client/utilities";
|
2020-12-02 11:19:39 +01:00
|
|
|
import fetch from "unfetch";
|
2021-09-29 20:24:55 +02:00
|
|
|
import buildCurrentUserResolver from "@/apollo/user";
|
|
|
|
import { AUTH_ACCESS_TOKEN } from "@/constants";
|
|
|
|
import { logout } from "@/utils/auth";
|
2020-02-18 08:57:00 +01:00
|
|
|
import { GRAPHQL_API_ENDPOINT, GRAPHQL_API_FULL_PATH } from "./api/_entrypoint";
|
2021-05-12 18:10:07 +02:00
|
|
|
import {
|
|
|
|
possibleTypes,
|
|
|
|
typePolicies,
|
|
|
|
refreshAccessToken,
|
|
|
|
} from "./apollo/utils";
|
2021-06-18 18:42:54 +02:00
|
|
|
import { GraphQLError } from "graphql";
|
2018-11-06 10:30:27 +01:00
|
|
|
|
|
|
|
// Install the vue plugin
|
|
|
|
Vue.use(VueApollo);
|
|
|
|
|
2021-07-27 19:55:17 +02:00
|
|
|
let isRefreshing = false;
|
|
|
|
let pendingRequests: any[] = [];
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2019-12-03 11:29:51 +01:00
|
|
|
// Endpoints
|
2020-02-18 08:57:00 +01:00
|
|
|
const httpServer = GRAPHQL_API_ENDPOINT || "http://localhost:4000";
|
2018-11-15 17:35:47 +01:00
|
|
|
const httpEndpoint = GRAPHQL_API_FULL_PATH || `${httpServer}/api`;
|
2020-02-18 08:57:00 +01:00
|
|
|
const webSocketPrefix = process.env.NODE_ENV === "production" ? "wss" : "ws";
|
|
|
|
const wsEndpoint = `${webSocketPrefix}${httpServer.substring(
|
|
|
|
httpServer.indexOf(":")
|
|
|
|
)}/graphql_socket`;
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
function generateTokenHeader() {
|
|
|
|
const token = localStorage.getItem(AUTH_ACCESS_TOKEN);
|
|
|
|
|
|
|
|
return token ? `Bearer ${token}` : null;
|
|
|
|
}
|
2018-11-06 10:30:27 +01:00
|
|
|
|
|
|
|
const authMiddleware = new ApolloLink((operation, forward) => {
|
|
|
|
// add the authorization to the headers
|
|
|
|
operation.setContext({
|
|
|
|
headers: {
|
2019-08-12 16:04:16 +02:00
|
|
|
authorization: generateTokenHeader(),
|
2018-11-06 10:30:27 +01:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-12-28 15:41:32 +01:00
|
|
|
if (forward) return forward(operation);
|
2018-12-21 15:41:34 +01:00
|
|
|
|
|
|
|
return null;
|
2018-11-06 10:30:27 +01:00
|
|
|
});
|
|
|
|
|
2021-08-13 17:47:26 +02:00
|
|
|
const customFetch = async (uri: string, options: any) => {
|
|
|
|
const response = await fetch(uri, options);
|
|
|
|
if (response.status >= 400) {
|
|
|
|
return Promise.reject(response.status);
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
};
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
const uploadLink = createLink({
|
|
|
|
uri: httpEndpoint,
|
2021-08-13 17:47:26 +02:00
|
|
|
fetch: customFetch,
|
2020-02-18 08:57:00 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2020-11-30 10:24:11 +01:00
|
|
|
return (
|
|
|
|
definition.kind === "OperationDefinition" &&
|
|
|
|
definition.operation === "subscription"
|
|
|
|
);
|
2020-02-18 08:57:00 +01:00
|
|
|
},
|
|
|
|
wsLink,
|
|
|
|
uploadLink
|
|
|
|
);
|
|
|
|
|
2021-07-27 19:55:17 +02:00
|
|
|
const resolvePendingRequests = () => {
|
|
|
|
pendingRequests.map((callback) => callback());
|
|
|
|
pendingRequests = [];
|
|
|
|
};
|
|
|
|
|
2021-08-13 17:47:26 +02:00
|
|
|
const isAuthError = (graphQLError: GraphQLError | undefined) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
return graphQLError && [403, 401].includes(graphQLError.status_code);
|
|
|
|
};
|
|
|
|
|
2020-11-30 10:24:11 +01:00
|
|
|
const errorLink = onError(
|
|
|
|
({ graphQLErrors, networkError, forward, operation }) => {
|
2021-08-13 17:47:26 +02:00
|
|
|
console.debug("We have an apollo error", [graphQLErrors, networkError]);
|
|
|
|
if (
|
|
|
|
graphQLErrors?.some((graphQLError) => isAuthError(graphQLError)) ||
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
networkError === 401
|
|
|
|
) {
|
|
|
|
console.debug("It's a authorization error (statusCode 401)");
|
2021-07-27 19:55:17 +02:00
|
|
|
let forwardOperation;
|
|
|
|
|
|
|
|
if (!isRefreshing) {
|
2021-08-13 17:47:26 +02:00
|
|
|
console.debug("Setting isRefreshing to true");
|
2021-07-27 19:55:17 +02:00
|
|
|
isRefreshing = true;
|
|
|
|
|
|
|
|
forwardOperation = fromPromise(
|
|
|
|
refreshAccessToken(apolloClient)
|
2021-08-13 17:47:26 +02:00
|
|
|
.then((res) => {
|
|
|
|
if (res !== true) {
|
|
|
|
// failed to refresh the token
|
|
|
|
throw "Failed to refresh the token";
|
|
|
|
}
|
2021-07-27 19:55:17 +02:00
|
|
|
resolvePendingRequests();
|
|
|
|
|
|
|
|
const context = operation.getContext();
|
|
|
|
const oldHeaders = context.headers;
|
|
|
|
|
|
|
|
operation.setContext({
|
|
|
|
headers: {
|
|
|
|
...oldHeaders,
|
|
|
|
authorization: generateTokenHeader(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
})
|
2021-08-13 17:47:26 +02:00
|
|
|
.catch((e) => {
|
|
|
|
console.debug("Something failed, let's logout", e);
|
2021-07-27 19:55:17 +02:00
|
|
|
pendingRequests = [];
|
2021-08-13 17:47:26 +02:00
|
|
|
// don't perform a logout since we don't have any working access/refresh tokens
|
|
|
|
logout(apolloClient, false);
|
2021-07-27 19:55:17 +02:00
|
|
|
return;
|
|
|
|
})
|
|
|
|
.finally(() => {
|
|
|
|
isRefreshing = false;
|
|
|
|
})
|
|
|
|
).filter((value) => Boolean(value));
|
|
|
|
} else {
|
|
|
|
forwardOperation = fromPromise(
|
|
|
|
new Promise((resolve) => {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
pendingRequests.push(() => resolve());
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return forwardOperation.flatMap(() => forward(operation));
|
2020-11-30 10:24:11 +01:00
|
|
|
}
|
2019-08-12 16:04:16 +02:00
|
|
|
|
2020-11-30 10:24:11 +01:00
|
|
|
if (graphQLErrors) {
|
2021-06-18 18:42:54 +02:00
|
|
|
graphQLErrors.map(
|
|
|
|
(graphQLError: GraphQLError & { status_code?: number }) => {
|
|
|
|
if (graphQLError?.status_code !== 401) {
|
|
|
|
console.log(
|
|
|
|
`[GraphQL error]: Message: ${graphQLError.message}, Location: ${graphQLError.locations}, Path: ${graphQLError.path}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-11-30 10:24:11 +01:00
|
|
|
);
|
|
|
|
}
|
2019-08-12 16:04:16 +02:00
|
|
|
|
2020-11-30 10:24:11 +01:00
|
|
|
if (networkError) {
|
2021-04-12 10:42:45 +02:00
|
|
|
console.error(`[Network error]: ${networkError}`);
|
2020-11-30 10:24:11 +01:00
|
|
|
}
|
2019-10-13 13:56:24 +02:00
|
|
|
}
|
2020-11-30 10:24:11 +01:00
|
|
|
);
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2021-08-13 17:47:26 +02:00
|
|
|
const retryLink = new RetryLink();
|
|
|
|
|
|
|
|
const fullLink = authMiddleware
|
|
|
|
.concat(retryLink)
|
|
|
|
.concat(errorLink)
|
|
|
|
.concat(link);
|
2019-08-12 16:04:16 +02:00
|
|
|
|
2019-09-09 11:21:42 +02:00
|
|
|
const cache = new InMemoryCache({
|
2021-06-04 20:24:43 +02:00
|
|
|
addTypename: true,
|
2021-05-12 18:10:07 +02:00
|
|
|
typePolicies,
|
|
|
|
possibleTypes,
|
2020-02-18 08:57:00 +01:00
|
|
|
dataIdFromObject: (object: any) => {
|
|
|
|
if (object.__typename === "Address") {
|
2019-11-08 19:37:14 +01:00
|
|
|
return object.origin_id;
|
|
|
|
}
|
|
|
|
return defaultDataIdFromObject(object);
|
|
|
|
},
|
2019-09-09 11:21:42 +02:00
|
|
|
});
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
const apolloClient = new ApolloClient<NormalizedCacheObject>({
|
2018-12-21 15:41:34 +01:00
|
|
|
cache,
|
2019-12-03 11:29:51 +01:00
|
|
|
link: fullLink,
|
2018-12-21 15:41:34 +01:00
|
|
|
connectToDevTools: true,
|
2019-09-02 14:35:50 +02:00
|
|
|
resolvers: buildCurrentUserResolver(cache),
|
2019-01-18 14:47:10 +01:00
|
|
|
});
|
2018-11-06 10:30:27 +01:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
export default new VueApollo({
|
2019-01-18 14:47:10 +01:00
|
|
|
defaultClient: apolloClient,
|
|
|
|
errorHandler(error) {
|
2018-11-06 10:30:27 +01:00
|
|
|
// eslint-disable-next-line no-console
|
2020-02-18 08:57:00 +01:00
|
|
|
console.log(
|
|
|
|
"%cError",
|
|
|
|
"background: red; color: white; padding: 2px 4px; border-radius: 3px; font-weight: bold;",
|
|
|
|
error.message
|
|
|
|
);
|
2020-10-01 15:07:15 +02:00
|
|
|
console.error(error);
|
2019-01-18 14:47:10 +01:00
|
|
|
},
|
|
|
|
});
|