apollosubscriptionsettingwithsubscribeToMorequery-updateQuerynotrespectingkeyargs
创始人
2024-09-09 13:30:15
0

当使用 Apollo 的 subscription 监听数据变化时,使用了 subscribeToMore 方法,但可能会出现 updateQuery 方法没有正确传递 key args 参数的问题。

解决方法是在 updateQuery 方法中手动传递 key args 参数,如下所示:

const subscription = gql`
  subscription {
    newPost {
      id
      content
    }
  }
`;

const POSTS_QUERY = gql`
  query getPosts($postId: Int!) {
    posts(where: {id: {_eq: $postId}}) {
      id
      content
    }
  }
`;

const SUBSCRIBE_TO_NEW_POSTS = gql`
  subscription {
    newPost {
      id
      content
    }
  }
`;

const Post = ({ post }) => (
  
{post.content}
); const Posts = ({ postId }) => { const { data, loading, subscribeToMore } = useQuery(POSTS_QUERY, { variables: { postId }, }); useEffect(() => { const unsubscribe = subscribeToMore({ document: SUBSCRIBE_TO_NEW_POSTS, updateQuery: (prev, { subscriptionData }) => { if (!subscriptionData.data) return prev; const newPost = subscriptionData.data.newPost; // Use `postId` variable to filter and update the correct post. // Otherwise, every post in the cache will be updated. const update = { posts: [ ...(prev.posts || []), ...(post.id === newPost.id ? [newPost] : []), ], }; return update; }, variables: { postId }, // <-- Manually pass the `postId` variable. }); return unsubscribe; }, [postId, subscribeToMore]); if (loading) return

Loading...

; const post = data.posts[0]; return (
); };

在上面的示例中,需要手动传递 postId 变量到 updateQuery 函数中。

这会告诉 Apollo 在缓存中找到与当前 postId 匹配的 post,并更新它,而不是更新所有的 post。

相关内容

热门资讯

安装程序教程(aa扑克模拟器)... 安装程序教程(aa扑克模拟器)外挂透明挂辅助插件(辅助挂)软件透明挂(有挂教程)-哔哩哔哩;aa扑克...
实测分享(Wepoke漏洞)外... 实测分享(Wepoke漏洞)外挂透明挂辅助器安装(辅助挂)透视辅助(有挂头条)-哔哩哔哩;(需添加指...
推荐攻略(德扑之星真的太假)外... 推荐攻略(德扑之星真的太假)外挂透明挂辅助挂(透视)的确真的有挂(2023已更新)(哔哩哔哩);亲真...
热门推荐!德扑之星底牌(wep... 热门推荐!德扑之星底牌(wepOkE)外挂透明挂辅助神器(透视)AA德州教程(确实有挂)-哔哩哔哩;...
玩家必备科普(WPK线上)外挂... 《玩家必备科普(WPK线上)外挂透明挂辅助器(辅助挂)辅助透视(有挂工具)-哔哩哔哩》 WPK线上软...
玩家实测(微扑克wpk)外挂透... 玩家实测(微扑克wpk)外挂透明挂辅助软件(透视)发牌规律(有挂技术)-哔哩哔哩是一款可以让一直输的...
总算了解(德州ai人工智能)外... 总算了解(德州ai人工智能)外挂透明挂辅助工具(辅助挂)确实真的有挂(2022已更新)(哔哩哔哩);...
揭秘关于!wpk透视辅助(WE... 揭秘关于!wpk透视辅助(WEPoke)外挂透明挂辅助app(透视)教你教程(有挂存在)-哔哩哔哩需...
透视真的(wpk发牌)外挂透明... 1、透视真的(wpk发牌)外挂透明挂辅助软件(辅助挂)透视辅助(有挂透明挂)-哔哩哔哩(UU pok...
今日头条(微扑克软件)外挂透明... WePoker透视辅助版本稳定性对比与推荐‌:今日头条(微扑克软件)外挂透明挂辅助APP(辅助挂)发...