Based on the official docs Apollo's cache identify method is used to get the id of the cache item because this ID could be custom and composed from different fields. That's clear.
To do it we need to receive an item from the cache to pass this item into the cache.identify and receive this item ID.
To get a cache item we have the next possibilities, based on the docs:
const READ_TODO = gql`
query ReadTodo($id: ID!) {
todo(id: $id) {
id
text
completed
}
}
`;
// Fetch the cached to-do item with ID 5
const { todo } = client.readQuery({
query: READ_TODO,
variables: {
id: 5,
},
});
const todo = client.readFragment({
id: 'Todo:5', // The value of the to-do item's cache ID
fragment: gql`
fragment MyTodo on Todo {
id
text
completed
}
`,
});
These methods will return an item from the cache that we further could pass into the cache.identify to receive the ID.
But they require the ID so it must be known at the moment of the execution of any of them.
So I'm wondering what is the best practice to use the cache.identify ?
So far I know, probably, it could be the only 1 use-case.
Obtain the possible cache item id in the update callback that could be used as an option for a mutation that only updates an existing item. The backend could return a response with the modified object and it's possible to pass this object into the cache.identify to get the ID and pass it further into the appropriated cache update method. But I'm not sure if I'm right so would like to clarify it with more experienced Apollo developers.
Is this the only 1 purpose for the cache.identify method?
Thanks for any help!