import { ApolloError, MutationUpdaterFn } from "apollo-client"; import { DocumentNode } from "graphql"; import React from "react"; import { Mutation, MutationFunction, MutationResult } from "react-apollo"; import { useIntl } from "react-intl"; import useNotifier from "./hooks/useNotifier"; import { commonMessages } from "./intl"; export interface TypedMutationInnerProps { children: ( mutateFn: MutationFunction, result: MutationResult ) => React.ReactNode; onCompleted?: (data: TData) => void; onError?: (error: ApolloError) => void; variables?: TVariables; } // For some reason Mutation returns () => Element instead of () => ReactNode export function TypedMutation( mutation: DocumentNode, update?: MutationUpdaterFn ) { return (props: TypedMutationInnerProps) => { const notify = useNotifier(); const intl = useIntl(); const { children, onCompleted, onError, variables } = props; return ( { notify({ text: intl.formatMessage(commonMessages.somethingWentWrong) }); if (onError) { onError(err); } }} variables={variables} update={update} > {(mutateFn, result) => <>{children(mutateFn, result)}} ); }; }