diff --git a/src/mutations.tsx b/src/mutations.tsx index f7f27665b..84bf465b1 100644 --- a/src/mutations.tsx +++ b/src/mutations.tsx @@ -1,19 +1,14 @@ -import { ApolloError } from "apollo-client"; +import { ApolloError, MutationUpdaterFn } from "apollo-client"; import { DocumentNode } from "graphql"; import React from "react"; -import { - Mutation, - MutationFn, - MutationResult, - MutationUpdaterFn -} from "react-apollo"; +import { Mutation, MutationFunction, MutationResult } from "react-apollo"; import useNotifier from "./hooks/useNotifier"; import i18n from "./i18n"; export interface TypedMutationInnerProps { children: ( - mutateFn: MutationFn, + mutateFn: MutationFunction, result: MutationResult ) => React.ReactNode; onCompleted?: (data: TData) => void; @@ -21,26 +16,17 @@ export interface TypedMutationInnerProps { variables?: TVariables; } +// For some reason Mutation returns () => Element instead of () => ReactNode export function TypedMutation( mutation: DocumentNode, update?: MutationUpdaterFn ) { - class StrictTypedMutation extends Mutation {} return (props: TypedMutationInnerProps) => { const notify = useNotifier(); - // Obviously, this is workaround to the problem described here: - // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/32588 - const { - children, - onCompleted, - onError, - variables - } = props as JSX.LibraryManagedAttributes< - typeof StrictTypedMutation, - typeof props - >; + const { children, onCompleted, onError, variables } = props; + return ( - { @@ -55,8 +41,8 @@ export function TypedMutation( variables={variables} update={update} > - {children} - + {(mutateFn, result) => <>{children(mutateFn, result)}} + ); }; } diff --git a/src/queries.tsx b/src/queries.tsx index 43a066183..58266d99e 100644 --- a/src/queries.tsx +++ b/src/queries.tsx @@ -1,9 +1,9 @@ +import { ApolloQueryResult } from "apollo-client"; import { DocumentNode } from "graphql"; import gql from "graphql-tag"; import React from "react"; import { Query, QueryResult } from "react-apollo"; -import { ApolloQueryResult } from "apollo-client"; import AppProgress from "./components/AppProgress"; import ErrorPage from "./components/ErrorPage/ErrorPage"; import useNavigator from "./hooks/useNavigator"; @@ -62,98 +62,82 @@ class QueryProgress extends React.Component { } } +// For some reason Query returns () => Element instead of () => ReactNode export function TypedQuery( query: DocumentNode ): React.FC> { - class StrictTypedQuery extends Query {} - return props => { + return ({ children, displayLoader, skip, variables, require }) => { const navigate = useNavigator(); const pushMessage = useNotifier(); return ( - {({ setProgressState }) => { - // Obviously, this is workaround to the problem described here: - // https://github.com/DefinitelyTyped/DefinitelyTyped/issues/32588 - const { - children, - displayLoader, - skip, - variables, - require - } = props as JSX.LibraryManagedAttributes< - typeof StrictTypedQuery, - typeof props - >; - return ( - - {queryData => { - if (queryData.error) { - const msg = i18n.t("Something went wrong: {{ message }}", { - message: queryData.error.message - }); - pushMessage({ text: msg }); - } - - const loadMore = ( - mergeFunc: ( - previousResults: TData, - fetchMoreResult: TData - ) => TData, - extraVariables: RequireAtLeastOne - ) => - queryData.fetchMore({ - query, - updateQuery: (previousResults, { fetchMoreResult }) => { - if (!fetchMoreResult) { - return previousResults; - } - return mergeFunc(previousResults, fetchMoreResult); - }, - variables: { ...variables, ...extraVariables } - }); - - let childrenOrNotFound = children({ - ...queryData, - loadMore + {({ setProgressState }) => ( + + {queryData => { + if (queryData.error) { + const msg = i18n.t("Something went wrong: {{ message }}", { + message: queryData.error.message }); - if ( - !queryData.loading && - require && - queryData.data && - !require.reduce( - (acc, key) => acc && queryData.data[key] !== null, - true - ) - ) { - childrenOrNotFound = ( - navigate("/")} /> - ); - } + pushMessage({ text: msg }); + } - if (displayLoader) { - return ( - setProgressState(false)} - onLoading={() => setProgressState(true)} - > - {childrenOrNotFound} - - ); - } + const loadMore = ( + mergeFunc: ( + previousResults: TData, + fetchMoreResult: TData + ) => TData, + extraVariables: RequireAtLeastOne + ) => + queryData.fetchMore({ + query, + updateQuery: (previousResults, { fetchMoreResult }) => { + if (!fetchMoreResult) { + return previousResults; + } + return mergeFunc(previousResults, fetchMoreResult); + }, + variables: { ...variables, ...extraVariables } + }); - return childrenOrNotFound; - }} - - ); - }} + let childrenOrNotFound = children({ + ...queryData, + loadMore + }); + if ( + !queryData.loading && + require && + queryData.data && + !require.reduce( + (acc, key) => acc && queryData.data[key] !== null, + true + ) + ) { + childrenOrNotFound = navigate("/")} />; + } + + if (displayLoader) { + return ( + setProgressState(false)} + onLoading={() => setProgressState(true)} + > + {childrenOrNotFound} + + ); + } + + return <>{childrenOrNotFound}; + }} + + )} ); };