mui / toolpad

Toolpad: Full stack components and low-code builder for dashboards and internal apps.
https://mui.com/toolpad/
MIT License
964 stars 243 forks source link

Reloading the app hits the API #852

Closed prakhargupta1 closed 2 years ago

prakhargupta1 commented 2 years ago

Duplicates

Latest version

Current behavior 😯

Every time I reload this app, I see another entry in the table for that timestamp.

There are 2 queries in this app, query10 is the one responsible for POST call. A GET call on page reload makes sense, but should there be a setting for the POST method, or may be in general?

Expected behavior 🤔

On page reload, a new blank entry should not be created.

Janpot commented 2 years ago

a query is a way to make state available on the page, it is always fetched, regardless of how the datasource is accessed. Using a query is telling Toolpad "hey, I'm interested in this data, do whatever you can to make it available to the page". That includes automatic retries on failure, or rerunning periodically to make sure the latest data is available. This is very suitable to show data, but not so much for destructive actions. (BTW, it's not very RESTful, but perfectly valid for an API to use POST requests for data fetching.)

If you want to run an action on an event, and want to communicate to Toolpad "hey, this datasource access is destructive, never run it automatically" then you need to use a mutation, that's exactly what they are for. Toolpad will never run these automatically, you will have to explicitly specifiy when you want it to be executed.

prakhargupta1 commented 2 years ago

For each query Retool asks when to run the query:

  1. Run query only when manually triggered
  2. Run query automatically when inputs change.

Also, for POST method they have default setting, that it will run on user interaction:

Screenshot 2022-08-25 at 3 57 40 PM
Janpot commented 2 years ago

For each query Retool asks when to run the query

Yep, that's basically another way to define what is "a query" and what is "a mutation". We can do it that way as well for Toolpad and remove "mutations" if we want. If you feel strongly about it, feel free to open a feature request. Ideally such big issues are caught during the PR phase in the preview on render.com before they make it into master.

prakhargupta1 commented 2 years ago

Correct me if I am wrong. I realized I should have used mutation for the write call and query should ideally be used for read operations only. When I used mutation to do the same thing, on reload I don't get another entry on page reload. So it only runs on some event.

So I think it is we should keep them separate, the way it is in react query, and we can also keep this issue closed.

Janpot commented 2 years ago

In hindsight, I think if we had done something more like retool it might have prevented you from having to open this issue so there could be an argument that their UX is better.