How to update cache on mutation for subscription?

Viewed 180

I have subscription which shows the liked list with a list of posts. But the subscription takes few secs after the mutation. I am not managing local state ATM.

So what is the best way, manage in local state? or update cache for subscription?

function LikeButton() {
  const [insertLike, { loading }] = useMutation(INSERT_LIKE);
  function likePost() {
    insertLike({
      variables: {
        object: {
          user_id: owner,
          post_id: id,
        },
      },
      update: (cache, { data: { addItem } }) => {
        // Not sure how to do this :(
      }
    })
  }
    return (
        <button
            onClick={ownLiked ? unlikePost : likePost}
            disabled={likeLoading || unLikeLoading}
            className={likeLoading || unLikeLoading ? 'cursor-not-allowed' : ''}
        >
            Like
        </button>
    )
}

// useSubscription being called in the parent level
const GET_POSTS_WITH_OUT_USER = gql`
  subscription getItems {
    posts(limit: 10, order_by: {created_at: desc}) {
      id
      content
      date: created_at
      user {
        username: display_name
        id
        image: avatar_url
      }
      likedCount: posts_likes_aggregate {
        aggregate {
          count
        }
      }
      comments {
        content
        id
        date: created_at
        user {
          username: display_name
          id
          image: avatar_url
        }
      }
    }
  }
`
0 Answers
Related