-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Support orderBy as array #5681
Changes from 4 commits
4cebaf2
5386056
77651d5
7c0665f
d6108bf
7753958
0bc9401
3636bd2
4c053b7
85ed7a3
12440f8
e2a38d4
81c35a6
fdb9f48
1410906
ca8264d
203dca4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,11 +36,16 @@ export class ArgsStringFactory { | |
typeof computedArgs[key] === 'object' && | ||
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]}, `; | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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]}}, `; | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}]`; | ||
} | ||
} |
There was a problem hiding this comment.
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.