reactjs - React JS 与 Apollo GraphQL 订阅
问题描述
嗨,我正在尝试使用 auth 标头在 apollo 中实现订阅。我一直遇到出现 CORS 错误的问题。
似乎没有办法做到这一点。
这是我对您的代码所做的修改的精简版:
const httpLink = new HttpLink({
uri: "http://localhost:4444/graphql",
fetchOptions: {
credentials: "include"
},
request: operation => {
const token = localStorage.getItem("token");
operation.setContext({
headers: {
Authorization: "Bearer " + token
}
});
},
onError: ({ networkError }) => {
if (networkError) {
console.log("Network Error:::", networkError);
}
}
});
const wsLink = new WebSocketLink({
uri: ws://localhost:4444/subscriptions,
options: {
reconnect: true
}
});
const link = split(
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === "OperationDefinition" && operation === "subscription";
},
wsLink,
httpLink
);
*注意我正在尝试使用 auth 来实现
解决方案
推荐阅读
- tensorflow - 带有 Dropout 层的 Keras 小批量梯度下降
- python-3.x - 从 pandas 列中删除小数(字符串类型)
- swift - SKTileMap 不显示任何图块
- react-native - 安装反应导航 5.x 后,应用程序一直崩溃
- python - 使用日期范围替换 nan 日期
- powershell - 为什么 powershell 脚本可以在 ISE 和批处理文件中工作,而不是作为任务或服务?
- git - 天蓝色触发不同的分支推送
- pipenv - 为什么当我更改哈希时 pipenv 不抱怨?
- angular - 如何在不推入角度 6 的情况下存储多行值?
- c# - 仅更改所选 div 的类