Apollo GraphQL订阅在Vuejs中不起作用 [英] Apollo GraphQL subscriptions is not working in Vuejs

查看:283
本文介绍了Apollo GraphQL订阅在Vuejs中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有Prisma和vuejs + apollo客户端的GraphQL订阅

I am using GraphQL Subscription with Prisma and vuejs + apollo client

在本地系统中,我正在 http://localhost:8080 上运行vuejs,并在

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服务器.除订阅外,其他所有内容均正常运行.我设置了websocket,它也可以正常工作.

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/

I am following below docuemnt https://www.apollographql.com/docs/react/data/subscriptions/

我尝试了不同的方法,但是没有成功.连接稳定/断开连接后重新连接.

I have a tried a different ways but didn't get success. The connection is stable/ reconnects after disconnecting itself.

在服务器端,我添加了用于Web套接字的侦听器,并且该侦听器正在连接.

At server side, I added listener for web socket and it's connecting.

这是我的vue-apollo.js文件代码

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请求.

Finally I got issue. Actually I was using classical load balancer and classical load balancer is not supporting websocket requests.

我配置了应用程序负载平衡器并附加到我的服务器实例.现在工作正常.

I configured application load balancer and attached to my server instance. It's working proper now.

我的代码正确.

这篇关于Apollo GraphQL订阅在Vuejs中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆