Use cache.modify to update appolo graphql client

John Ding - Aug 9 '22 - - Dev Community

update(cache, { data }) {
      cache.modify({
        fields: {
          getCommentsByPostId(existingComments = []) {
            if (data.comment) {
              return [data.comment, ...existingComments]
            }
            return existingComments
          },
        },
      })
      cache.modify({
        // find the ID
        id: cache.identify({ id: data.comment.postId, __typename: 'Post' }),
        // update the fields you need to
        fields: {
          commentCount(oldCount, { readField, toReference }) {
            return oldCount + 1
          },
        },
      })
    },

Enter fullscreen mode Exit fullscreen mode
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .