首页 > 解决方案 > Apollo 客户端订阅在一段时间后未获取

问题描述

我有这个奇怪的错误,我无法缝合解决。我有一个反应原生应用程序,我在我的 android 平板电脑上使用。

我希望能够让应用程序永远打开并保持与服务器的连接,这样我就可以获得应用程序的订阅。但即使应用程序/平板电脑打开,它也会在夜间以某种方式失去连接。

然后在早上,我可以在查询和突变中恢复连接,但订阅挂钩不再起作用。除非我重新加载应用程序。任何想法为什么会这样?

我知道后端在应该发送数据的时候。但是该应用程序并没有在 useSubscription 挂钩中恢复它。

这是我的client.js文件 =>

const cache = new InMemoryCache({
    typePolicies: {
      Subscription: {
        fields: {
          orderUpdatedByRestaurantId: {
            // Merges together incoming and existing cache ref
            merge(existing = [], incoming: any) {
              const newEntries = incoming.filter(
                (n) =>
                  !existing.some(
                    (y) => JSON.stringify(y) === JSON.stringify(n),
                  ) && n,
              );
              return [...existing, ...newEntries];
            },
          },
        },
      },
    },
  });
  const httpLink = createHttpLink({
    uri: GRAPHQL_ENDPOINT,
    credentials: true,
  });

  const client = new SubscriptionClient(SUBSCRIPTION_ENDPOINT, {
    reconnect: true,
  });

  const wsLink = new WebSocketLink(client);

  client.onConnected(() => {
    console.log('connected');
    Sentry.captureMessage(
      `Subscription client connected at ${dayjs().format()}`,
    );
  });

  client.onDisconnected(() => {
    console.log('disconnected');
    Sentry.captureMessage(
      `Subscription client disconnected at ${dayjs().format()}`,
    );
    setTimeout(() => {
      client.connect();
    }, 300000);
  });

  const mainLink = split(
    ({ query }) => {
      const { kind, operation } = getMainDefinition(query);
      return (
        kind === 'OperationDefinition' && operation === 'subscription'
      );
    },
    wsLink,
    httpLink,
  );

标签: react-nativewebsocketapollo-clientapollo-server

解决方案


推荐阅读