Skip to content

Commit

Permalink
Fix issues with Apollo cache in workflow module (#7569)
Browse files Browse the repository at this point in the history
Fixes #7523
  • Loading branch information
Devessier authored Oct 11, 2024
1 parent 3761fbf commit 521dd04
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { ApolloCache, StoreObject } from '@apollo/client';

import { triggerUpdateRelationsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerUpdateRelationsOptimisticEffect';
import { CachedObjectRecordQueryVariables } from '@/apollo/types/CachedObjectRecordQueryVariables';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
import { RecordGqlRefEdge } from '@/object-record/cache/types/RecordGqlRefEdge';
import { isObjectRecordConnectionWithRefs } from '@/object-record/cache/utils/isObjectRecordConnectionWithRefs';
import { RecordGqlNode } from '@/object-record/graphql/types/RecordGqlNode';
import { isDefined } from '~/utils/isDefined';
import { parseApolloStoreFieldName } from '~/utils/parseApolloStoreFieldName';

export const triggerDeleteRecordsOptimisticEffect = ({
cache,
Expand All @@ -24,7 +22,7 @@ export const triggerDeleteRecordsOptimisticEffect = ({
fields: {
[objectMetadataItem.namePlural]: (
rootQueryCachedResponse,
{ DELETE, readField, storeFieldName },
{ readField },
) => {
const rootQueryCachedResponseIsNotACachedObjectRecordConnection =
!isObjectRecordConnectionWithRefs(
Expand All @@ -38,11 +36,6 @@ export const triggerDeleteRecordsOptimisticEffect = ({

const rootQueryCachedObjectRecordConnection = rootQueryCachedResponse;

const { fieldVariables: rootQueryVariables } =
parseApolloStoreFieldName<CachedObjectRecordQueryVariables>(
storeFieldName,
);

const recordIdsToDelete = recordsToDelete.map(({ id }) => id);

const cachedEdges = readField<RecordGqlRefEdge[]>(
Expand All @@ -65,14 +58,6 @@ export const triggerDeleteRecordsOptimisticEffect = ({
if (nextCachedEdges.length === cachedEdges?.length)
return rootQueryCachedObjectRecordConnection;

// TODO: same as in update, should we trigger DELETE ?
if (
isDefined(rootQueryVariables?.first) &&
cachedEdges?.length === rootQueryVariables.first
) {
return DELETE;
}

return {
...rootQueryCachedObjectRecordConnection,
edges: nextCachedEdges,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ export const RecordShowPageWorkflowHeader = ({
);

return deleteOneWorkflowVersion({
workflowId: workflowWithCurrentVersion.id,
workflowVersionId: workflowWithCurrentVersion.currentVersion.id,
});
}}
Expand All @@ -69,9 +68,10 @@ export const RecordShowPageWorkflowHeader = ({
workflowWithCurrentVersion,
);

return activateWorkflowVersion(
workflowWithCurrentVersion.currentVersion.id,
);
return activateWorkflowVersion({
workflowVersionId: workflowWithCurrentVersion.currentVersion.id,
workflowId: workflowWithCurrentVersion.id,
});
}}
/>
) : workflowWithCurrentVersion?.currentVersion?.status === 'ACTIVE' ? (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ export const RecordShowPageWorkflowVersionHeader = ({
Icon={IconPower}
disabled={isWaitingForWorkflowVersion}
onClick={() => {
return activateWorkflowVersion(workflowVersion.id);
return activateWorkflowVersion({
workflowVersionId: workflowVersion.id,
workflowId: workflowVersion.workflowId,
});
}}
/>
) : workflowVersion?.status === 'ACTIVE' ? (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,43 +1,75 @@
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
import { ApolloClient, useApolloClient, useMutation } from '@apollo/client';

import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useFindOneRecordQuery } from '@/object-record/hooks/useFindOneRecordQuery';
import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache';
import { ACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/activateWorkflowVersion';
import { WorkflowVersion } from '@/workflow/types/Workflow';
import {
ActivateWorkflowVersionMutation,
ActivateWorkflowVersionMutationVariables,
} from '~/generated/graphql';

export const useActivateWorkflowVersion = () => {
const apolloClient = useApolloClient();

const apolloMetadataClient = useApolloMetadataClient();
const apolloClient = useApolloClient();
const [mutate] = useMutation<
ActivateWorkflowVersionMutation,
ActivateWorkflowVersionMutationVariables
>(ACTIVATE_WORKFLOW_VERSION, {
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
});

const { findOneRecordQuery: findOneWorkflowVersionQuery } =
useFindOneRecordQuery({
const { objectMetadataItem: objectMetadataItemWorkflowVersion } =
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
});

const activateWorkflowVersion = async (workflowVersionId: string) => {
const activateWorkflowVersion = async ({
workflowVersionId,
workflowId,
}: {
workflowVersionId: string;
workflowId: string;
}) => {
await mutate({
variables: {
workflowVersionId,
},
});
update: () => {
modifyRecordFromCache({
cache: apolloClient.cache,
recordId: workflowVersionId,
objectMetadataItem: objectMetadataItemWorkflowVersion,
fieldModifiers: {
status: () => 'ACTIVE',
},
});

await apolloClient.query({
query: findOneWorkflowVersionQuery,
variables: {
objectRecordId: workflowVersionId,
const cacheSnapshot = apolloClient.cache.extract();
const allWorkflowVersions: Array<WorkflowVersion> = Object.values(
cacheSnapshot,
).filter(
(item) =>
item.__typename === 'WorkflowVersion' &&
item.workflowId === workflowId,
);

for (const workflowVersion of allWorkflowVersions) {
apolloClient.cache.modify({
id: apolloClient.cache.identify(workflowVersion),
fields: {
status: () => {
return workflowVersion.id !== workflowVersionId &&
workflowVersion.status === 'ACTIVE'
? 'ARCHIVED'
: workflowVersion.status;
},
},
});
}
},
fetchPolicy: 'network-only',
});
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
import { useApolloMetadataClient } from '@/object-metadata/hooks/useApolloMetadataClient';
import { ApolloClient, useApolloClient, useMutation } from '@apollo/client';

import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useFindOneRecordQuery } from '@/object-record/hooks/useFindOneRecordQuery';
import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache';
import { DEACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/deactivateWorkflowVersion';
import {
ActivateWorkflowVersionMutation,
ActivateWorkflowVersionMutationVariables,
} from '~/generated/graphql';

export const useDeactivateWorkflowVersion = () => {
const apolloClient = useApolloClient();

const apolloMetadataClient = useApolloMetadataClient();
const apolloClient = useApolloClient();
const [mutate] = useMutation<
ActivateWorkflowVersionMutation,
ActivateWorkflowVersionMutationVariables
>(DEACTIVATE_WORKFLOW_VERSION, {
client: apolloMetadataClient ?? ({} as ApolloClient<any>),
});

const { findOneRecordQuery: findOneWorkflowVersionQuery } =
useFindOneRecordQuery({
const { objectMetadataItem: objectMetadataItemWorkflowVersion } =
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
});

Expand All @@ -30,14 +30,16 @@ export const useDeactivateWorkflowVersion = () => {
variables: {
workflowVersionId,
},
});

await apolloClient.query({
query: findOneWorkflowVersionQuery,
variables: {
objectRecordId: workflowVersionId,
update: () => {
modifyRecordFromCache({
cache: apolloClient.cache,
recordId: workflowVersionId,
objectMetadataItem: objectMetadataItemWorkflowVersion,
fieldModifiers: {
status: () => 'DEACTIVATED',
},
});
},
fetchPolicy: 'network-only',
});
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,17 @@
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { useDeleteOneRecord } from '@/object-record/hooks/useDeleteOneRecord';
import { useFindOneRecordQuery } from '@/object-record/hooks/useFindOneRecordQuery';
import { useApolloClient } from '@apollo/client';

export const useDeleteOneWorkflowVersion = () => {
const apolloClient = useApolloClient();

const { findOneRecordQuery: findOneWorkflowRecordQuery } =
useFindOneRecordQuery({
objectNameSingular: CoreObjectNameSingular.Workflow,
});

const { deleteOneRecord } = useDeleteOneRecord({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
});

const deleteOneWorkflowVersion = async ({
workflowId,
workflowVersionId,
}: {
workflowId: string;
workflowVersionId: string;
}) => {
await deleteOneRecord(workflowVersionId);

await apolloClient.query({
query: findOneWorkflowRecordQuery,
variables: {
objectRecordId: workflowId,
},
fetchPolicy: 'network-only',
});
};

return { deleteOneWorkflowVersion };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ export const useWorkflowWithCurrentVersion = (
createdAt: 'DescNullsLast',
},
],
limit: 1,
skip: !isDefined(workflowId),
});

Expand All @@ -50,7 +49,16 @@ export const useWorkflowWithCurrentVersion = (
return undefined;
}

const currentVersion = mostRecentWorkflowVersions?.[0];
const draftVersion = mostRecentWorkflowVersions.find(
(workflowVersion) => workflowVersion.status === 'DRAFT',
);
const latestVersion = mostRecentWorkflowVersions[0];

const currentVersion = draftVersion ?? latestVersion;

if (!isDefined(currentVersion)) {
return undefined;
}

return {
...workflow,
Expand Down

0 comments on commit 521dd04

Please sign in to comment.