Files
twenty/packages/twenty-front/src/modules/workflow/hooks/useDeactivateWorkflowVersion.ts
T
Charles Bochet 9e21e55db4 Prevent leak between /metadata and /graphql GQL schemas (#17845)
## Fix resolver schema leaking between `/metadata` and `/graphql`
endpoints

### Summary
- Patch `@nestjs/graphql` to support a `resolverSchemaScope` option that
filters resolvers at both schema generation and runtime, preventing
cross-endpoint leaking
- Introduce `@CoreResolver()` and `@MetadataResolver()` decorators to
explicitly scope each resolver to its endpoint
- Move most resolvers (auth, billing, workspace, user, etc.) to the
metadata schema where the frontend expects them; only workflow and
timeline calendar/messaging resolvers remain on `/graphql`
- Fix frontend `SSEQuerySubscribeEffect` to use the default (metadata)
Apollo client instead of the core client

### Problem
NestJS GraphQL's module-based resolver discovery traverses transitive
imports, causing resolvers from `/metadata` modules to leak into the
`/graphql` schema and vice versa. This made the schemas unpredictable
and tightly coupled to module import order.

### Approach
- Added `resolverSchemaScope` to `GqlModuleOptions` via a patch on
`@nestjs/graphql`, filtering in both `filterResolvers()` (runtime
binding) and `getAllCtors()` (schema generation)
- Each resolver is explicitly decorated with `@CoreResolver()` or
`@MetadataResolver()`
- Organized decorator, constant, and type files under `graphql-config/`
following project conventions


Core GQL Schema: (see: no more fields!)
<img width="827" height="894" alt="image"
src="https://github.com/user-attachments/assets/668f3f0f-485e-43f0-92be-4345aeccacb6"
/>

Metadata GQL Schema (see no more getTimelineCalendarEventsFromCompany)
<img width="827" height="894" alt="image"
src="https://github.com/user-attachments/assets/443913db-e5fe-4161-b0e7-4a971cc80a71"
/>
2026-02-11 10:05:24 +00:00

129 lines
4.4 KiB
TypeScript

import { useMutation } from '@apollo/client';
import { triggerUpdateRecordOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerUpdateRecordOptimisticEffect';
import { useApolloCoreClient } from '@/object-metadata/hooks/useApolloCoreClient';
import { useObjectMetadataItem } from '@/object-metadata/hooks/useObjectMetadataItem';
import { useObjectMetadataItems } from '@/object-metadata/hooks/useObjectMetadataItems';
import { CoreObjectNameSingular } from '@/object-metadata/types/CoreObjectNameSingular';
import { getRecordFromCache } from '@/object-record/cache/utils/getRecordFromCache';
import { modifyRecordFromCache } from '@/object-record/cache/utils/modifyRecordFromCache';
import { useObjectPermissions } from '@/object-record/hooks/useObjectPermissions';
import { useUpsertRecordsInStore } from '@/object-record/record-store/hooks/useUpsertRecordsInStore';
import { DEACTIVATE_WORKFLOW_VERSION } from '@/workflow/graphql/mutations/deactivateWorkflowVersion';
import {
type Workflow,
type WorkflowStatus,
type WorkflowVersion,
} from '@/workflow/types/Workflow';
import { isDefined } from 'twenty-shared/utils';
import {
type DeactivateWorkflowVersionMutation,
type DeactivateWorkflowVersionMutationVariables,
} from '~/generated/graphql';
export const useDeactivateWorkflowVersion = () => {
const apolloCoreClient = useApolloCoreClient();
const { objectPermissionsByObjectMetadataId } = useObjectPermissions();
const { objectMetadataItems } = useObjectMetadataItems();
const [mutate] = useMutation<
DeactivateWorkflowVersionMutation,
DeactivateWorkflowVersionMutationVariables
>(DEACTIVATE_WORKFLOW_VERSION, {
client: apolloCoreClient,
});
const { upsertRecordsInStore } = useUpsertRecordsInStore();
const { objectMetadataItem: objectMetadataItemWorkflow } =
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.Workflow,
});
const { objectMetadataItem: objectMetadataItemWorkflowVersion } =
useObjectMetadataItem({
objectNameSingular: CoreObjectNameSingular.WorkflowVersion,
});
const deactivateWorkflowVersion = async ({
workflowVersionId,
}: {
workflowVersionId: string;
}) => {
await mutate({
variables: {
workflowVersionId,
},
update: () => {
modifyRecordFromCache({
cache: apolloCoreClient.cache,
recordId: workflowVersionId,
objectMetadataItem: objectMetadataItemWorkflowVersion,
fieldModifiers: {
status: () => 'DEACTIVATED',
},
});
const cacheSnapshot = apolloCoreClient.cache.extract();
const workflowVersion: WorkflowVersion | undefined = Object.values(
cacheSnapshot,
).find(
(item) =>
item.__typename === 'WorkflowVersion' &&
item.id === workflowVersionId,
);
if (!isDefined(workflowVersion)) {
return;
}
triggerUpdateRecordOptimisticEffect({
cache: apolloCoreClient.cache,
objectMetadataItem: objectMetadataItemWorkflowVersion,
currentRecord: workflowVersion,
updatedRecord: {
...workflowVersion,
status: 'DEACTIVATED',
},
objectMetadataItems,
objectPermissionsByObjectMetadataId,
upsertRecordsInStore,
});
const cachedWorkflow = getRecordFromCache<Workflow>({
objectMetadataItem: objectMetadataItemWorkflow,
cache: apolloCoreClient.cache,
objectMetadataItems,
objectPermissionsByObjectMetadataId,
recordId: workflowVersion.workflowId,
});
const newStatuses = new Set(
[...(cachedWorkflow?.statuses ?? []), 'DEACTIVATED'].filter(
(status) => status !== 'ACTIVE',
),
);
if (isDefined(cachedWorkflow)) {
modifyRecordFromCache({
cache: apolloCoreClient.cache,
recordId: workflowVersion.workflowId,
objectMetadataItem: objectMetadataItemWorkflow,
fieldModifiers: {
statuses: () => Array.from(newStatuses),
},
});
upsertRecordsInStore({
partialRecords: [
{
...cachedWorkflow,
statuses: Array.from(newStatuses) as WorkflowStatus[],
},
],
});
}
},
});
};
return { deactivateWorkflowVersion };
};