TheCactusBlue / react-cell

Redwood Cells, but without Redwood and with TypeScript
MIT License
4 stars 1 forks source link

Allow passing options to useQuery #1

Open janwirth opened 3 years ago

janwirth commented 3 years ago

I would like to use polling to have a 'reactive' cell. The current implementation does not support this. I propose allowing to pass a third, optional argument that gets used as 'options' argument for the useQuery apollo hook.

tubbo commented 1 year ago

Redwood's implementation has a beforeQuery() and afterQuery() hook that you can optionally define to configure the Apollo client before running the query and to decorate data after the query resolves. I'm not a huge fan of those names, but I do think it's pretty useful to have stuff like that in the cell implementation as well. It might also be useful to define isEmpty() in order to configure when the empty component will render. You should be able to define a hook that configures useQuery() to use this polling behavior.

I forked this repo and spiked on a possible implementation of this, exporting more type definitions and adding the configure(), decorate(), and isEmpty() hooks which can be optionally defined in your cell implementation to configure useQuery() options, decorate data before rendering to the screen, and determining whether the data coming back is empty or not, respectively. The configure() hook is analogous to beforeQuery (except it doesn't set a default fetch policy), and the decorate() hook has the same API as afterQuery.

It's not ready for PR just yet, as I need to add tests and make sure it works in the real world, but I think it's a good start:

https://github.com/TheCactusBlue/react-cell/compare/master...tubbo:react-cell:more-type-defs?expand=1

@TheCactusBlue if you're OK with it I could make a pull request here to add this into the library.

TheCactusBlue commented 1 year ago

Yep, I am always happy with PRs, will take a look when I get back home!