用阿波罗服务器订阅时GraphQL订阅异常

发布于 2025-02-07 16:02:37 字数 1126 浏览 1 评论 0原文

GraphQL订阅抛出异常“ Web套接字连接失败” 使用Apollo服务器订阅时。订阅与GraphQl Explorer合作正常,但是在扑面

代码中实现时失败,下面附加了异常。

**********代码

final _wsLink = WebSocketLink(    
  ‘***************’,    
  config: SocketClientConfig(
    autoReconnect: true,
    inactivityTimeout: Duration(hours: 1),
    // delayBetweenReconnectionAttempts: Duration(seconds: 1),
  ),  
); 

final Link _link = Link.split((request) => request.isSubscription, _wsLink, httpLink);
     
client = GraphQLClient(     
  cache: GraphQLCache(store: InMemoryStore()),
  link: _link,  
);

SubscriptionOptions options = SubscriptionOptions(
  document: gql(query),
  variables: variables);

final result = client.subscribe(options);

result.listen((event) {
  print("Event fired ${event.data}");
  print("Event fired ${event.exception?.graphqlErrors.toString()}");
}).onError((e) {
  print("Received Error ! ${e.toString()}");
});

return result;

***************************************************************************************************************

i/flutter(5204):与Websocket断开连接。

GraphQl subscription throwing exception "web socket connect failed" when subscribing with Apollo server. Subscription is working fine with graphQl explorer but failing when implemented in flutter

Code and exceptions are attached below.

********** Code ***********

final _wsLink = WebSocketLink(    
  ‘***************’,    
  config: SocketClientConfig(
    autoReconnect: true,
    inactivityTimeout: Duration(hours: 1),
    // delayBetweenReconnectionAttempts: Duration(seconds: 1),
  ),  
); 

final Link _link = Link.split((request) => request.isSubscription, _wsLink, httpLink);
     
client = GraphQLClient(     
  cache: GraphQLCache(store: InMemoryStore()),
  link: _link,  
);

SubscriptionOptions options = SubscriptionOptions(
  document: gql(query),
  variables: variables);

final result = client.subscribe(options);

result.listen((event) {
  print("Event fired ${event.data}");
  print("Event fired ${event.exception?.graphqlErrors.toString()}");
}).onError((e) {
  print("Received Error ! ${e.toString()}");
});

return result;

************ Exception ***************

I/flutter ( 5204): Disconnected from websocket.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉梦枕江山 2025-02-14 16:02:37

我迟到了,但这对我有用,如果您有阿波罗服务器4

final HttpLink httpLink = HttpLink(
    'http://192.168.43.29:4000/graphql', // my localhhost
  );
  
   final AuthLink authLink = AuthLink(
      getToken: () async => 'Bearer $token',
    );
    Link link = authLink.concat(httpLink);

    WebSocketLink websocketLink = WebSocketLink(
      'ws://192.168.43.29:4000/graphql/subscriptions', // my localhhost
      config: SocketClientConfig(
          autoReconnect: true,
          inactivityTimeout: const Duration(seconds: 30),
          initialPayload: {
            'token': token,
          },
          ),
      subProtocol: GraphQLProtocol.graphqlTransportWs, // This line is important, by default protocol is GraphQLProtocol.graphqlWs
    );

    if (websocketLink != null) {
      link =
          Link.split((request) => request.isSubscription, websocketLink, link);
    }

    graphqlClient = ValueNotifier(
      GraphQLClient(
        link: link,
        cache: GraphQLCache(store: HiveStore()),
      ),
    );

I'm late but this works for me, if you have Apollo Server 4

final HttpLink httpLink = HttpLink(
    'http://192.168.43.29:4000/graphql', // my localhhost
  );
  
   final AuthLink authLink = AuthLink(
      getToken: () async => 'Bearer $token',
    );
    Link link = authLink.concat(httpLink);

    WebSocketLink websocketLink = WebSocketLink(
      'ws://192.168.43.29:4000/graphql/subscriptions', // my localhhost
      config: SocketClientConfig(
          autoReconnect: true,
          inactivityTimeout: const Duration(seconds: 30),
          initialPayload: {
            'token': token,
          },
          ),
      subProtocol: GraphQLProtocol.graphqlTransportWs, // This line is important, by default protocol is GraphQLProtocol.graphqlWs
    );

    if (websocketLink != null) {
      link =
          Link.split((request) => request.isSubscription, websocketLink, link);
    }

    graphqlClient = ValueNotifier(
      GraphQLClient(
        link: link,
        cache: GraphQLCache(store: HiveStore()),
      ),
    );

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文