From 26f48172ee9f7d92715555244ccf24aac3de6773 Mon Sep 17 00:00:00 2001 From: handtrix Date: Wed, 7 Oct 2020 15:17:08 +0200 Subject: [PATCH] Fix with-apollo example --- examples/with-apollo/components/Submit.js | 28 ++++++++++++----------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/examples/with-apollo/components/Submit.js b/examples/with-apollo/components/Submit.js index a276050ad51671f..8aa1d13265fc7e1 100644 --- a/examples/with-apollo/components/Submit.js +++ b/examples/with-apollo/components/Submit.js @@ -1,5 +1,4 @@ import { gql, useMutation } from '@apollo/client' -import { ALL_POSTS_QUERY, allPostsQueryVars } from './PostList' const CREATE_POST_MUTATION = gql` mutation createPost($title: String!, $url: String!) { @@ -26,19 +25,22 @@ export default function Submit() { createPost({ variables: { title, url }, - update: (proxy, { data: { createPost } }) => { - const data = proxy.readQuery({ - query: ALL_POSTS_QUERY, - variables: allPostsQueryVars, - }) - // Update the cache with the new post at the top of the list - proxy.writeQuery({ - query: ALL_POSTS_QUERY, - data: { - ...data, - allPosts: [createPost, ...data.allPosts], + update: (cache, { data: { createPost } }) => { + cache.modify({ + fields: { + allPosts(existingPosts = []) { + const newPostRef = cache.writeFragment({ + data: createPost, + fragment: gql` + fragment NewPost on allPosts { + id + type + } + `, + }) + return [newPostRef, ...existingPosts] + }, }, - variables: allPostsQueryVars, }) }, })