Skip to content

Commit

Permalink
Bug fix: "order by" crash on non real keys when list first field is a…
Browse files Browse the repository at this point in the history
… relationship (#4791)
  • Loading branch information
josemf authored Feb 8, 2021
1 parent 208722a commit 45b047a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/many-feet-rule.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@keystonejs/adapter-knex': patch
---

Bug fix: "order by" crash on non real keys when list first field is a relationship
20 changes: 14 additions & 6 deletions packages/adapter-knex/lib/adapter-knex.js
Original file line number Diff line number Diff line change
Expand Up @@ -648,17 +648,25 @@ class QueryBuilder {
// SELECT ... ORDER BY <orderField>
const [orderField, orderDirection] = this._getOrderFieldAndDirection(orderBy);
const sortKey = listAdapter.fieldAdaptersByPath[orderField].sortKey || orderField;
this._query.orderBy(sortKey, orderDirection);
if (listAdapter.realKeys.includes(sortKey)) {
this._query.orderBy(sortKey, orderDirection);
}
}
if (sortBy !== undefined) {
// SELECT ... ORDER BY <orderField>[, <orderField>, ...]
this._query.orderBy(
sortBy.map(s => {
const [orderField, orderDirection] = this._getOrderFieldAndDirection(s);
const sortKey = listAdapter.fieldAdaptersByPath[orderField].sortKey || orderField;
sortBy
.map(s => {
const [orderField, orderDirection] = this._getOrderFieldAndDirection(s);
const sortKey = listAdapter.fieldAdaptersByPath[orderField].sortKey || orderField;

return { column: sortKey, order: orderDirection };
})
if (listAdapter.realKeys.includes(sortKey)) {
return { column: sortKey, order: orderDirection };
} else {
return undefined;
}
})
.filter(s => typeof s !== 'undefined')
);
}
}
Expand Down

1 comment on commit 45b047a

@vercel
Copy link

@vercel vercel bot commented on 45b047a Feb 8, 2021

Choose a reason for hiding this comment

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

Please sign in to comment.