Fixed mass deletion page size. (#6275)

Fixed when page size was not the same as the one used for calculating
number of pages
This commit is contained in:
Lucas Bordeau
2024-07-16 11:45:29 +02:00
committed by GitHub
parent 26bffce23b
commit 341328fdf8
3 changed files with 26 additions and 11 deletions

View File

@ -34,20 +34,20 @@ export const thirdRequestLastCursor = peopleMockWithIdsOnly.edges[mockPageSize *
export const variablesFirstRequest = { export const variablesFirstRequest = {
filter: undefined, filter: undefined,
limit: undefined, limit: mockPageSize,
orderBy: undefined orderBy: undefined
}; };
export const variablesSecondRequest = { export const variablesSecondRequest = {
filter: undefined, filter: undefined,
limit: undefined, limit: mockPageSize,
orderBy: undefined, orderBy: undefined,
lastCursor: firstRequestLastCursor lastCursor: firstRequestLastCursor
}; };
export const variablesThirdRequest = { export const variablesThirdRequest = {
filter: undefined, filter: undefined,
limit: undefined, limit: mockPageSize,
orderBy: undefined, orderBy: undefined,
lastCursor: secondRequestLastCursor lastCursor: secondRequestLastCursor
} }

View File

@ -1,12 +1,14 @@
import { useApolloClient } from '@apollo/client'; import { useApolloClient } from '@apollo/client';
import { triggerDeleteRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect'; import { triggerDeleteRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect';
import { apiConfigState } from '@/client-config/states/apiConfigState';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem'; import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems'; import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache'; import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache';
import { DEFAULT_MUTATION_BATCH_SIZE } from '@/object-record/constants/DefaultMutationBatchSize'; import { DEFAULT_MUTATION_BATCH_SIZE } from '@/object-record/constants/DefaultMutationBatchSize';
import { useDeleteManyRecordsMutation } from '@/object-record/hooks/useDeleteManyRecordsMutation'; import { useDeleteManyRecordsMutation } from '@/object-record/hooks/useDeleteManyRecordsMutation';
import { getDeleteManyRecordsMutationResponseField } from '@/object-record/utils/getDeleteManyRecordsMutationResponseField'; import { getDeleteManyRecordsMutationResponseField } from '@/object-record/utils/getDeleteManyRecordsMutationResponseField';
import { useRecoilValue } from 'recoil';
import { isDefined } from '~/utils/isDefined'; import { isDefined } from '~/utils/isDefined';
import { sleep } from '~/utils/sleep'; import { sleep } from '~/utils/sleep';
import { capitalize } from '~/utils/string/capitalize'; import { capitalize } from '~/utils/string/capitalize';
@ -24,6 +26,11 @@ type DeleteManyRecordsOptions = {
export const useDeleteManyRecords = ({ export const useDeleteManyRecords = ({
objectNameSingular, objectNameSingular,
}: useDeleteOneRecordProps) => { }: useDeleteOneRecordProps) => {
const apiConfig = useRecoilValue(apiConfigState);
const mutationPageSize =
apiConfig?.mutationMaximumAffectedRecords ?? DEFAULT_MUTATION_BATCH_SIZE;
const apolloClient = useApolloClient(); const apolloClient = useApolloClient();
const { objectMetadataItem } = useObjectMetadataItem({ const { objectMetadataItem } = useObjectMetadataItem({
@ -48,16 +55,14 @@ export const useDeleteManyRecords = ({
idsToDelete: string[], idsToDelete: string[],
options?: DeleteManyRecordsOptions, options?: DeleteManyRecordsOptions,
) => { ) => {
const numberOfBatches = Math.ceil( const numberOfBatches = Math.ceil(idsToDelete.length / mutationPageSize);
idsToDelete.length / DEFAULT_MUTATION_BATCH_SIZE,
);
const deletedRecords = []; const deletedRecords = [];
for (let batchIndex = 0; batchIndex < numberOfBatches; batchIndex++) { for (let batchIndex = 0; batchIndex < numberOfBatches; batchIndex++) {
const batchIds = idsToDelete.slice( const batchIds = idsToDelete.slice(
batchIndex * DEFAULT_MUTATION_BATCH_SIZE, batchIndex * mutationPageSize,
(batchIndex + 1) * DEFAULT_MUTATION_BATCH_SIZE, (batchIndex + 1) * mutationPageSize,
); );
const deletedRecordsResponse = await apolloClient.mutate({ const deletedRecordsResponse = await apolloClient.mutate({

View File

@ -20,6 +20,7 @@ export const useFetchAllRecordIds = <T>({
objectNameSingular, objectNameSingular,
filter, filter,
orderBy, orderBy,
limit: pageSize,
recordGqlFields: { id: true }, recordGqlFields: { id: true },
}); });
@ -49,12 +50,17 @@ export const useFetchAllRecordIds = <T>({
const remainingPages = Math.ceil(remainingCount / pageSize); const remainingPages = Math.ceil(remainingCount / pageSize);
let lastCursor = firstQueryResult?.pageInfo.endCursor ?? ''; let lastCursor = firstQueryResult?.pageInfo.endCursor ?? null;
for (let pageIndex = 0; pageIndex < remainingPages; pageIndex++) {
if (lastCursor === null) {
break;
}
for (let i = 0; i < remainingPages; i++) {
const rawResult = await fetchMore?.({ const rawResult = await fetchMore?.({
variables: { variables: {
lastCursor: lastCursor, lastCursor: lastCursor,
limit: pageSize,
}, },
}); });
@ -64,7 +70,11 @@ export const useFetchAllRecordIds = <T>({
recordIdSet.add(edge.node.id); recordIdSet.add(edge.node.id);
} }
lastCursor = fetchMoreResult.pageInfo.endCursor ?? ''; if (fetchMoreResult.pageInfo.hasNextPage === false) {
break;
}
lastCursor = fetchMoreResult.pageInfo.endCursor ?? null;
} }
const recordIds = Array.from(recordIdSet); const recordIds = Array.from(recordIdSet);