Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed mass deletion page size. #6275

Merged
merged 3 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading