saga-query initial release

June 16, 2021 on Eric Bower's blog

I’m excited to officially announce the initial release of a new library I wrote called saga-query.

For the past month, most of my free time has been dedicated to building a new way to manage data fetching using redux and redux-saga, primarily for react apps. My goal for this library is to create a koa-like middleware system for interacting with APIs using redux-saga.

There are a lot of common processes to fetching API data:

On top of those common ETL processes, there’s also UX-specific functionality baked into it:

There’s a lot going on here, not to mention the asynchronous nature of javascript makes it even more complicated. Creating common abstractions that work for the needs of every web app is difficult. It’s difficult to construct common abstractions even for a specific app’s needs.

I love koa, it’s a simple web server written in node.js that creates a robust middleware system that allows end-developers to manage API requests and responses. I thought to myself: what if we did that same thing on the front-end? What if we could create a middleware system using redux-saga that handles all the ETL logic of interacting with an API? That idea culminated in saga-query.

This library was ultimately inspired by react-query, redux-toolkit, and koajs.

Those libraries offer a ton of functionality for data fetching and caching.

The redux maintainers have done a great job moving the state management ecosystem forward by creating common abstractions people can use to reduce boilerplate. However, there are no common abstractions being built for people using redux-saga which is my library of choice for medium to large sized projects. redux-toolkit heavily prefers redux-thunk and has little interest in supporting paradigms that revolve around sagas.

react-query has made the distinction clear between UI state and API data for state management. Data, they argue, should be automatically cached and not something the end-developer needs to think about. I love this idea because caching API data is a huge part of constructing a front-end application. However, for medium to large sized projects, I’ve inevitably needed to control the async flow control as well as the caching logic for my API data. It’s not uncommon to performance tune my selectors, build special indexes to make queries faster, and to leverage memoization with reselect to squeeze performance out of my data caching layer. I’m also not a fan of every piece of business logic living inside of the view layer, react. I think with react-query, react is consuming to entire stack and I find that to be less than idea. I want my side-effects to be treated as data and separated from my view layer. For these reasons and more, react-query is not something I would reach for unless it was a small application.

To learn more about why I prefer redux-saga and treating side-effects as data, read my previous article on redux-saga style guide or simplify testing async I/O in javascript.

If you’re like me, someone who prefers treating side-effects as data and wants full control over the data synchronization of your application, I recommend giving saga-query a try. This library, coupled with robodux will not only reduce boilerplate, but give you total control over fetching and caching logic using a koa-style middleware.

Visit the Github repo to learn more.

Articles from blogs I read

Generated by openring

A finger client

This is a short follow-up to the io_uring finger server article posted about a month ago. In the time since, we have expanded our language with a more complete networking stack, most importantly by adding a DNS resolver. I have used these improvements to wri…

via Drew DeVault's blog June 24, 2021

The Go Collective on Stack Overflow

Since the earliest days of Go, Stack Overflow has been a significant part of the Go user experience. For the past five years, the Go user survey has consistently identified Stack Overflow as the #1 place users go to find answers to their…

via The Go Programming Language Blog June 23, 2021

How to use React Context effectively

I've posted a new article "How to use React Context effectively" and you can read it online. How to create and expose React Context providers and consumers …

via Kent C. Dodds Blog RSS Feed June 5, 2021