突变后如何重新查询?

时间:2018-10-14 07:17:56

标签: reactjs graphql apollo prisma

我正在尝试为帖子添加书签和取消收藏。突变取决于bookmarkId是否已经存在。新书签效果很好,然后取消书签效果很好,然后如果我尝试再次对同一帖子添加书签,则会收到错误消息:Error: GraphQL error: No Node for the model Bookmark with value cjn8j3ka0ktwl0b77katcw8eu for id found.我试图重新获取所有帖子FEED_QUERY,但仍然无法正常工作。我的按钮的突变:

   <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,
            })
          }}
        >

这里是repo

0 个答案:

没有答案