2020-02-18 07:57:00 +00:00
|
|
|
import Vue from "vue";
|
2021-06-11 12:21:27 +00:00
|
|
|
import VueApollo from "@vue/apollo-option";
|
2021-05-12 16:10:07 +00:00
|
|
|
import { onError } from "@apollo/client/link/error";
|
|
|
|
import { createLink } from "apollo-absinthe-upload-link";
|
2020-02-18 07:57:00 +00:00
|
|
|
import {
|
2021-05-12 16:10:07 +00:00
|
|
|
ApolloClient,
|
|
|
|
ApolloLink,
|
2020-02-18 07:57:00 +00:00
|
|
|
defaultDataIdFromObject,
|
2021-07-27 17:55:17 +00:00
|
|
|
fromPromise,
|
2020-02-18 07:57:00 +00:00
|
|
|
InMemoryCache,
|
|
|
|
NormalizedCacheObject,
|
2021-05-12 16:10:07 +00:00
|
|
|
split,
|
|
|
|
} from "@apollo/client/core";
|
2021-08-13 15:47:26 +00:00
|
|
|
import { RetryLink } from "@apollo/client/link/retry";
|
2020-02-18 07:57:00 +00:00
|
|
|
import buildCurrentUserResolver from "@/apollo/user";
|
|
|
|
import { AUTH_ACCESS_TOKEN } from "@/constants";
|
|
|
|
import { logout } from "@/utils/auth";
|
|
|
|
import { Socket as PhoenixSocket } from "phoenix";
|
|
|
|
import * as AbsintheSocket from "@absinthe/socket";
|
|
|
|
import { createAbsintheSocketLink } from "@absinthe/socket-apollo-link";
|
2021-05-12 16:10:07 +00:00
|
|
|
import { getMainDefinition } from "@apollo/client/utilities";
|
2020-12-02 10:19:39 +00:00
|
|
|
import fetch from "unfetch";
|
2020-02-18 07:57:00 +00:00
|
|
|
import { GRAPHQL_API_ENDPOINT, GRAPHQL_API_FULL_PATH } from "./api/_entrypoint";
|
2021-05-12 16:10:07 +00:00
|
|
|
import {
|
|
|
|
possibleTypes,
|
|
|
|
typePolicies,
|
|
|
|
refreshAccessToken,
|
|
|
|
} from "./apollo/utils";
|
2021-06-18 16:42:54 +00:00
|
|
|
import { GraphQLError } from "graphql";
|
2018-11-06 09:30:27 +00:00
|
|
|
|
|
|
|
// Install the vue plugin
|
|
|
|
Vue.use(VueApollo);
|
|
|
|
|
2021-07-27 17:55:17 +00:00
|
|
|
let isRefreshing = false;
|
|
|
|
let pendingRequests: any[] = [];
|
2020-02-18 07:57:00 +00:00
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2021-08-13 15:47:26 +00: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 07:57:00 +00:00
|
|
|
const uploadLink = createLink({
|
|
|
|
uri: httpEndpoint,
|
2021-08-13 15:47:26 +00:00
|
|
|
fetch: customFetch,
|
2020-02-18 07:57:00 +00: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 09:24:11 +00:00
|
|
|
return (
|
|
|
|
definition.kind === "OperationDefinition" &&
|
|
|
|
definition.operation === "subscription"
|
|
|
|
);
|
2020-02-18 07:57:00 +00:00
|
|
|
},
|
|
|
|
wsLink,
|
|
|
|
uploadLink
|
|
|
|
);
|
|
|
|
|
2021-07-27 17:55:17 +00:00
|
|
|
const resolvePendingRequests = () => {
|
|
|
|
pendingRequests.map((callback) => callback());
|
|
|
|
pendingRequests = [];
|
|
|
|
};
|
|
|
|
|
2021-08-13 15:47:26 +00: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 09:24:11 +00:00
|
|
|
const errorLink = onError(
|
|
|
|
({ graphQLErrors, networkError, forward, operation }) => {
|
2021-08-13 15:47:26 +00: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 17:55:17 +00:00
|
|
|
let forwardOperation;
|
|
|
|
|
|
|
|
if (!isRefreshing) {
|
2021-08-13 15:47:26 +00:00
|
|
|
console.debug("Setting isRefreshing to true");
|
2021-07-27 17:55:17 +00:00
|
|
|
isRefreshing = true;
|
|
|
|
|
|
|
|
forwardOperation = fromPromise(
|
|
|
|
refreshAccessToken(apolloClient)
|
2021-08-13 15:47:26 +00:00
|
|
|
.then((res) => {
|
|
|
|
if (res !== true) {
|
|
|
|
// failed to refresh the token
|
|
|
|
throw "Failed to refresh the token";
|
|
|
|
}
|
2021-07-27 17:55:17 +00:00
|
|
|
resolvePendingRequests();
|
|
|
|
|
|
|
|
const context = operation.getContext();
|
|
|
|
const oldHeaders = context.headers;
|
|
|
|
|
|
|
|
operation.setContext({
|
|
|
|
headers: {
|
|
|
|
...oldHeaders,
|
|
|
|
authorization: generateTokenHeader(),
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return true;
|
|
|
|
})
|
2021-08-13 15:47:26 +00:00
|
|
|
.catch((e) => {
|
|
|
|
console.debug("Something failed, let's logout", e);
|
2021-07-27 17:55:17 +00:00
|
|
|
pendingRequests = [];
|
2021-08-13 15:47:26 +00:00
|
|
|
// don't perform a logout since we don't have any working access/refresh tokens
|
|
|
|
logout(apolloClient, false);
|
2021-07-27 17:55:17 +00: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 09:24:11 +00:00
|
|
|
}
|
2019-08-12 14:04:16 +00:00
|
|
|
|
2020-11-30 09:24:11 +00:00
|
|
|
if (graphQLErrors) {
|
2021-06-18 16:42:54 +00: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 09:24:11 +00:00
|
|
|
);
|
|
|
|
}
|
2019-08-12 14:04:16 +00:00
|
|
|
|
2020-11-30 09:24:11 +00:00
|
|
|
if (networkError) {
|
2021-04-12 08:42:45 +00:00
|
|
|
console.error(`[Network error]: ${networkError}`);
|
2020-11-30 09:24:11 +00:00
|
|
|
}
|
2019-10-13 11:56:24 +00:00
|
|
|
}
|
2020-11-30 09:24:11 +00:00
|
|
|
);
|
2018-11-06 09:30:27 +00:00
|
|
|
|
2021-08-13 15:47:26 +00:00
|
|
|
const retryLink = new RetryLink();
|
|
|
|
|
|
|
|
const fullLink = authMiddleware
|
|
|
|
.concat(retryLink)
|
|
|
|
.concat(errorLink)
|
|
|
|
.concat(link);
|
2019-08-12 14:04:16 +00:00
|
|
|
|
2019-09-09 09:21:42 +00:00
|
|
|
const cache = new InMemoryCache({
|
2021-06-04 18:24:43 +00:00
|
|
|
addTypename: true,
|
2021-05-12 16:10:07 +00:00
|
|
|
typePolicies,
|
|
|
|
possibleTypes,
|
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
|
|
|
|
);
|
2020-10-01 13:07:15 +00:00
|
|
|
console.error(error);
|
2019-01-18 13:47:10 +00:00
|
|
|
},
|
|
|
|
});
|