-
-
Notifications
You must be signed in to change notification settings - Fork 365
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
Regression in 7.4.1: TData generic breaks type check for custom mutation options function #1820
Comments
Can you try 7.4.1 i think this is fixed. |
Thanks a lot for the quick reply! I'm afraid not. Both 7.4.0 and 7.4.1 have the same problem. I've in the meantime I found a way to unblock the upgrade by dropping the type safety in the custom mutation options function. Something like the following: /** Shows a default, generic error snack bar if the request fails. Callers can override this behavior. */
export const useDefaultOpenApiMutationOptions = <T>(mutationOptions: T): T => {
const { t } = useTranslation();
// The parameter has the type UseMutationOptions. However, using this type would cause the call
// in the Orval-generated mutation code to not type-check due to https://github.com/orval-labs/orval/issues/1820.
// Hence, for loosen the type safety of the method signature and cast.
const typedMutationOptions = mutationOptions as UseMutationOptions;
return {
onError: (error: unknown) => {
// Only display snackbar if error is not being caught by an error boundary.
if (!typedMutationOptions.useErrorBoundary) {
SnackbarUtils.error('Something went wrong');
}
},
...typedMutationOptions,
} as T;
}; While this works, it's essentially a hack. I think other users of Orval will run into the same problem when trying to use custom mutation options functions, and will not know how to type them. Orval's documentation does not give an example of how to type such a function. |
I also think that since the 7.4 the support of https://github.com/lentyaevpk/orval/blob/master/packages/query/src/index.ts#L501 |
@zdarovka did you try 7.4.1? |
yes |
OK I updated the title of the ticket to say 7.4.1 |
I was thinking explicit typing would be preferable, but does that mean we lose type safety if we use custom mutations? What do you think? |
@severinh @oferitz-apex @zdarovka @melloware Hey guys, |
@soartec-lab In my specific case the union type with |
Yep go ahead and revert it. |
@severinh @oferitz-apex @zdarovka @melloware I reverted this by #1871 . A new version will be released soon, so please check back soon. Look forward to the new version. @oferitz |
What are the steps to reproduce this issue?
In our system, we use the
mutationOptions
Orval configuration to add a default error handler to all mutations to show an error snackbar in the product (unless overridden by the user of the mutation):Config used
useDefaultOpenApiOptions.ts
contains the following:This worked well until 7.3.0. With the upgrade to 7.4.0, the code generated by Orval does not type-check anymore.
The error is:
The same type check error happens for all generated mutations.
Cause
The change that introduced this breakage is #1735.
The comment in that PR mentions that this is a breaking change, but it does not say how teams affected by the breakage could resolve the breakage.
Resolution?
I could not figure out how to update our custom mutation options function to make the type checking issue go away. It seems the problem is not our function, but rather the type inconsistency in the generated mutation itself between the
mutationOptions
variable (anyTData
allowed) andmutationFn
(TData
is the actual response type).Could you tell me how I can make this type-check? Right now this blocks us from upgrading to Orval 7.4.0.
If it's not possible on our end, can we revert #1735? I personally don't understand why that PR was needed, and what value it brings to override the
TData
of a mutation.What versions are you using?
The text was updated successfully, but these errors were encountered: