无法在Apolloclient中发送authToken

时间:2019-05-21 10:29:14

标签: reactjs graphql apollo

我需要在appolloclient中将authToken和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客户端发送标头的正确过程是什么?

1 个答案:

答案 0 :(得分:0)

检查一下

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(), });