【问题标题】:can't send authToken in Apolloclient in react无法在 Apolloclient 中发送 authToken 以做出反应
【发布时间】:2019-10-07 17:34:25
【问题描述】:

我需要将 authToken 与 appolloclient 中的 uri 一起发送。 我试过这样做,但它似乎没有发送自动令牌。

const client = new ApolloClient({
  uri: 'some url',
  request: async operation => {
   const token = 'MvHE4SXPDa17yzh6Shswhsvwhswdwvd';
   operation.setContext({
     headers: {
       authorization: token
     }
   });
  }
})

我也试过这种方法

const client = new ApolloClient({
  uri: 'some url,
     headers: {
       authorization: token
     }
   });

使用 apollo 客户端发送标头的正确程序是什么?

【问题讨论】:

    标签: reactjs graphql apollo


    【解决方案1】:

    看看这个

    const authLink = setContext(async (_, { headers }) => { // get the authentication token from local storage if it exists const token = await getToken();
    // return the headers to the context so httpLink can read them return { headers: { ...headers, authorization: token, }, }; }); const newLink = authLink.concat(resetUser); const client = new ApolloClient({ link: newLink.concat(link), // link, cache: new InMemoryCache({ addTypename: false, }).restore(), });

    【讨论】:

      猜你喜欢
      • 2021-10-24
      • 2022-10-20
      • 2020-12-26
      • 1970-01-01
      • 1970-01-01
      • 2021-05-16
      • 1970-01-01
      • 2020-11-27
      • 2021-12-30
      相关资源
      最近更新 更多