Commit 73924ec4 by qiuzhi99

error-handler

1 parent 4c47e536
......@@ -7,6 +7,7 @@
"apollo-boost": "^0.4.4",
"apollo-cache-inmemory": "^1.6.3",
"apollo-link-context": "^1.0.19",
"apollo-link-error": "^1.1.12",
"apollo-link-http": "^1.5.16",
"graphql": "^14.5.8",
"graphql-tag": "^2.10.1",
......
......@@ -5,6 +5,7 @@ import { ApolloProvider } from "@apollo/react-hooks";
import { createHttpLink } from "apollo-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import { setContext } from "apollo-link-context";
import { onError } from "apollo-link-error";
const httpLink = createHttpLink({ uri: "http://localhost:5001" });
......@@ -17,8 +18,23 @@ const authLink = setContext(() => {
};
});
const errorLink = onError(({ graphQLErrors, operation, forward }) => {
if (graphQLErrors) {
for (let err of graphQLErrors) {
switch (err.extensions.code) {
case "UNAUTHENTICATED":
window.location.href = "/login";
// retry the request, returning the new observable
return forward(operation);
}
}
}
});
const link = errorLink.concat(authLink);
const client = new ApolloClient({
link: authLink.concat(httpLink),
link: link.concat(httpLink),
cache: new InMemoryCache()
});
......
......@@ -1797,7 +1797,7 @@ apollo-link-context@^1.0.19:
apollo-link "^1.2.13"
tslib "^1.9.3"
apollo-link-error@^1.0.3:
apollo-link-error@^1.0.3, apollo-link-error@^1.1.12:
version "1.1.12"
resolved "https://registry.npm.taobao.org/apollo-link-error/download/apollo-link-error-1.1.12.tgz#e24487bb3c30af0654047611cda87038afbacbf9"
integrity sha1-4kSHuzwwrwZUBHYRzahwOK+6y/k=
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!