DEV Community

John Ding
John Ding

Posted on

Use cache.modify to update appolo graphql client


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

Top comments (1)

Collapse
 
thomasbnt profile image
Thomas Bnt ☕

Hello ! Don't hesitate to put colors on your codeblock like this example for have to have a better understanding of your code 😎

console.log('Hello world!');
Enter fullscreen mode Exit fullscreen mode

Example of how to add colors and syntax in codeblocks