如何使用ApolloClient参数解决错误?

时间:2020-06-30 10:01:11

标签: javascript reactjs authentication apollo apollo-client

我遇到了apollo-client问题,尤其是Apollo客户参数。

类型'TokenRefreshLink'无法分配给类型 “ ApolloLink”。 [1]属性“ split”的类型不兼容。

const authLink = new TokenRefreshLink({
  accessTokenField: "accessToken",
  isTokenValidOrUndefined: () => {
    const token = getAccessToken();

    if (!token) {
      return true;
    }

    try {
      const { exp } = jwtDecode(token);
      if (Date.now() >= exp * 1000) {
        return false;
      } else {
        return true;
      }
    } catch {
      return false;
    }
  },
  fetchAccessToken: () => {
    return fetch("http://localhost:4000/refresh_token", {
      method: "POST",
      credentials: "include"
    });
  },
  handleFetch: accessToken => {
    setAccessToken(accessToken);
  },
  handleError: err => {
    console.warn("Your refresh token is invalid. Try to relogin");
    console.error(err);
  }
})

const client = new ApolloClient({
  link: ApolloLink.from([
    authLink,
    onError(({ graphQLErrors, networkError }) => {
      console.log(graphQLErrors);
      console.log(networkError);
    }),
    requestLink,
    new HttpLink({
      uri: "/graphql",
      credentials: "include"
    })
  ]),
  cache
});

如果我从客户端删除authLink,则没有错误(也没有身份验证)。 这就是我的目的:

const client = new ApolloClient({
  link: ApolloLink.from([
   onError(({ graphQLErrors, networkError }) => {
      console.log(graphQLErrors);
      console.log(networkError);
    }),
    requestLink,
    new HttpLink({
      uri: "/graphql",
      credentials: "include"
    })
  ]),
  cache

});

在某些以前的依赖版本中,它可以工作,但是实际上我不确定应该降级哪些依赖。如何解决?

0 个答案:

没有答案