Skip to content

Commit

Permalink
Fixed mass deletion page size. (#6275)
Browse files Browse the repository at this point in the history
Fixed when page size was not the same as the one used for calculating
number of pages
  • Loading branch information
lucasbordeau authored Jul 16, 2024
1 parent 26bffce commit 341328f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ export const thirdRequestLastCursor = peopleMockWithIdsOnly.edges[mockPageSize *

export const variablesFirstRequest = {
filter: undefined,
limit: undefined,
limit: mockPageSize,
orderBy: undefined
};

export const variablesSecondRequest = {
filter: undefined,
limit: undefined,
limit: mockPageSize,
orderBy: undefined,
lastCursor: firstRequestLastCursor
};

export const variablesThirdRequest = {
filter: undefined,
limit: undefined,
limit: mockPageSize,
orderBy: undefined,
lastCursor: secondRequestLastCursor
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { useApolloClient } from '@apollo/client';

import { triggerDeleteRecordsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerDeleteRecordsOptimisticEffect';
import { apiConfigState } from '@/client-config/states/apiConfigState';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
import { useGetRecordFromCache } from '@/object-record/cache/hooks/useGetRecordFromCache';
import { DEFAULT_MUTATION_BATCH_SIZE } from '@/object-record/constants/DefaultMutationBatchSize';
import { useDeleteManyRecordsMutation } from '@/object-record/hooks/useDeleteManyRecordsMutation';
import { getDeleteManyRecordsMutationResponseField } from '@/object-record/utils/getDeleteManyRecordsMutationResponseField';
import { useRecoilValue } from 'recoil';
import { isDefined } from '~/utils/isDefined';
import { sleep } from '~/utils/sleep';
import { capitalize } from '~/utils/string/capitalize';
Expand All @@ -24,6 +26,11 @@ type DeleteManyRecordsOptions = {
export const useDeleteManyRecords = ({
objectNameSingular,
}: useDeleteOneRecordProps) => {
const apiConfig = useRecoilValue(apiConfigState);

const mutationPageSize =
apiConfig?.mutationMaximumAffectedRecords ?? DEFAULT_MUTATION_BATCH_SIZE;

const apolloClient = useApolloClient();

const { objectMetadataItem } = useObjectMetadataItem({
Expand All @@ -48,16 +55,14 @@ export const useDeleteManyRecords = ({
idsToDelete: string[],
options?: DeleteManyRecordsOptions,
) => {
const numberOfBatches = Math.ceil(
idsToDelete.length / DEFAULT_MUTATION_BATCH_SIZE,
);
const numberOfBatches = Math.ceil(idsToDelete.length / mutationPageSize);

const deletedRecords = [];

for (let batchIndex = 0; batchIndex < numberOfBatches; batchIndex++) {
const batchIds = idsToDelete.slice(
batchIndex * DEFAULT_MUTATION_BATCH_SIZE,
(batchIndex + 1) * DEFAULT_MUTATION_BATCH_SIZE,
batchIndex * mutationPageSize,
(batchIndex + 1) * mutationPageSize,
);

const deletedRecordsResponse = await apolloClient.mutate({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export const useFetchAllRecordIds = <T>({
objectNameSingular,
filter,
orderBy,
limit: pageSize,
recordGqlFields: { id: true },
});

Expand Down Expand Up @@ -49,12 +50,17 @@ export const useFetchAllRecordIds = <T>({

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?.({
variables: {
lastCursor: lastCursor,
limit: pageSize,
},
});

Expand All @@ -64,7 +70,11 @@ export const useFetchAllRecordIds = <T>({
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);
Expand Down

0 comments on commit 341328f

Please sign in to comment.