我需要在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客户端发送标头的正确过程是什么?
答案 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(),
});