So data transformations was the second thing a good state manager has. So let's take a look at how we can do data transformations in Apollo Client. So this is again an example, right? On the right hand side, you will see a sample data.json, wherein we can again see products. We have Glossier, and like item name is lipstick, prize, and so on. It's a very typical product, eCommerce product, right? On the left hand side, we have getCartQuery, and on the bottom, we can invoke that query, right? So again, this is exactly how we would invoke the data. We would declare exactly what we want, and then invoke that query inside our react component using useQuery.
So how do we manipulate data then, right? Like how do we do more things with it, for example? So in this case, again, if you notice that we are manipulating data. So here is how we can manipulate data. So on the right hand side, again, it's the same product, right? But on the left hand side, we are invoking the query, and we already took a look at the query in the previous slide, but we're invoking the query, and here we're just displaying whether to display getOutOfStockText or low stock text. If the stock minus quantity is less than or equal to 2, then it's low stock. Otherwise, if it says it's out of stock, it's out of stock, right? Again, very simple example, but this is exactly how you can manipulate data because exactly you get data back from GraphQL. But you might imagine that data will get repeated, right? Let's say if you want to show that same product in different places, you want to make sure that we are storing out of stock message in all the different places as well.
So let's take a look at what localOnlyFields are introducing localOnlyFields. In this case, we can see that we have Apollo Client and Apollo Server, and in the cache itself, we can add more data. So whenever a GraphQL makes a query, or an Apollo client makes a query and we get data on the frontend, we can additionally add more data to that cache. All the data is stored in Apollo Client 3 in the form of cache, right? Similarly to Redux, all the data was stored inside a stateManager. So for example, if we were to refresh the page, the data is gone, we have to refresh all over again. Same thing over here too, it's cache, and we need to make sure that we are storing new data in that cache as well. So how can Apollo Client differentiate between server data and frontend data, right? This is exactly what we're going to take a look at. So you can see over here, stock text has this specific value next to it called atClient, and that allows us to specify that so that we can say that, resolve this specific field on the client side, right? And then whenever we want to resolve it, whenever there's an atClient next to it, we can just call the cache for it.
So let's take a look at an example, same example, again, same cart query, right? So what happens is at the bottom, we are invoking the query inside the React component. And we all know about that, we have reviewed that code already. But on the top, for stock text, Apollo client will resolve stock text on the client side, right? So again, getCartQuery is exactly the same, but in addition to stock text that we can just resolve on the client side, which is amazing. So local only fields allow us to do that. We can just declare as many local only fields as we like. Now if we can imagine, if that same card object is being displayed in different ways, we can access stock text as well in all the different places in our front end. Isn't that amazing? So again, if you want to add, define more client side fields in the GraphQL type itself, that doesn't need to come from the server, we can define local only fields so you can resolve state locally, right? On your front end.
Now this is an example, again, a core sample of what is in-memory cache and how we are declaring it. So again, that cache that I talked about on the front end, that is called in-memory cache. And we can declare a field called as stock text, if you want to now go ahead and resolve that stock text, right? Stock text, we are able to declare it with the at client derivative, but then in the code itself in-memory cache, we have to then say what that stock text will resolve to.
Comments