Skip to content
This repository has been archived by the owner on Sep 3, 2021. It is now read-only.

Commit

Permalink
Merge pull request #184 from whostolebenfrog/order-by-arrays
Browse files Browse the repository at this point in the history
Support arrays of orderBy arguments
  • Loading branch information
johnymontana authored Jan 21, 2019
2 parents 7559802 + 8749db8 commit 7f674cb
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ export const relationFieldOnNodeType = ({
tailParams,
temporalClauses
}) => {
const arrayFilterParams = _.pickBy(filterParams, param =>
Array.isArray(param.value)
const arrayFilterParams = _.pickBy(
filterParams,
(param, keyName) => Array.isArray(param.value) && !('orderBy' === keyName)
);

const allParams = innerFilterParams(filterParams, temporalArgs);
Expand Down
27 changes: 17 additions & 10 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,24 +336,31 @@ export function computeSkipLimit(selection, variableValues) {
return `[${offset}..${parseInt(offset) + parseInt(first)}]`;
}

function orderByStatement(resolveInfo, orderByVar) {
const splitIndex = orderByVar.lastIndexOf('_');
const order = orderByVar.substring(splitIndex + 1);
const orderBy = orderByVar.substring(0, splitIndex);
const { variableName } = typeIdentifiers(resolveInfo.returnType);
return ` ${variableName}.${orderBy} ${order === 'asc' ? 'ASC' : 'DESC'} `;
}

export const computeOrderBy = (resolveInfo, selection) => {
const orderByVar = argumentValue(
const orderByArgs = argumentValue(
resolveInfo.operation.selectionSet.selections[0],
'orderBy',
resolveInfo.variableValues
);

if (orderByVar == undefined) {
if (orderByArgs == undefined) {
return '';
} else {
const splitIndex = orderByVar.lastIndexOf('_');
const order = orderByVar.substring(splitIndex + 1);
const orderBy = orderByVar.substring(0, splitIndex);
const { variableName } = typeIdentifiers(resolveInfo.returnType);
return ` ORDER BY ${variableName}.${orderBy} ${
order === 'asc' ? 'ASC' : 'DESC'
} `;
}

const orderByArray = Array.isArray(orderByArgs) ? orderByArgs : [orderByArgs];
const orderByStatments = orderByArray.map(orderByVar =>
orderByStatement(resolveInfo, orderByVar)
);

return ' ORDER BY' + orderByStatments.join(',');
};

export const possiblySetFirstId = ({ args, statements, params }) => {
Expand Down

0 comments on commit 7f674cb

Please sign in to comment.