Support orderBy as array (#5681)

closes: #4301

---------

Co-authored-by: Félix Malfait <felix.malfait@gmail.com>
This commit is contained in:
Aditya Pimpalkar
2024-06-14 10:23:37 +01:00
committed by GitHub
parent 85fd801480
commit 4603999d1c
35 changed files with 249 additions and 157 deletions

View File

@ -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);
@ -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);
@ -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);

View File

@ -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.buildStringifiedOrderBy(
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,30 @@ export class ArgsStringFactory {
return argsString;
}
private buildStringifiedObject(
key: string,
obj: Record<string, any>,
private buildStringifiedOrderBy(
keyValuePairArray: Array<Record<string, any>>,
): string {
// 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(', ');
if (
keyValuePairArray.length !== 0 &&
Object.keys(keyValuePairArray[0]).length === 0
) {
return `[]`;
}
// if position argument is present we want to put it at the very last
let orderByString = keyValuePairArray
.sort((_, obj) => (Object.hasOwnProperty.call(obj, 'position') ? -1 : 0))
.map((obj) => {
const [key] = Object.keys(obj);
const value = obj[key];
return `[${orderByString}]`;
return `{${key}: ${value}}`;
})
.join(', ');
if (orderByString.endsWith(', ')) {
orderByString = orderByString.slice(0, -2);
}
return stringifyWithoutKeyQuote(obj);
return `[${orderByString}]`;
}
}

View File

@ -16,9 +16,9 @@ export enum OrderByDirection {
DescNullsLast = 'DescNullsLast',
}
export type RecordOrderBy = {
export type RecordOrderBy = Array<{
[Property in keyof Record]?: OrderByDirection;
};
}>;
export interface RecordDuplicateCriteria {
objectName: string;

View File

@ -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: {

View File

@ -38,6 +38,7 @@ export const getResolverArgs = (
orderBy: {
kind: InputTypeDefinitionKind.OrderBy,
isNullable: true,
isArray: true,
},
};
case 'findOne':