Skip to content

The open alternative to Salesforce, designed for AI.

1.1 KB
Raw Blame
import { useApolloCoreClient } from '@/object-metadata/hooks/useApolloCoreClient';
import { getGroupByAggregateQueryName } from '@/object-record/record-aggregate/utils/getGroupByAggregateQueryName';
import { getAggregateQueryName } from '@/object-record/utils/getAggregateQueryName';

export const useRefetchAggregateQueries = () => {
  const apolloCoreClient = useApolloCoreClient();

  const refetchAggregateQueries = async ({
    objectMetadataNamePlural,
  }: {
    objectMetadataNamePlural: string;
  }) => {
    const queryName = getAggregateQueryName(objectMetadataNamePlural);

    const groupByAggregateQueryName = getGroupByAggregateQueryName({
      objectMetadataNamePlural,
    });

    await apolloCoreClient.refetchQueries({
      include: [queryName, groupByAggregateQueryName],
      // Matching by name also picks up skipped (standby) queries, such as the
      // footer cell of every table column that has no aggregate operation
      onQueryUpdated: (observableQuery) =>
        observableQuery.options.fetchPolicy !== 'standby',
    });
  };

  return {
    refetchAggregateQueries,
  };
};