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

fix: add create cursor util #9086

Merged
merged 3 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
@@ -1,6 +1,5 @@
import { ApolloCache, StoreObject } from '@apollo/client';
import { isNonEmptyString } from '@sniptt/guards';
import { Buffer } from 'buffer';

import { triggerUpdateRelationsOptimisticEffect } from '@/apollo/optimistic-effect/utils/triggerUpdateRelationsOptimisticEffect';
import { ObjectMetadataItem } from '@/object-metadata/types/ObjectMetadataItem';
Expand All @@ -11,6 +10,7 @@ import { RecordGqlNode } from '@/object-record/graphql/types/RecordGqlNode';
import { isRecordMatchingFilter } from '@/object-record/record-filter/utils/isRecordMatchingFilter';

import { CachedObjectRecordQueryVariables } from '@/apollo/types/CachedObjectRecordQueryVariables';
import { createCursor } from '@/apollo/utils/createCursor';
import { isDefined } from '~/utils/isDefined';
import { parseApolloStoreFieldName } from '~/utils/parseApolloStoreFieldName';

Expand Down Expand Up @@ -128,13 +128,7 @@ export const triggerCreateRecordsOptimisticEffect = ({
);

if (recordToCreateReference && !recordAlreadyInCache) {
const cursor = Buffer.from(
JSON.stringify({
position: recordToCreate.position,
id: recordToCreate.id,
}),
'utf-8',
).toString('base64');
const cursor = createCursor(recordToCreate);

const edge = {
__typename: getEdgeTypename(objectMetadataItem.nameSingular),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { createCursor } from '@/apollo/utils/createCursor';
import { ObjectRecord } from '@/object-record/types/ObjectRecord';

describe('createCursor', () => {
it('should create a cursor with id only', () => {
const record: ObjectRecord = { __typename: 'ObjectRecord', id: '123' };
const cursor = createCursor(record);
const decoded = JSON.parse(Buffer.from(cursor, 'base64').toString('utf-8'));
magrinj marked this conversation as resolved.
Show resolved Hide resolved

expect(decoded).toEqual({ id: '123' });
});

it('should create a cursor with id and position', () => {
const record: ObjectRecord = {
__typename: 'ObjectRecord',
id: '123',
position: 1,
};
const cursor = createCursor(record);
const decoded = JSON.parse(Buffer.from(cursor, 'base64').toString('utf-8'));

expect(decoded).toEqual({ id: '123', position: 1 });
});

it('should create a cursor with id and position as 0', () => {
const record: ObjectRecord = {
__typename: 'ObjectRecord',
id: '123',
position: 0,
};
const cursor = createCursor(record);
const decoded = JSON.parse(Buffer.from(cursor, 'base64').toString('utf-8'));

expect(decoded).toEqual({ id: '123', position: 0 });
});

it('should create a cursor with id and ignore extra fields', () => {
const record: ObjectRecord = {
__typename: 'ObjectRecord',
id: '123',
position: 1,
extra: 'extra',
};
const cursor = createCursor(record);
const decoded = JSON.parse(Buffer.from(cursor, 'base64').toString('utf-8'));

expect(decoded).toEqual({ id: '123', position: 1 });
});

it('should throw an error if record does not have an id', () => {
const record = { position: 1 } as any;

expect(() => createCursor(record)).toThrow();
});
});
21 changes: 21 additions & 0 deletions packages/twenty-front/src/modules/apollo/utils/createCursor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { ObjectRecord } from '@/object-record/types/ObjectRecord';
import { isDefined } from '~/utils/isDefined';

export const createCursor = (record: ObjectRecord) => {
if (!('id' in record) || !isDefined(record.id)) {
magrinj marked this conversation as resolved.
Show resolved Hide resolved
throw new Error('Record does not have an id');
}

const payload: {
id: string;
position?: number;
} = {
id: record.id,
};

if ('position' in record) {
magrinj marked this conversation as resolved.
Show resolved Hide resolved
payload.position = record.position;
}

return Buffer.from(JSON.stringify(payload), 'utf-8').toString('base64');
};
Loading