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

Refactor triggerUpdateRelationsOptimisticEffect to compute relationship from Metadatas #9815

Merged

Conversation

prastoin
Copy link
Contributor

@prastoin prastoin commented Jan 23, 2025

Introduction

At the moment the relationships are inferred from the record data structure instead of its metadatas
We should refactor the code that computes or not the necessity to detach a relation on a mutation

We've refactored the isObjectRecordConnection method to be consuming a relationDefintion instead of "typeChecking" at the runtime the data structure using zod validation schema

Related to #9580

Copy link

github-actions bot commented Jan 23, 2025

Fails
🚫

node failed.

Log

�[31mError: �[39m SyntaxError: Unexpected token C in JSON at position 0
    at JSON.parse (<anonymous>)
�[90m    at parseJSONFromBytes (node:internal/deps/undici/undici:5584:19)�[39m
�[90m    at successSteps (node:internal/deps/undici/undici:5555:27)�[39m
�[90m    at fullyReadBody (node:internal/deps/undici/undici:1665:9)�[39m
�[90m    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)�[39m
�[90m    at async specConsumeBody (node:internal/deps/undici/undici:5564:7)�[39m
danger-results://tmp/danger-results-0f4d90fd.json

Generated by 🚫 dangerJS against afb243d

@prastoin prastoin force-pushed the 9580-2-refactor-trigger-update-relations-optimistic-effect branch from c80242c to b09bdb1 Compare January 23, 2025 17:11
@prastoin prastoin force-pushed the 9580-2-refactor-trigger-update-relations-optimistic-effect branch from 4ac212c to cf640e6 Compare January 23, 2025 17:16
@prastoin prastoin marked this pull request as ready for review January 23, 2025 17:21
@prastoin prastoin marked this pull request as draft January 23, 2025 17:21
Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

This PR refactors the relationship handling in optimistic updates by switching from record data structure inference to metadata-based relationship computation, improving type safety and reliability.

  • Added new computeRecordsToAttachAndDetach function in /packages/twenty-front/src/modules/apollo/optimistic-effect/utils/triggerUpdateRelationsOptimisticEffect.ts to determine relationship changes based on metadata
  • Implemented proper relationship direction handling (ONE_TO_MANY, MANY_TO_ONE, etc.) using RelationDefinitionType enum
  • Enhanced type safety with explicit typing for function arguments and return values in relationship computation
  • Added support for handling both connection-based and direct reference relationships through metadata

1 file(s) reviewed, 1 comment(s)
Edit PR Review Bot Settings | Greptile

@prastoin prastoin force-pushed the 9580-2-refactor-trigger-update-relations-optimistic-effect branch from bc81a18 to fcf12a6 Compare January 24, 2025 10:19
@@ -7,6 +7,7 @@ export const getRecordsFromRecordConnection = <T extends ObjectRecord>({
}: {
recordConnection: RecordGqlConnection;
}): T[] => {
Copy link
Contributor Author

@prastoin prastoin Jan 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: Unless I'm mistaken this function could return undefined

}: {
  recordConnection: RecordGqlConnection;
}): T[] | undefined => {

if (shouldAttachSourceToAllTargets) {
targetRecordsToAttachTo.forEach((targetRecordToAttachTo) =>
// Could it be an invariant ?
if (isDefined(updatedSourceRecord)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: This could be an invariant, but implicitly quite though to tell TypeScript

objectMetadataItems,
});
// Could it be an invariant ?
} else if (isDefined(currentSourceRecord)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remark: This could be an invariant, but implicitly quite though to tell TypeScript

@prastoin prastoin force-pushed the 9580-2-refactor-trigger-update-relations-optimistic-effect branch 2 times, most recently from 746ae2d to 8cf4115 Compare January 24, 2025 14:17
@prastoin prastoin force-pushed the 9580-2-refactor-trigger-update-relations-optimistic-effect branch from 8cf4115 to cca0c89 Compare January 24, 2025 14:32
@prastoin prastoin marked this pull request as ready for review January 24, 2025 14:35
@prastoin
Copy link
Contributor Author

prastoin commented Jan 24, 2025

About to create an issue regarding the possible cache corruption due to ObjectRecord and RecordGqlNode inclusion, after the related critical bug has been resolved

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR Summary

(updates since last review)

This PR continues the refactoring of relationship handling by simplifying the logic and adding robust testing for relationship type determination.

  • Added comprehensive test coverage in isObjectRecordConnection.test.ts for all relation definition types (MANY_TO_MANY, ONE_TO_MANY, etc.)
  • Simplified isObjectRecordConnection to make decisions purely based on relation direction metadata
  • Added edge case handling for array injection in cache through extractTargetRecordsFromRelation function
  • Improved error handling with explicit checks for unknown relation directions

3 file(s) reviewed, 4 comment(s)
Edit PR Review Bot Settings | Greptile

@prastoin prastoin requested a review from lucasbordeau January 24, 2025 14:36
@prastoin prastoin merged commit 95c7726 into main Jan 24, 2025
50 checks passed
@prastoin prastoin deleted the 9580-2-refactor-trigger-update-relations-optimistic-effect branch January 24, 2025 16:24
@charlesBochet
Copy link
Member

LGTM too

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants