Skip to content

Commit

Permalink
Merge branch 'canary' into i18n/redirect-default-locale
Browse files Browse the repository at this point in the history
  • Loading branch information
kodiakhq[bot] authored Oct 8, 2020
2 parents 552fc6d + a79bcfb commit cab80be
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions examples/with-apollo/components/Submit.js
Original file line number Diff line number Diff line change
@@ -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!) {
Expand All @@ -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,
})
},
})
Expand Down

0 comments on commit cab80be

Please sign in to comment.