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

Support orderBy as array #5681

Merged
merged 17 commits into from
Jun 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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
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 @@ -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 @@ -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 @@ -11,7 +11,7 @@ import { FieldMetadataType } from '~/generated/graphql';
const query = gql`
query CombinedFindManyRecords(
$filterNameSingular: NameSingularFilterInput
$orderByNameSingular: NameSingularOrderByInput
$orderByNameSingular: [NameSingularOrderByInput]
$lastCursorNameSingular: String
$limitNameSingular: Int
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ query FindMany${capitalize(
objectMetadataItem.namePlural,
)}($filter: ${capitalize(
objectMetadataItem.nameSingular,
)}FilterInput, $orderBy: ${capitalize(
)}FilterInput, $orderBy: [${capitalize(
objectMetadataItem.nameSingular,
)}OrderByInput, $lastCursor: String, $limit: Int) {
)}OrderByInput], $lastCursor: String, $limit: Int) {
${
objectMetadataItem.namePlural
}(filter: $filter, orderBy: $orderBy, first: $limit, after: $lastCursor){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,7 @@ describe('ArgsStringFactory', () => {

it('when orderBy is present, should return an array of objects', () => {
const args = {
orderBy: {
id: 'AscNullsFirst',
name: 'AscNullsFirst',
},
orderBy: [{ id: 'AscNullsFirst' }, { name: 'AscNullsFirst' }],
};

argsAliasCreate.mockReturnValue(args);
Expand All @@ -85,11 +82,11 @@ describe('ArgsStringFactory', () => {

it('when orderBy is present with position criteria, should return position at the end of the list', () => {
const args = {
orderBy: {
position: 'AscNullsFirst',
id: 'AscNullsFirst',
name: 'AscNullsFirst',
},
orderBy: [
{ position: 'AscNullsFirst' },
{ id: 'AscNullsFirst' },
{ name: 'AscNullsFirst' },
],
};

argsAliasCreate.mockReturnValue(args);
Expand All @@ -103,11 +100,11 @@ describe('ArgsStringFactory', () => {

it('when orderBy is present with position in the middle, should return position at the end of the list', () => {
const args = {
orderBy: {
id: 'AscNullsFirst',
position: 'AscNullsFirst',
name: 'AscNullsFirst',
},
orderBy: [
{ id: 'AscNullsFirst' },
{ position: 'AscNullsFirst' },
{ name: 'AscNullsFirst' },
],
};

argsAliasCreate.mockReturnValue(args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,16 @@ export class ArgsStringFactory {
typeof computedArgs[key] === 'object' &&
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove trailing comma and space, if present.

computedArgs[key] !== null
) {
// If it's an object (and not null), stringify it
argsString += `${key}: ${this.buildStringifiedObject(
key,
computedArgs[key],
)}, `;
if (key === 'orderBy') {
argsString += `${key}: ${this.buildStringifiedObjectFromArray(
computedArgs[key],
)}, `;
} else {
// If it's an object (and not null), stringify it
argsString += `${key}: ${stringifyWithoutKeyQuote(
computedArgs[key],
)}, `;
}
} else {
// For other types (number, boolean), add as is
argsString += `${key}: ${computedArgs[key]}, `;
Expand All @@ -55,22 +60,39 @@ export class ArgsStringFactory {
return argsString;
}

private buildStringifiedObject(
key: string,
obj: Record<string, any>,
private buildStringifiedObjectFromArray(
AdityaPimpalkar marked this conversation as resolved.
Show resolved Hide resolved
keyValuePairArray: Array<Record<string, any>>,
): string {
Copy link
Contributor

Choose a reason for hiding this comment

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

The sorting function's parameters should be named more descriptively to clarify their roles.

// PgGraphql is expecting the orderBy argument to be an array of objects
if (key === 'orderBy') {
const orderByString = Object.keys(obj)
.sort((_, b) => {
return b === 'position' ? -1 : 0;
})
.map((orderByKey) => `{${orderByKey}: ${obj[orderByKey]}}`)
.join(', ');
AdityaPimpalkar marked this conversation as resolved.
Show resolved Hide resolved
let argsString = '';
// if position argument is present we want to put it at the very last
const positionObj = keyValuePairArray.find((obj) =>
Object.hasOwnProperty.call(obj, 'position'),
);

// we put filter only if positionObj is defined otherwise we loop through normal array
const orderByKeyValuePairs = positionObj
? keyValuePairArray.filter(
(obj) => !Object.hasOwnProperty.call(obj, 'position'),
)
: keyValuePairArray;

for (const obj of orderByKeyValuePairs) {
for (const key in obj) {
argsString += `{${key}: ${obj[key]}}, `;
}
}

Copy link
Contributor

Choose a reason for hiding this comment

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

Remove the check for trailing comma and space as it is redundant with the new join logic.

return `[${orderByString}]`;
if (positionObj) {
for (const key in positionObj) {
argsString += `{${key}: ${positionObj[key]}}, `;
}
}

if (argsString.endsWith(', ')) {
argsString = argsString.slice(0, -2);
}

return stringifyWithoutKeyQuote(obj);
return `[${argsString}]`;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ describe('getResolverArgs', () => {
before: { type: GraphQLString, isNullable: true },
after: { type: GraphQLString, isNullable: true },
filter: { kind: InputTypeDefinitionKind.Filter, isNullable: true },
orderBy: { kind: InputTypeDefinitionKind.OrderBy, isNullable: true },
orderBy: {
kind: InputTypeDefinitionKind.OrderBy,
isNullable: true,
isArray: true,
},
limit: { type: GraphQLInt, isNullable: true },
},
findOne: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const getResolverArgs = (
orderBy: {
kind: InputTypeDefinitionKind.OrderBy,
isNullable: true,
isArray: true,
},
};
case 'findOne':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class FindManyQueryFactory {
return `
query FindMany${capitalize(objectNamePlural)}(
$filter: ${objectNameSingular}FilterInput,
$orderBy: ${objectNameSingular}OrderByInput,
$orderBy: [${objectNameSingular}OrderByInput],
$lastCursor: String,
$limit: Int = 60
) {
Expand Down
Loading