Hi, after executing a query getItems
, it returns an array of Items
which consist of an _id
and title
, and this appears in my cache as such:
items = [{_id:"someID", title:"someTitle"},{_id:"someID", title:"someTitle"},...]
I have a mutation to add an item:
const ADD_ITEM = gql`
mutation AddItem($title: String!) {
addItem(title: $title) {
_id
title
}
}
`
And a corresponding useMutation
hook:
const [addItem] = useMutation(ADD_ITEM, {
update(cache, { data: { addItem } }) {
const { _id, title } = addItem
cache.modify({
fields: {
getItems(existingItems = []){
const newItemRef = cache.writeFragment({
fragment: gql`
fragment NewItem on Item {
_id
title
}
`,
data: {
_id,
title
}
})
return [...existingItems, newItemRef]
}
}
})
}
})
Now, my question is, how do I update this array by pushing the returned data into it?
From what I understand from the docs, the data
field is supposed to identify the query, but I’m not sure how I’m supposed to identify the cached query. When I inspect the cache query using the Apollo Dev Tools, I don’t see any unique ID or anything of that sort associated with it.
I tried identifying it with data: { "userID" : Meteor.userId() }
(returns the matching userID), but this doesn’t work either.
How should I be approaching this?