所以我知道我可以使用 ctx.req.headers.cookie 访问我的 cookie 服务器端,但我不确定如何使用 Apollo Client 将该 cookie 发送到我的 GraphQL 端点。
有什么帮助吗?谢谢。
我可以建议的最简单的方法是使用Apollo HTTP Link's context(https://www.apollographql.com/docs/link/links/http/#passing-context-per-query)
如果您监控网站上常见的 HTTP 请求,您可能会注意到cookie请求标头中设置了标头,代表发送的 cookie。
因此,您可以send cookies通过cookie在 Apollo HTTP 客户端的标头中设置相同的内容来访问 Apollo
import {
ApolloClient,
InMemoryCache,
gql,
createHttpLink
} from "@apollo/client";
const client = new ApolloClient({
link: createHttpLink({ uri: "https://myserver.com" })
cache: new InMemoryCache()
});
// Pass the cookie header into context when using ApolloClient
const cookie = 'mycookie1=myvalue1; mycookie2=myvalue2';
client.query({
fetchPolicy: "cache-first",
context: {
headers: {
cookie
}
},
query: gql``
});
Run Code Online (Sandbox Code Playgroud)
或者使用低级 Apollo Link:https : //www.apollographql.com/docs/react/api/link/apollo-link-context/#overview
| 归档时间: |
|
| 查看次数: |
737 次 |
| 最近记录: |