[Flexible-schema] Refactor gql query runner to emit api event before processing to gql types (#8596)

Fixes https://github.com/twentyhq/twenty/issues/8300

## Context
API events were created too late and were already formatted as Gql
responses (including nesting with edges/node/type + formatting that
should not exist in an event payload). This PR moves the emit logic to
the resolver where we actually do the DB query
Note: Also added RESTORED events
This commit is contained in:
Weiko
2024-11-21 18:11:28 +01:00
committed by GitHub
parent 8f5515cab3
commit 1c04b2b0b7
46 changed files with 1251 additions and 1049 deletions

View File

@ -26,7 +26,7 @@ export class ApiEventEmitterService {
objectMetadata: objectMetadataItem,
properties: {
before: null,
after: this.removeGraphQLAndNestedProperties(record),
after: record,
},
})),
workspaceId: authContext.workspace.id,
@ -52,10 +52,8 @@ export class ApiEventEmitterService {
objectMetadataNameSingular: objectMetadataItem.nameSingular,
action: DatabaseEventAction.UPDATED,
events: records.map((record) => {
const before = this.removeGraphQLAndNestedProperties(
mappedExistingRecords[record.id],
);
const after = this.removeGraphQLAndNestedProperties(record);
const before = mappedExistingRecords[record.id];
const after = record;
const diff = objectRecordChangedValues(
before,
after,
@ -93,7 +91,7 @@ export class ApiEventEmitterService {
recordId: record.id,
objectMetadata: objectMetadataItem,
properties: {
before: this.removeGraphQLAndNestedProperties(record),
before: record,
after: null,
},
};
@ -102,6 +100,29 @@ export class ApiEventEmitterService {
});
}
public emitRestoreEvents<T extends ObjectRecord>(
records: T[],
authContext: AuthContext,
objectMetadataItem: ObjectMetadataInterface,
): void {
this.workspaceEventEmitter.emitDatabaseBatchEvent({
objectMetadataNameSingular: objectMetadataItem.nameSingular,
action: DatabaseEventAction.RESTORED,
events: records.map((record) => {
return {
userId: authContext.user?.id,
recordId: record.id,
objectMetadata: objectMetadataItem,
properties: {
before: null,
after: record,
},
};
}),
workspaceId: authContext.workspace.id,
});
}
public emitDestroyEvents<T extends ObjectRecord>(
records: T[],
authContext: AuthContext,
@ -116,7 +137,7 @@ export class ApiEventEmitterService {
recordId: record.id,
objectMetadata: objectMetadataItem,
properties: {
before: this.removeGraphQLAndNestedProperties(record),
before: record,
after: null,
},
};
@ -124,26 +145,4 @@ export class ApiEventEmitterService {
workspaceId: authContext.workspace.id,
});
}
private removeGraphQLAndNestedProperties<T extends ObjectRecord>(record: T) {
if (!record) {
return {};
}
const sanitizedRecord = {};
for (const [key, value] of Object.entries(record)) {
if (value && typeof value === 'object' && value['edges']) {
continue;
}
if (key === '__typename') {
continue;
}
sanitizedRecord[key] = value;
}
return sanitizedRecord;
}
}