I'm trying to bookmark and unbookmark a post. The ...
# orm-help
k
I'm trying to bookmark and unbookmark a post. The mutation depends on whether bookmarkId already exist or not. new bookmark works well, then unbookmark works well then if I try to bookmark the same post again, I get an error:
Error: GraphQL error: No Node for the model Bookmark with value cjn8j3ka0ktwl0b77katcw8eu for id found.
I tried to refetch FEED_QUERY and ME_QUERY but It still doesn't work. I guess this would be something similar as adding a downwote feature to
howtographql
tutorial. I just can't make this work. My Button's Mutation:
Copy code
<Mutation
          mutation={bookmarkId ? UN_BOOKMARK_MUTATION : BOOKMARK_MUTATION}
          variables={variables}
          // refetchQueries={[{ query: FEED_QUERY }]}
          update={(cache, payload) => {
            const data = cache.readQuery({
              query: ME_QUERY,
            })
            if (payload.data.createBookmark) {
              const { createBookmark } = payload.data
              data.me.bookmarks = [...data.me.bookmarks, createBookmark]
            }
            if (
              payload.data.deleteBookmark &&
              payload.data.deleteBookmark.id
            ) {
              filledIcon = false
              const { deleteBookmark } = payload.data
              console.log('deleteBookmark', deleteBookmark)
              data.me.bookmarks = data.me.bookmarks.filter(
                bm => bm.id !== deleteBookmark.id,
              )
            }
            console.log('data after', data)
            cache.writeQuery({
              query: ME_QUERY,
              data,
            })
          }}
        >
Here's the repo https://github.com/kstulgys/blog-demo-app/blob/master/src/components/Post/CreateBookmark.js
n
Sounds like an issue in your Apollo Cache
k
Thanks for your reply. How do I properly change the cache to toggle bookmark?
figured this out already 🙂