-
Notifications
You must be signed in to change notification settings - Fork 4
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
useApiMutation hook #48
Conversation
Code Climate has analyzed commit 3767d87 and detected 0 issues on this pull request. The test coverage on the diff in this pull request is 100.0% (100% is the threshold). This pull request will bring the total coverage in the repository to 100.0% (0.0% change). View more on Code Climate. |
9cb6ccc
to
8984be1
Compare
8984be1
to
cd71b8d
Compare
deregisterMutation() | ||
} | ||
|
||
params.onSuccess?.(returnValue, {mutationArgs}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason this needs to run after the try-catch block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This way, if an error occurs inside the onSuccess
handler, onError
will not trigger. This could avoid situations where a success snackbar shows via onSuccess
, but then an error occurs after that, causing onError
to be called and immediately triggering an error snackbar. I figure it's more clear that onError
is only called if the actual mutation
fails
TBH I was originally not going to add the onSuccess
callback at all, but I think the above distinction justifies it's usefulness. It's also consistent with other popular data-fetching libraries. LMK what you think
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense!
Added
useApiMutation
hook (resolves #28):Notes about API decisions:
mutation
property is curried so it can passapi
helpers (a subset ofapi
class methods), primarily so that the types can be inferred for the mutation function (createUser
in the example)onError
reduces the need to add atry/catch
blockonSuccess
allows for logic to run once the mutation completes. It's worth noting that if an error occurs inside theonSuccess
handler,onError
will not trigger. This is important because the error handler should only apply to the actual mutation operation.request
's default fetch policy isno-cache
by default, but can be overridden viaApiProvider
or per-request.Removed
Api#defaultFetchPolicy
(resolves #37)defaultFetchPolicy
, since this was just added for mutations. Also, this now defaults tono-cache
(which I think is the most sensible default for making the rare raw api call).