【发布时间】:2020-10-14 21:23:36
【问题描述】:
所以我试图根据他们网站上的文档将授权标头传递给 Apollo。
但是,当我尝试在我的服务器上登录 request.headers 时,我看不到授权属性。接下来,我通过在其中添加console.log() 来检查函数setContext() 是否正在运行,发现它没有运行。
const httpLink = new HttpLink({ uri: 'http://localhost:4000' });
const authLink = setContext((request, { headers }) => {
const token = localStorage.getItem('library-user-token')
console.log(token) //doesn't log at all
return ({
headers: {
...headers,
authorization: token ? `Bearer ${token}` : null,
}
})
});
const client = new ApolloClient({
link: authLink.concat(httpLink),
cache: new InMemoryCache()
});
【问题讨论】:
-
我的项目中有几乎相同的代码,并且可以正常工作。您是否将客户传递给您的
ApolloProvider? -
SDKFSISDFIKOSFO。非常感谢老兄,那是我没有做的事
-
酷。乐意效劳。我添加了我的评论作为答案。 :)
标签: apollo apollo-client apollo-server