我使用的是用Nextjs和Apollo构建的应用程序。我在Graphql响应标头中收到一个令牌。我可以在服务器端读取此令牌,并且需要将其传递给客户端的Apollo Provider。
cache.writeData({
data: {
isLoggedIn: false
}
});
const afterwareLink = new ApolloLink((operation, forward) => {
return forward(operation).map(response => {
const context = operation.getContext();
const {
response: { headers },
} = context;
if (headers) {
const authorization = headers.get('authorization');
if (authorization) {
console.log(authorization);
// what to do next? I need somehow set isLoggedIn to true...
}
}
return response;
});
});
const link = ApolloLink.from([
afterwareLink,
new RetryLink(),
httpLink
]);
const client = new ApolloClient({
link,
cache
});
function App({ children }){
return (
<ApolloProvider client={client}>
{children}
</ApolloProvider>
);
}
我尝试在de ApolloLink中设置上下文:
operation.setContext({ isLoggedIn: true });
由于页面是在服务器端呈现的,因此我无法直接在缓存上写内容。