我实际上遇到了同样的问题,发现它与我的服务器端口如何启动以及我如何在我的 FE 上配置我的 Apollo Provider 有关。注意“ws”不起作用,它也需要“wss”才能为我工作。我没有像您使用“/subscriptions”那样将任何内容定向到特定的 url。相反,我只是使用 Heroku/localhost 提供给我的任何 URI
服务器 GraphQLServer 代码:
const server = new GraphQLServer({
typeDefs,
resolvers,
context: { pubsub, endpoint: "/" },
});
服务器启动代码:
server.start(({ port }) => {
console.log(`Server listening on ${port} for incoming requests`);
});
FE Apollo 提供者配置
const httpLink = new HttpLink({
uri: "https://domainName.herokuapp.com,
});
const wsLink = new WebSocketLink({
uri: "wss://domainName.herokuapp.com/",
options: {
reconnect: true,
},
});
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === "OperationDefinition" &&
definition.operation === "subscription"
);
},
wsLink,
httpLink
);
const client = new ApolloClient({
link: splitLink,
uri: "https://domainName.herokuapp.com",
cache: new InMemoryCache({
addTypename: false,
}),
});
希望这可以帮助您并让您继续前进。花了半天时间弄清楚自己。我认为这对您来说可能是相同的情况,只是您的情况略有不同。编码愉快!!!