可以通过设置Apollo Client的fetch
选项来实现在浏览器上存储cookie。代码示例如下:
import { ApolloClient, HttpLink, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
cache: new InMemoryCache(),
link: new HttpLink({
uri: 'https://graphql.example.com',
credentials: 'include', // 设置为'include',表示允许跨域请求时携带cookie
fetch: (uri, options) => { // 自定义fetch方法
options.headers = {
...options.headers,
Cookie: document.cookie // 从document中获取cookie,并添加到请求头中
};
return fetch(uri, options);
},
}),
});
在这个示例中,我们通过credentials
选项将允许浏览器跨域请求时携带cookie。然后通过自定义的fetch
方法,在请求头中添加了从document
中获取的cookie。这样就可以在浏览器上存储cookie了。