vega-frontend-monorepo/apps/trading/components/page-query-container/index.tsx
2022-03-30 13:05:41 +02:00

26 lines
819 B
TypeScript

import type { OperationVariables, QueryHookOptions } from '@apollo/client';
import { useQuery } from '@apollo/client';
import type { DocumentNode } from 'graphql';
import type { ReactNode } from 'react';
import { AsyncRenderer } from '@vegaprotocol/ui-toolkit';
interface PageQueryContainerProps<TData, TVariables> {
query: DocumentNode;
options?: QueryHookOptions<TData, TVariables>;
children: (data: TData) => ReactNode;
}
export const PageQueryContainer = <TData, TVariables = OperationVariables>({
query,
options,
children,
}: PageQueryContainerProps<TData, TVariables>) => {
const { data, loading, error } = useQuery<TData, TVariables>(query, options);
return (
<AsyncRenderer<TData> loading={loading} error={error} data={data}>
{(data) => children(data)}
</AsyncRenderer>
);
};