41 lines
923 B
TypeScript
41 lines
923 B
TypeScript
import {
|
|
ApolloError,
|
|
ApolloQueryResult,
|
|
MaybeMasked,
|
|
OperationVariables,
|
|
useQuery
|
|
} from '@apollo/client';
|
|
import { DocumentNode } from 'graphql';
|
|
|
|
interface UsePaginatedQueryProps {
|
|
query: DocumentNode;
|
|
input: {
|
|
[key: string]: any; // for the custom inputs
|
|
filter: {
|
|
terms?: string | null;
|
|
offset?: number | null;
|
|
size?: number | null;
|
|
};
|
|
};
|
|
}
|
|
|
|
interface UsePaginatedQueryReturn<T> {
|
|
data?: MaybeMasked<T> | undefined;
|
|
loading: boolean;
|
|
error: ApolloError | undefined;
|
|
refetch: (
|
|
variables?: Partial<OperationVariables> | undefined
|
|
) => Promise<ApolloQueryResult<MaybeMasked<T>>>;
|
|
}
|
|
|
|
export function usePaginatedQuery<T>(props: UsePaginatedQueryProps): UsePaginatedQueryReturn<T> {
|
|
const { data, loading, error, refetch } = useQuery<T>(props.query, {
|
|
variables: { input: props.input }
|
|
});
|
|
return {
|
|
data,
|
|
loading,
|
|
error,
|
|
refetch
|
|
};
|
|
}
|