-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Request creators #432
Comments
Hi @klis87,
Answers/opinions:
|
Regarding FSA, this is really a tough call, I thought about this because API is growing, lib becomes more and more complex and this could simplify things a lot in many places. Plus, with this library, really you almost don't need to use Redux at all, and my goal is to make it clear that no Redux knowledge is required to use it, but thx to Redux used for the core, apps are easy to debug, we have access to stable ecosystem and we can track any state transition in Redux dev tools for free. Regarding FSA-only middleware, are you aware of any popular which assumes only FSA action types? |
Thanks for your response.
Nope :) |
|
I agree with all points, and totally agree that current necessity to provide action type together with action creator (in default setup without help of other action creator libraries) looks very ugly - request creator should gracefully solve that issue. Just want to say 2 things:
const {load} = useQuery({
type: 'IS_EMAIL_REGISTERED',
action: isEmailRegistered,
autoload: false
});
// This is submit handler for form provided by "formik" library:
const onFormSubmit = (formValues, formHelpers) => {
// Before user registration - check whether user with such email already registered,
// and if yes - display proper error below email input:
const { data: isEmailAlreadyRegistered } = await load(formValues.email); // <-- HERE IS VARIABLES PASSED
if (isEmailAlreadyRegistered) {
formHelpers.setFieldError('email', 'Email already registered');
return;
}
// ... Email is not registered, so make request here to register user
} |
@avasuro thx for the great feedback!
Yeah, also And, if you see any problems and we would still need to support both, wondering how we should do that. I don't wanna force FSA, but how to make it configurable?
This is a really tough nut to crack
very good example, I assumed that useQuery will rerender on variable change and new memoized load will be used, but indeed, it is not always the case, I will need to make this change! |
Adding to this discussion, it is worth mentioning, that even currently all utility actions like |
To sum up, the only way I could support FSA action is to transform all actions to FSA mode, for example { type: 'RESET_REQUESTS', payload: ['QUERY1', 'QUERY2'] } but really I am not sure we should go FSA or not FSA. However, supporting both would just cause big overhead without too big reasons. |
I read your previous answers, and there is something to think about, so I will answer later (hope I will not forget about them). But according to this answer:
I think there is no sense to transform internal actions of this library to FSA (or not FSA). When I wrote about that it is worth keeping the support of FSA events, I meant the ability to create actions for requests. Like this library should support both versions of actions: Because "initial" actions can be created in several ways (using plain object, or using some action creator library, or using some utility function provided by this library) and it is not always easy to produce only non-FSA (or only FSA) actions. While "internal" actions (reset, abort) can be created only via utility functions provided by this library (in theory they also can be created manually, but that is unsupported way, so no need to take it into account, I think). |
@avasuro Good points. The question is then, will all people use createQuery, createMutation and createSubscription helpers? In my opinion yes, and in fact this way will be advocated in all examples and documentations. This is the only way I can show nice API in examples, without duplicating action and constant imports in places like But, if not, if some people preferred not using those helpers, then it would be enough to support fsa request actions in reducers and middleware, helpers themselves wouldn't need to support fsa flag. |
There is also one important thing to put in the equasion, I am pretty sure that the next major version will support request actions only created from Redux action creator lib, be it |
Mmm, if I understand right: to simplify library code it will be good to choose some one type of actions (FSA or non-FSA) to use inside this library. In short:If you force users to use only your helpers ( Longer explanation:Should we force users to use only our helpers to create actions? How user can produce actions?
Pros and cons of choosing FSA/non-FSA
So, according all above: I think it's better to use FSA actions in this lib, because they will work in all possible use cases. Yes, this approach is not backwards compatible, but because currently library supports both actions types dropping support of any of them is a breaking change, so this con is not so significant from my point of view. |
Fair enough, then you can exclude all things related to "manually created actions" from my previous answer. |
@avasuro thx for your analysis, indeed it looks like going FSA all the way could be the best option. Actually we almost have FSA anyway, as we use I know this is quite a big change, but this will be for a reason. Not for all drivers it is possible to deduct whether it is query or mutation. For example, for promise driver it is always necessary to pass But with Also, I have some more plans for action creator helpers - one of my TODO is adding support for offline mutations - they will be queued when offline, and could be auto dispatched when going back online. But callbacks are not serializable, so we cannot just save mutations to local storage for example and recreate them on reload when going online. But I am pretty sure I will be able to mitigate it with some alternatives, thx to nothing else than request creators. This is yet another argument to really enforce usage of this, but in the docs we should of course explain, what it does under the hood, so Redux users will understand what's going on behind the scenes |
Right now we create requests like this:
This is problematic due to the following reasons:
RequestAction
interface, but with pure JS you won't get autocomplete at alluseQuery
,type
is needed for selector and action is needed to be called to automatically fetchtoString
method returning this type, so that action becomes both action itself and source of its type, I mean libraries likeredux-act
,redux-actions
and myredux-smart-actions
So, suggestion? For instance helper like
createQuery
, which could work like that:Then,
fetchBook(1)
will return the same what previous version, butfetchBook.toString() === 'FETCH_BOOK'
, so no constants anymore.What we gain?
5) autocomplete
6) No reason to explain why we need constants for people not knowing Redux
7) We could do nice stuff, like inject
meta.variables
which might be nice source of info, we know also whether request is query or mutation, so we won't need things likemeta.asQuery
anymore8)
createQuery
etc could have some generics, which could be filled with drivers like axios driver, then you could importcreateQuery
from a driver and also have driver config typed and autocompletedThis would be not breakable, current way of writing would also work, but in the next major version:
9) API simplification,
useQuery
won't haveaction
prop anymore, as type will be always action creator, never string10) I would really remove support for FSA action, no
payload.request
, justrequest
, this really shouldn't matter, and library could be simplifiedBut are there any disadvantages? I cannot see, but maybe I miss some, please write if anything comes to your mind!
The text was updated successfully, but these errors were encountered: