Now, of course, I can't take credit for that. Tener Linsley made the library, and he designed most of the APIs. And he actually has a very good tweet summarizing the goals of the library, where he says that the query API is actually medium sized when you unpack it all, but the most important part is that you can understand and learn how to use it by starting with a single function that provides 80% of the entire value proposition first try. From there, the rest of its API can be gradually learned if needed. And I think that's what it takes for a library to become popular. It needs to be both minimal and intuitive, as well as powerful and flexible.
But for any given API, you know, those two things are usually on the opposite sides of the same scale. If we take a look at array methods, for example, on the left-hand side, we would have something like array.join, right? A very simple method. It does one thing and does it very well, and there's no surprises there. And on the other end of the spectrum, we would have something like array.reduce, which is very powerful. We can implement all other array methods just with reduce. But you know, if this is the only thing we have available, it would probably also not be great, because it's also quite complicated to read from time to time.
Now, for libraries, I think the second scale is missing, and that should usually be application complexity, because as your app complexity grows, you actually want your APIs to become more powerful and flexible. And on that scale, I would put useQuery right about here, bottom left, if we call it with the minimal required arguments, which is basically just the query key and the query function. Now, that API, I think, is still quite simple and easy to use, but it gives us a lot of things already out of the box. We get things like caching, requested duplication, background updates, you know, automatic garbage collection, like the list goes on and on. There's a lot of things that we get from just this one function call. And then later on, we might add a useMutation call, right, to make an update and then link it back with invalidate queries. So, this is already a little bit more involved, but, you know, we can get really far with just those two functions.
So, I put that right about here. But as time goes on and your application becomes more complex, you might want to do more things. So, you're going to make an optimistic update, maybe from time to time, or you need an infinite query. And those APIs are certainly a bit more complex. And all the way up, we have, like, for example, our plugins or the cache subscriptions, which are really, really low-level. For example, our dev tools are built with the cache subscriptions. But, you know, once you get to a point where you need this complexity, you're probably happy that those exist as well, just like you are about using reduce from time to time. And that gets me right to the first learning that I had as an open-source maintainer, which is I'm no longer excited about major versions. And I think you probably shouldn't be either. API design is hard.
Comments