【发布时间】:2019-10-11 16:45:14
【问题描述】:
我正在使用 setupProxy 为我的 react 应用程序设置代理服务器,该应用程序使用 graphql 作为在不同部分运行的后端,这样做 HTTP 链接代理工作正常,但 WebSocket 链接代理给我一个错误
为了解决问题,我尝试将选项包括为 ws: true,但它不起作用。 错误如下: SyntaxError: 无法构造 'WebSocket': URL '/ws' 无效。
错误:
setupProxy.js
const proxy = require("http-proxy-middleware");
module.exports = function(app) {
app.use(proxy("/graphql", { target: "http://localhost:8001/graphql" }));
app.use(
proxy("/ws", {
target: "ws://localhost:8001",
ws: true,
})
);
};
index.js
import { WebSocketLink } from "apollo-link-ws";
import { createUploadLink } from "apollo-upload-client";
//Apollo Imports End
// Declaring constants for GraphQL
const httpLink = new createUploadLink({
uri: "/graphql"
});
const wsLink = new WebSocketLink({
uri: "/ws",
options: {
reconnect: true
}
});
我预计调用应该与正常调用相同,但它会引发错误。
【问题讨论】:
-
您找到解决方案了吗?我也遇到了同样的问题
-
还没有。 @安德鲁
-
您是否尝试在创建 WebSocketLink 时向 URI 添加主机名?即
uri: "localhost/ws"
标签: node.js reactjs graphql create-react-app react-apollo