问题描述
我正在使用带有 Prisma 和 vuejs + apollo 客户端的 GraphQL 订阅
I am using GraphQL Subscription with Prisma and vuejs + apollo client
在本地系统中,我在 http://localhost:8080 和服务器上运行 vuejs http://localhost:4000.
In local system, I am running vuejs at http://localhost:8080 and server at http://localhost:4000.
我想在仪表板中显示最近添加和更新的记录.
I want to display recently added and updated records in dashboard.
我已在本地系统中实现订阅,并且运行正常.
I have implemented subscription in my local system and it's working proper.
我将所有服务器端和客户端代码推送到服务器,但订阅在那里不起作用.
I push all server side and client side code to server but subscription is not working there.
我正在使用 AWS 服务器.除订阅外,一切正常.我设置了 websockets,它也正常工作.
I am using AWS server. Everything is working proper except subscription. I set up websockets and it's also working proper.
有时我会遇到以下错误:WebSocket 与 'wss://URL:4000/graphql' 的连接失败:在建立连接之前 WebSocket 已关闭
Sometime I am getting below error:WebSocket connection to 'wss://URL:4000/graphql' failed: WebSocket is closed before the connection is established
我正在关注以下文档https://www.apollographql.com/docs/react/data/subscriptions/
我尝试了不同的方法,但没有成功.连接稳定/自行断开后重新连接.
At server side, I added listener for web socket and it's connecting.
This is my code of vue-apollo.js file
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import { ApolloClient } from 'apollo-client';
import { HttpLink } from 'apollo-link-http';
import { InMemoryCache } from 'apollo-cache-inmemory';
// imports for subscription
import { split } from 'apollo-link';
import { WebSocketLink } from 'apollo-link-ws';
import { getMainDefinition } from 'apollo-utilities';
const uriHttp = process.env.VUE_APP_SERVER_URL;
const uriWs = process.env.VUE_APP_WS_SERVER_URL;
const headers = { authorization: localStorage.getItem('token') };
const httpLink = new HttpLink({ uri: uriHttp, headers });
const wsLink = new WebSocketLink({
uri: uriWs,
options: {
reconnect: true,
connectionParams() {
return { headers };
},
},
});
wsLink.subscriptionClient.on('connecting', () => {
console.log('connecting');
});
wsLink.subscriptionClient.on('connected', () => {
console.log('connected');
});
wsLink.subscriptionClient.on('reconnecting', () => {
console.log('reconnecting');
});
wsLink.subscriptionClient.on('reconnected', () => {
console.log('reconnected');
});
wsLink.subscriptionClient.on('disconnected', () => {
console.log('disconnected');
});
wsLink.subscriptionClient.maxConnectTimeGenerator.duration = () => wsLink.subscriptionClient.maxConnectTimeGenerator.max;
const link = split(({ query }) => {
const { kind, operation } = getMainDefinition(query);
return kind === 'OperationDefinition' && operation === 'subscription';
}, wsLink, httpLink);
export const defaultClient = new ApolloClient({
link,
cache: new InMemoryCache(),
connectToDevTools: true,
});
const apolloProvider = new VueApollo({
defaultClient,
defaultOptions: {
$loadingKey: 'loading',
},
});
Vue.use(VueApollo);
export default apolloProvider;
推荐答案
最后,我明白了这个问题.实际上,我使用的是不支持 WebSocket 请求的 Classical load balancer
.
我配置了应用程序负载平衡器并将其附加到我的服务器实例.它现在工作正常.
这篇关于Apollo GraphQL 订阅在 Vuejs 中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!