Hacker News new | ask | show | jobs
by filoeleven 1652 days ago
> With libs like ReactQuery every component "simply" fetches what it needs, sort of directly communicating to the Backend. No intermediary party (like a Store)

But in the article, you wrote:

> What if we could have an intermediate player between our components and Backend, say an API wrapper or interceptor, solving all those problems under the hood: [deduping, consistency, invalidation/automatic data refresh, hiding implementation]

The way I look at it, I don’t expect (or want) the value to be fetched from the server, I expect it to be fetched from “the single source of truth that abstracts the server away from my widget’s concerns.” I still think of that as a store, because that’s what stores advertise themselves as, it’s just one with a better API. The differences in how we are talking about this may come down to semantics—or maybe you can trust your servers more than I trust mine :)

To me, “directly communicating with the backend” implies that I have to have REST calls in my components, and handle all that comes with that, and that makes me want to run and hide. I don’t think that is your intent. There is still an intermediary here, and it is one that better separates ALL of the data fetching aspects from the widget by being async and providing good feedback on request/response status. Your diagram with the “API Wrapper” in it displays this quite well.

It’s a good design principle; it’s how I want to build things! At work we recently migrated to React, and I tried to follow similar principles without awareness of things like ReactQuery. I will definitely be looking more into this (especially the update side of things), and thinking about if/how it could simplify what we are doing. I’m not sure if it will work for us, but maybe I can at least steal a few more good ideas.

Thanks for writing the article and answering my question!

1 comments

>But in the article, you wrote:

I also wrote a bit below: "be transparent to components and not affect their logic in any way (make components think they communicate to Backend directly)"

I met people trying to use ReactQuery with the mindset that it's a Store. The result was that they were greatly frustrated with the outcome. That mindset led them to use ReactQuery in a way it's not supposed to be used. Every now and then they wanted to directly manipulate with the Cache (the "Store" underneath ReactQuery).

That's why I find that Store-mindset very dangerous when working with such Libraries.

Better come with a mindset that there is no Store at all. Better think that "useBookQuery" is just a simple hook to fetch data. NO STORE.

>To me, “directly communicating with the backend” implies that I have to have REST calls in my components, and handle all that comes with that

This is exactly what I meant :)

I'm glad that you found the article useful. Thanks again for providing your feedback!

Thanks again for your thoughtful response.

I have more questions about whether ReactQuery can help me out when working with a bad legacy API, but I don’t know how to ask them clearly. It would be unfair to ask you to understand the system I’m working with, especially when you’ve already written an article that clearly describes the idea and have been so generous with your responses. I will be looking at this more deeply for sure.

This kind of thing is why I come to HN.