nestjs-query
nestjs-query copied to clipboard
[Question / FR] Nested Order By
Awesome library you got here guys, exactly what i was looking for. The only thing it seems to be missing is nested order-by.
Is there a way to implement this functionality? Suppose i have a nested query for customers and i want to sort them by their bank name (which lies in a joined table), something like this:
query {
customers(sorting: [{field: "bank.name", direction: ASC}]) {
edges {
node {
name
email
bank {
name
}
}
}
}
}
Right now it seems only first-level order-by is supported
If this isn't doable, how hard do you think would it be for me to add this functionality to your library myself by contributing or forking it? Is it even possible with dataloader under the hood? Could there be a workaround?
This is not currently supported, I don't think it should be too difficult to add for the existing query services, as it should be building off of the current ability to filter on relations. Feel free to dive it and try to tackle it if you would like.
Thanks!
@nebo-digital I'm currently researching this. At the surface it looks easy enough to implement, however, it comes with its own unique set of challenges that I'm trying to work through.
- The current version of typescript does not support a type safe field attribute. With version
4.1.0
I'll be able to make this type safe withtemplate literal types
. - Supporting this with keyset paging will be difficult and I will need to play with this more in order to support it. In short
nestjs-query
does not eagerly fetch relation values, in the scenario where we have amany to many
orone to many
relation I think we'll need to fetch the max sort value in order to create a unique cursor for a record. I'm going to dive deeper on this to see if there is a work around but this is the current hold up. - The field value in a sort cannot contain a
.
in it my current approach is to replace all.
with a__
. For example if you wanted to sort aTodoItem
by thesubTasks.title
it would be[{ field: subTasks__title, direction: ASC }]
. This syntax is not as nice assubTasks.title
but I think it will do for now. See here for more details
I'm not giving up on this but I wanted to a give an update that it will not be a quick fix as I thought.
I'm going to hold off on this until typescript 4.1
is released with templated types.
@nebo-digital this might help you as a temporary solution: https://github.com/doug-martin/nestjs-query/issues/752#issuecomment-746296613
It could be a really nice feature, a usual use-case is when you have a ManyToMany relation with an explicit entity to handle the relation.
Parent <ManyToOne> RelationHolder <OneToMany> Child
You would like to call RelationHolders
with sort fields from the Child
.
Happy to help debug this if needed ;)
@doug-martin Hi man, this project is awesome and it already saved me ton of time. Is there any progress on this? Can I help somehow?
Also keen to have an elegant solution to this issue. Have to use a workaround for now.
Is anybody working on this feature at the moment? @doug-martin I recently came across some very interesting new possibilities with Typescript 4.5 and am curious to know how your progress is going
@doug-martin Any updates on this? It would be great to have this feature
Also highly interested in this feature, would be great to have it.