It seems like all the useMutation update examples change an already existing fragment or query in the cache. I’d like to use useMutation’s update parameter to add a completely new fragment to the cache, but when I try to use writeFragment or even writeQuery to add to the cache, nothing changes. Any help would be much appreciated.
here’s some pseudo code that explains how I’m going about it:
const [createBook] = useMutation(CREATE_BOOK)
const book = {
id: "1234",
__typename: "Book",
name: "The Guns of August",
author: "Barbara W. Tuchman"
}
createBook({
variables: {
book
},
optimisticResponse: {
createBook: {
...book,
},
},
update: (cache, { data: { createBook } }) => {
const bookId = "Book:" + createBook._id
// doesn't work
cache.writeQuery({
query: GET_BOOK,
data: {
book: {
...createBook,
},
},
variables: {
_id: createBook._id,
},
})
// neither does this
cache.writeFragment({
fragment: gql`
fragment BookFragment on Book {
_id
name
author
}
`,
id: bookId,
data: {
...createBook,
},
})
}
})
also, I’m logging cache.data.data
to look for my updates.