【问题标题】:Apollo useSubscription doesn't work for new windowsApollo useSubscription 不适用于新窗口
【发布时间】:2020-09-28 04:51:18
【问题描述】:

这是我第一次使用 useSub,我注意到尽管我的后端将它的响应发送给客户端(至少 console.log 显示它是)。使用 useSub 的客户端不做任何事情。我通常将 subscribeToMore 与查询一起使用,但对于这项工作,我只想获得最新的信息。有没有办法检查 useSub 是否正确连接?还是在 "@apollo/react-hooks": "^3.1.3"

查询

export const INCOMING_VIDEO_CHAT = gql`
  subscription {
    incomingVideoChat {
      rn
      p
    }
  }
`;

客户

const { data, loading } = useSubscription(INCOMING\_VIDEO\_CHAT, {
onSubscriptionData: ({ subscriptionData }) => {
console.log(subscriptionData);
}
});

服务器:

module.exports = {
type: chatInfoType,
subscribe: () => pubsub.asyncIterator(INCOMING\_VIDEO\_CHAT),
async resolve(payload, { }, req) {
if (auth.isAuthenticated(req)) {
if (!payload) {
return;
}
const { userID, rn, p } = payload;
  try {
    if (req.id === userID) {
      return { rn, p };
    } else {
      return;
    }
  } catch (e) {
    throw new Error(e);
  }
}
}
};

【问题讨论】:

    标签: reactjs apollo react-apollo apollo-client graphql-subscriptions


    【解决方案1】:

    您可以使用 Chrome 开发工具检查 useSubscription 是否正确连接。在 Chrome DevTools 的 Network 选项卡中,您应该将过滤器切换到 WS 以查看和调试您的 Apollo GraphQL 订阅。你应该阅读it

    【讨论】:

      猜你喜欢
      • 2020-07-24
      • 2020-08-13
      • 2019-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-26
      相关资源
      最近更新 更多