Skip to content

Commit

Permalink
Support orderBy as array (#5681)
Browse files Browse the repository at this point in the history
closes: #4301

---------

Co-authored-by: Félix Malfait <[email protected]>
  • Loading branch information
AdityaPimpalkar and FelixMalfait authored Jun 14, 2024
1 parent 85fd801 commit 4603999
Show file tree
Hide file tree
Showing 35 changed files with 249 additions and 157 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ export const fetchAllThreadMessagesOperationSignatureFactory: RecordGqlOperation
eq: messageThreadId || '',
},
},
orderBy: {
receivedAt: 'AscNullsLast',
},
orderBy: [
{
receivedAt: 'AscNullsLast',
},
],
limit: 10,
},
fields: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ export const useAttachments = (targetableObject: ActivityTargetableObject) => {
eq: targetableObject.id,
},
},
orderBy: {
createdAt: 'DescNullsFirst',
},
orderBy: [
{
createdAt: 'DescNullsFirst',
},
],
});

return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const mocks: MockedResponse[] = [
query: gql`
query FindManyActivityTargets(
$filter: ActivityTargetFilterInput
$orderBy: ActivityTargetOrderByInput
$orderBy: [ActivityTargetOrderByInput]
$lastCursor: String
$limit: Int
) {
Expand Down Expand Up @@ -103,7 +103,7 @@ const mocks: MockedResponse[] = [
query: gql`
query FindManyActivities(
$filter: ActivityFilterInput
$orderBy: ActivityOrderByInput
$orderBy: [ActivityOrderByInput]
$lastCursor: String
$limit: Int
) {
Expand Down Expand Up @@ -142,7 +142,7 @@ const mocks: MockedResponse[] = [
variables: {
filter: { id: { in: ['234'] } },
limit: undefined,
orderBy: {},
orderBy: [{}],
},
},
result: jest.fn(() => ({
Expand Down Expand Up @@ -178,7 +178,7 @@ describe('useActivities', () => {
useActivities({
targetableObjects: [],
activitiesFilters: {},
activitiesOrderByVariables: {},
activitiesOrderByVariables: [{}],
skip: false,
}),
{ wrapper: Wrapper },
Expand All @@ -202,7 +202,7 @@ describe('useActivities', () => {
{ targetObjectNameSingular: 'company', id: '123' },
],
activitiesFilters: {},
activitiesOrderByVariables: {},
activitiesOrderByVariables: [{}],
skip: false,
});
return { activities, setCurrentWorkspaceMember };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const mocks: MockedResponse[] = [
query: gql`
query FindManyActivityTargets(
$filter: ActivityTargetFilterInput
$orderBy: ActivityTargetOrderByInput
$orderBy: [ActivityTargetOrderByInput]
$lastCursor: String
$limit: Int
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export const usePrepareFindManyActivitiesQuery = () => {
objectRecordsToOverwrite: filteredActivities,
queryVariables: {
...nextFindManyActivitiesQueryFilter,
orderBy: { createdAt: 'DescNullsFirst' },
orderBy: [{ createdAt: 'DescNullsFirst' }],
},
recordGqlFields: FIND_ACTIVITIES_OPERATION_SIGNATURE.fields,
computeReferences: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const useNotes = (targetableObject: ActivityTargetableObject) => {

const { activities, loading } = useActivities({
activitiesFilters: notesQueryVariables.filter ?? {},
activitiesOrderByVariables: notesQueryVariables.orderBy ?? {},
activitiesOrderByVariables: notesQueryVariables.orderBy ?? [{}],
targetableObjects: [targetableObject],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ export const useTasks = ({
const { activities: completeTasksData } = useActivities({
targetableObjects,
activitiesFilters: completedQueryVariables.filter ?? {},
activitiesOrderByVariables: completedQueryVariables.orderBy ?? {},
activitiesOrderByVariables: completedQueryVariables.orderBy ?? [{}],
});

const { activities: incompleteTaskData } = useActivities({
targetableObjects,
activitiesFilters: incompleteQueryVariables.filter ?? {},
activitiesOrderByVariables: incompleteQueryVariables.orderBy ?? {},
activitiesOrderByVariables: incompleteQueryVariables.orderBy ?? [{}],
});

const todayOrPreviousTasks = incompleteTaskData?.filter((task) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { RecordGqlOperationOrderBy } from '@/object-record/graphql/types/RecordGqlOperationOrderBy';

export const FIND_MANY_TIMELINE_ACTIVITIES_ORDER_BY: RecordGqlOperationOrderBy =
{
createdAt: 'DescNullsFirst',
};
[
{
createdAt: 'DescNullsFirst',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ export const makeTimelineActivitiesQueryVariables = ({
in: [...activityIds].sort(sortByAscString),
},
},
orderBy: {
createdAt: 'DescNullsFirst',
},
orderBy: [
{
createdAt: 'DescNullsFirst',
},
],
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ export const useTimelineActivities = (
eq: targetableObject.id,
},
},
orderBy: {
createdAt: 'DescNullsFirst',
},
orderBy: [
{
createdAt: 'DescNullsFirst',
},
],
recordGqlFields: {
id: true,
createdAt: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const sortCachedObjectEdges = ({
orderBy: RecordGqlOperationOrderBy;
readCacheField: ReadFieldFunction;
}) => {
const [orderByFieldName, orderByFieldValue] = Object.entries(orderBy)[0];
const [orderByFieldName, orderByFieldValue] = Object.entries(orderBy[0])[0];
const [orderBySubFieldName, orderBySubFieldValue] =
typeof orderByFieldValue === 'string'
? []
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const query = gql`
export const findManyViewsQuery = gql`
query FindManyViews(
$filter: ViewFilterInput
$orderBy: ViewOrderByInput
$orderBy: [ViewOrderByInput]
$lastCursor: String
$limit: Int
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe('useGetObjectOrderByField', () => {
},
);

expect(result.current).toEqual({
name: { firstName: 'AscNullsLast', lastName: 'AscNullsLast' },
});
expect(result.current).toEqual([
{ name: { firstName: 'AscNullsLast', lastName: 'AscNullsLast' } },
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ describe('getObjectOrderByField', () => {
(item) => item.nameSingular === 'person',
)!;
const res = getOrderByFieldForObjectMetadataItem(objectMetadataItem);
expect(res).toEqual({
name: { firstName: 'AscNullsLast', lastName: 'AscNullsLast' },
});
expect(res).toEqual([
{ name: { firstName: 'AscNullsLast', lastName: 'AscNullsLast' } },
]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,26 @@ export const getOrderByFieldForObjectMetadataItem = (
if (isDefined(labelIdentifierFieldMetadata)) {
switch (labelIdentifierFieldMetadata.type) {
case FieldMetadataType.FullName:
return {
[labelIdentifierFieldMetadata.name]: {
firstName: orderBy ?? 'AscNullsLast',
lastName: orderBy ?? 'AscNullsLast',
return [
{
[labelIdentifierFieldMetadata.name]: {
firstName: orderBy ?? 'AscNullsLast',
lastName: orderBy ?? 'AscNullsLast',
},
},
};
];
default:
return {
[labelIdentifierFieldMetadata.name]: orderBy ?? 'AscNullsLast',
};
return [
{
[labelIdentifierFieldMetadata.name]: orderBy ?? 'AscNullsLast',
},
];
}
} else {
return {
createdAt: orderBy ?? 'DescNullsLast',
};
return [
{
createdAt: orderBy ?? 'DescNullsLast',
},
];
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { OrderBy } from '@/object-metadata/types/OrderBy';

export type RecordGqlOperationOrderBy = {
export type RecordGqlOperationOrderBy = Array<{
[fieldName: string]: OrderBy | { [subFieldName: string]: OrderBy };
};
}>;
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { RecoilRoot } from 'recoil';
import { useFindManyRecordsQuery } from '@/object-record/hooks/useFindManyRecordsQuery';

const expectedQueryTemplate = `
query FindManyPeople($filter: PersonFilterInput, $orderBy: PersonOrderByInput, $lastCursor: String, $limit: Int) {
query FindManyPeople($filter: PersonFilterInput, $orderBy: [PersonOrderByInput], $lastCursor: String, $limit: Int) {
people(filter: $filter, orderBy: $orderBy, first: $limit, after: $lastCursor) {
edges {
node {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export const useGenerateCombinedFindManyRecordsQuery = ({
const orderByPerMetadataItemArray = operationSignatures
.map(
({ objectNameSingular }) =>
`$orderBy${capitalize(objectNameSingular)}: ${capitalize(
`$orderBy${capitalize(objectNameSingular)}: [${capitalize(
objectNameSingular,
)}OrderByInput`,
)}OrderByInput]`,
)
.join(', ');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@ describe('turnSortsIntoOrderBy', () => {
it('should sort by recordPosition if no sorts', () => {
const fields = [{ id: 'field1', name: 'createdAt' }] as FieldMetadataItem[];
expect(turnSortsIntoOrderBy({ ...objectMetadataItem, fields }, [])).toEqual(
{
position: 'AscNullsFirst',
},
[
{
position: 'AscNullsFirst',
},
],
);
});

Expand All @@ -47,10 +49,7 @@ describe('turnSortsIntoOrderBy', () => {
const fields = [{ id: 'field1', name: 'field1' }] as FieldMetadataItem[];
expect(
turnSortsIntoOrderBy({ ...objectMetadataItem, fields }, sorts),
).toEqual({
field1: 'AscNullsFirst',
position: 'AscNullsFirst',
});
).toEqual([{ field1: 'AscNullsFirst' }, { position: 'AscNullsFirst' }]);
});

it('should create OrderByField with multiple sorts', () => {
Expand All @@ -72,11 +71,11 @@ describe('turnSortsIntoOrderBy', () => {
] as FieldMetadataItem[];
expect(
turnSortsIntoOrderBy({ ...objectMetadataItem, fields }, sorts),
).toEqual({
field1: 'AscNullsFirst',
field2: 'DescNullsLast',
position: 'AscNullsFirst',
});
).toEqual([
{ field1: 'AscNullsFirst' },
{ field2: 'DescNullsLast' },
{ position: 'AscNullsFirst' },
]);
});

it('should ignore if field not found', () => {
Expand All @@ -87,9 +86,9 @@ describe('turnSortsIntoOrderBy', () => {
definition: sortDefinition,
},
];
expect(turnSortsIntoOrderBy(objectMetadataItem, sorts)).toEqual({
position: 'AscNullsFirst',
});
expect(turnSortsIntoOrderBy(objectMetadataItem, sorts)).toEqual([
{ position: 'AscNullsFirst' },
]);
});

it('should not return position for remotes', () => {
Expand All @@ -102,6 +101,6 @@ describe('turnSortsIntoOrderBy', () => {
];
expect(
turnSortsIntoOrderBy({ ...objectMetadataItem, isRemote: true }, sorts),
).toEqual({});
).toEqual([]);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,23 @@ export const turnSortsIntoOrderBy = (
): RecordGqlOperationOrderBy => {
const fields: Pick<Field, 'id' | 'name'>[] = objectMetadataItem?.fields ?? [];
const fieldsById = mapArrayToObject(fields, ({ id }) => id);
const sortsOrderBy = Object.fromEntries(
sorts
.map((sort) => {
const correspondingField = fieldsById[sort.fieldMetadataId];
const sortsOrderBy = sorts
.map((sort) => {
const correspondingField = fieldsById[sort.fieldMetadataId];

if (isUndefinedOrNull(correspondingField)) {
return undefined;
}
if (isUndefinedOrNull(correspondingField)) {
return undefined;
}

const direction: OrderBy =
sort.direction === 'asc' ? 'AscNullsFirst' : 'DescNullsLast';
const direction: OrderBy =
sort.direction === 'asc' ? 'AscNullsFirst' : 'DescNullsLast';

return [correspondingField.name, direction];
})
.filter(isDefined),
);
return { [correspondingField.name]: direction };
})
.filter(isDefined);

if (hasPositionField(objectMetadataItem)) {
return {
...sortsOrderBy,
position: 'AscNullsFirst',
};
return [...sortsOrderBy, { position: 'AscNullsFirst' }];
}

return sortsOrderBy;
Expand Down
Loading

0 comments on commit 4603999

Please sign in to comment.