node.js - Graphql 连接错误 - 仅获取查询或订阅或两者
问题描述
我们在将应用程序设置到 graphql 连接时遇到错误。我们的主要需求是订阅,目前我们的代码可以很好地处理这些。但是我们的查询以“在 query_root 中找不到潜在客户”作为响应
当我们修改连接代码的位置时,查询现在可以工作,但订阅停止连接。
const scheme = proto => {
return window.location.protocol === 'https:' ? `${proto}s` : proto;
};
const wsurl = `${scheme('ws')}://${HASURA_GRAPHQL_ENGINE_HOSTNAME}/v1/graphql`;
const httpurl = `${scheme('http')}://${HASURA_GRAPHQL_ENGINE_HOSTNAME}/v1/graphql`;
// Create a WebSocket link:
const wsLink = new WebSocketLink({
uri: wsurl,
options: {
reconnect: true,
connectionParams: {
headers: {
'x-hasura-access-key': `${HASURA_ACCESS_KEY}`
}
}
}
});
const httpLink = new HttpLink({
uri: httpurl,
options: {
connectionParams: {
headers: {
'x-hasura-access-key': `${HASURA_ACCESS_KEY}`
}
}
}
});
const link = split(
// split based on operation type
({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
},
wsLink,
httpLink
);
const client = new ApolloClient({
link,
cache: new InMemoryCache()
});
export default client;
在链接 const 中,如果我们将 wsLink 放在 httpLink 下方,则 wsLink 停止并且 httpLink 工作。
解决方案
推荐阅读
- python - python:瑞利拟合(直方图)
- r - 使用 lapply() 在 R 中生成 igraph 属性
- sql - 在Oracle中将一列的数据显示为一行
- templates - 如何将调度程序和触发器添加到 Azure devops 管道中的嵌套模板?
- javascript - 如何在 VueJS 中来回传递数据
- javascript - 拒绝显示...在一个框架中,因为它在 React 和 oAuth 中将“X-Frame-Options”设置为“同源”
- powershell - 可重用的自定义排序
- nginx - Nginx 配置将任何 url 作为根 url 提供服务
- kubernetes - 将节点添加到 kubernetes 集群会导致无法加载 Kubelet 配置文件 /var/lib/kubelet/config.yaml 并且在 /etc/cni/net.d 中找不到网络
- android - 为什么会出现异常:无法在 onCreateViewHolder 收集类 java.lang.NoSuchMethodException 的必要方法