relay-tools / relay-hooks

Use Relay as React hooks
https://relay-tools.github.io/relay-hooks/docs/relay-hooks.html
MIT License
540 stars 56 forks source link

Make type generic for mutation for `onError` function #167

Open LFSCamargo opened 3 years ago

LFSCamargo commented 3 years ago

The Problem

The return type for the server is different from the relay error type so i created the interface and a helper to handle errors and i got the error bellow i think a good solution would be to add the generic type to the useMutation hook

Behaviour

Screen Shot 2021-03-11 at 8 55 08 PM

How i thought to resolve this issue

export interface MutationConfig<TOperation extends MutationParameters, TError = Error> {
    configs?: DeclarativeMutationConfig[];
    cacheConfig?: CacheConfig;
    mutation: GraphQLTaggedNode;
    onError?: ((error: Error) => void) | null;
    onCompleted?:
        | ((response: TOperation['response'], errors: ReadonlyArray<PayloadError> | null | undefined) => void)
        | null;
    onUnsubscribe?: () => void | null | undefined;
    optimisticResponse?: TOperation['response'];
    optimisticUpdater?: SelectorStoreUpdater<TOperation['response']> | null;
    updater?: SelectorStoreUpdater<TOperation['response']> | null;
    uploadables?: UploadableMap | null;
    variables: TOperation['variables'];
}
morrys commented 3 years ago

Hi @LFSCamargo, the behavior of useMutation is conditioned by the commitMutation of relay-runtime https://github.com/facebook/relay/blob/master/packages/relay-runtime/mutations/commitMutation.js#L65 It would be more correct to propose this in relay-runtime.