It's really simple to do. Again, as we saw, all those objects are just in this hash. We can easily grab them by their identifier, and we can merge these new fields in. And this is the one that will happen the most often.
But the second one is if you had a list or a collection of entities, and you returned all of them with all of their identifiers, and all the fields that need to be outdated. So this doesn't work if you return, let's say, some of them, or just one of them. You have to return the whole collection back in order to update that for each of them. So talk about when it doesn't work, because these are the ones where we run into it, and it's a really ugly situation.
So first, let's say your response data that's coming back isn't related with the update that you want to happen. So I know there were some situations where we had an object we were going to favorite, like a product that was being favorited. And you favorited it, the mutation goes out, it comes back. You return the ID and the favorite status, and that updated, and it would update that product wherever it was being used in that UI. The thing it's not going to update is how many products are favorited. Like let's say you had a UI that was showing the number of favorited objects. It might be related, but it's not the same data. So in this scenario, you would have to write your own update function and update that data yourself, even though it might seem related to you.
So then the rest of these are about lists, because that's really the hardest thing to manage, where it's like, if you, again, don't return the whole list of updated objects, you're not going to get that automatic update. The same is true if the order changes. So if you were to send out, let's say, objects in order 1, 2, 3, 4, and you're going to change it to 1, 2, 4, 3, when it comes back, the objects are still the same. The only thing that changes is the order. It's not going to reflect in your UI. That's something you'd have to write automatically. And it's mainly because the cache makes no assumptions about how you want to store your data or what your data should look like inside the cache. Those objects are identical. And the only thing it has reference to is the references to those objects. And then finally, adding or removing things, which also really sucks, because if I was going to unfavorite something, I can update that object's favorite status, but I can't remove it from a list of favorite objects. That was something you'd have to write an update function for, because, again, it doesn't know that you can't return something from a mutation and say, OK, yeah, now remove this for me. You can just return something. So again, the update functions exist to do that, but in these scenarios, like, it can be a bug where if you're not expecting it to update automatically. And we've definitely run into those.
Comments