Fix types
This commit is contained in:
parent
2f7867bf99
commit
ebd4f3093c
2 changed files with 75 additions and 105 deletions
|
@ -1,19 +1,14 @@
|
||||||
import { ApolloError } from "apollo-client";
|
import { ApolloError, MutationUpdaterFn } from "apollo-client";
|
||||||
import { DocumentNode } from "graphql";
|
import { DocumentNode } from "graphql";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import {
|
import { Mutation, MutationFunction, MutationResult } from "react-apollo";
|
||||||
Mutation,
|
|
||||||
MutationFn,
|
|
||||||
MutationResult,
|
|
||||||
MutationUpdaterFn
|
|
||||||
} from "react-apollo";
|
|
||||||
|
|
||||||
import useNotifier from "./hooks/useNotifier";
|
import useNotifier from "./hooks/useNotifier";
|
||||||
import i18n from "./i18n";
|
import i18n from "./i18n";
|
||||||
|
|
||||||
export interface TypedMutationInnerProps<TData, TVariables> {
|
export interface TypedMutationInnerProps<TData, TVariables> {
|
||||||
children: (
|
children: (
|
||||||
mutateFn: MutationFn<TData, TVariables>,
|
mutateFn: MutationFunction<TData, TVariables>,
|
||||||
result: MutationResult<TData>
|
result: MutationResult<TData>
|
||||||
) => React.ReactNode;
|
) => React.ReactNode;
|
||||||
onCompleted?: (data: TData) => void;
|
onCompleted?: (data: TData) => void;
|
||||||
|
@ -21,26 +16,17 @@ export interface TypedMutationInnerProps<TData, TVariables> {
|
||||||
variables?: TVariables;
|
variables?: TVariables;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For some reason Mutation returns () => Element instead of () => ReactNode
|
||||||
export function TypedMutation<TData, TVariables>(
|
export function TypedMutation<TData, TVariables>(
|
||||||
mutation: DocumentNode,
|
mutation: DocumentNode,
|
||||||
update?: MutationUpdaterFn<TData>
|
update?: MutationUpdaterFn<TData>
|
||||||
) {
|
) {
|
||||||
class StrictTypedMutation extends Mutation<TData, TVariables> {}
|
|
||||||
return (props: TypedMutationInnerProps<TData, TVariables>) => {
|
return (props: TypedMutationInnerProps<TData, TVariables>) => {
|
||||||
const notify = useNotifier();
|
const notify = useNotifier();
|
||||||
// Obviously, this is workaround to the problem described here:
|
const { children, onCompleted, onError, variables } = props;
|
||||||
// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/32588
|
|
||||||
const {
|
|
||||||
children,
|
|
||||||
onCompleted,
|
|
||||||
onError,
|
|
||||||
variables
|
|
||||||
} = props as JSX.LibraryManagedAttributes<
|
|
||||||
typeof StrictTypedMutation,
|
|
||||||
typeof props
|
|
||||||
>;
|
|
||||||
return (
|
return (
|
||||||
<StrictTypedMutation
|
<Mutation
|
||||||
mutation={mutation}
|
mutation={mutation}
|
||||||
onCompleted={onCompleted}
|
onCompleted={onCompleted}
|
||||||
onError={err => {
|
onError={err => {
|
||||||
|
@ -55,8 +41,8 @@ export function TypedMutation<TData, TVariables>(
|
||||||
variables={variables}
|
variables={variables}
|
||||||
update={update}
|
update={update}
|
||||||
>
|
>
|
||||||
{children}
|
{(mutateFn, result) => <>{children(mutateFn, result)}</>}
|
||||||
</StrictTypedMutation>
|
</Mutation>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
|
import { ApolloQueryResult } from "apollo-client";
|
||||||
import { DocumentNode } from "graphql";
|
import { DocumentNode } from "graphql";
|
||||||
import gql from "graphql-tag";
|
import gql from "graphql-tag";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { Query, QueryResult } from "react-apollo";
|
import { Query, QueryResult } from "react-apollo";
|
||||||
|
|
||||||
import { ApolloQueryResult } from "apollo-client";
|
|
||||||
import AppProgress from "./components/AppProgress";
|
import AppProgress from "./components/AppProgress";
|
||||||
import ErrorPage from "./components/ErrorPage/ErrorPage";
|
import ErrorPage from "./components/ErrorPage/ErrorPage";
|
||||||
import useNavigator from "./hooks/useNavigator";
|
import useNavigator from "./hooks/useNavigator";
|
||||||
|
@ -62,31 +62,18 @@ class QueryProgress extends React.Component<QueryProgressProps, {}> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For some reason Query returns () => Element instead of () => ReactNode
|
||||||
export function TypedQuery<TData, TVariables>(
|
export function TypedQuery<TData, TVariables>(
|
||||||
query: DocumentNode
|
query: DocumentNode
|
||||||
): React.FC<TypedQueryInnerProps<TData, TVariables>> {
|
): React.FC<TypedQueryInnerProps<TData, TVariables>> {
|
||||||
class StrictTypedQuery extends Query<TData, TVariables> {}
|
return ({ children, displayLoader, skip, variables, require }) => {
|
||||||
return props => {
|
|
||||||
const navigate = useNavigator();
|
const navigate = useNavigator();
|
||||||
const pushMessage = useNotifier();
|
const pushMessage = useNotifier();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<AppProgress>
|
<AppProgress>
|
||||||
{({ setProgressState }) => {
|
{({ setProgressState }) => (
|
||||||
// Obviously, this is workaround to the problem described here:
|
<Query
|
||||||
// https://github.com/DefinitelyTyped/DefinitelyTyped/issues/32588
|
|
||||||
const {
|
|
||||||
children,
|
|
||||||
displayLoader,
|
|
||||||
skip,
|
|
||||||
variables,
|
|
||||||
require
|
|
||||||
} = props as JSX.LibraryManagedAttributes<
|
|
||||||
typeof StrictTypedQuery,
|
|
||||||
typeof props
|
|
||||||
>;
|
|
||||||
return (
|
|
||||||
<StrictTypedQuery
|
|
||||||
fetchPolicy="cache-and-network"
|
fetchPolicy="cache-and-network"
|
||||||
query={query}
|
query={query}
|
||||||
variables={variables}
|
variables={variables}
|
||||||
|
@ -132,9 +119,7 @@ export function TypedQuery<TData, TVariables>(
|
||||||
true
|
true
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
childrenOrNotFound = (
|
childrenOrNotFound = <ErrorPage onBack={() => navigate("/")} />;
|
||||||
<ErrorPage onBack={() => navigate("/")} />
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (displayLoader) {
|
if (displayLoader) {
|
||||||
|
@ -149,11 +134,10 @@ export function TypedQuery<TData, TVariables>(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return childrenOrNotFound;
|
return <>{childrenOrNotFound}</>;
|
||||||
}}
|
|
||||||
</StrictTypedQuery>
|
|
||||||
);
|
|
||||||
}}
|
}}
|
||||||
|
</Query>
|
||||||
|
)}
|
||||||
</AppProgress>
|
</AppProgress>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue