I'm working with the Apollo client cache and trying to follow their official docs but after I played with it a little I found a weird behavior regarding the query arguments.
In the docs we can see that the writeQuery contains variables with the id prop to correctly select the appropriated item from the cache regarding this id to update it with the new data. It looks clear but when I tried to change or even remove the variables the writeQuery continued to work correctly. It looks like the only id and __typename in the data matter because the cache uses them to determine what kind of data it should update.
Here is the code example:
client.writeQuery({
query: gql`
query WriteTrack($trackId: ID!) {
track(id: $trackId) {
id
title
author {
id
name
photo
}
thumbnail
description
},
}`,
data: { // Contains the data to write
track: {
__typename: 'Track',
id: 'c_1',
title: 'Buy grapes ',
thumbnail: 'https://res.cloudinary.com/dety84pbu/image/upload/famous_cats_epuqcr.jpg',
description: "Be inspired by famous catstronauts",
author: {
"__typename": "Author",
"id": "cat-2",
"name": "Grumpy Cat UPDATED",
"photo": "https://images.unsplash.com/photo-d34828365da7"
}
},
},
variables: {
id: 'c_1' // The id is presented
}
})
And the second variant with the removed variables at all. Still works!
client.writeQuery({
query: gql`
query WriteTrack {
track {
id
title
author {
id
name
photo
}
thumbnail
description
},
}`,
data: { // Contains the data to write
track: {
__typename: 'Track',
id: 'c_1',
title: 'Buy grapes ',
thumbnail: 'https://res.cloudinary.com/dety84pbu/image/upload/famous_cats_epuqcr.jpg',
description: "Be inspired by famous catstronauts",
author: {
"__typename": "Author",
"id": "cat-2",
"name": "Grumpy Cat UPDATED",
"photo": "https://images.unsplash.com/photo-d34828365da7"
},
},
// I removed the variables and it continues to work fine
}
})
So my question is: is this behavior applicable for all the writeQuery use cases and I could omit the variables at all or is it works only in my particular use case somehow?
But why?
And why is this behavior inconsistent with the official documentation or did I just miss something?
Thanks for any help!