-
-
Notifications
You must be signed in to change notification settings - Fork 437
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
Allow SQL expressions in @orderBy
#2056
Comments
It could be necessary do have arbitrary ordering in the SQL clause, for example ordering by month and ignoring the year: SUBSTR(created_at, 5, 2) ASC Adding special support for one specific use case seems to be too narrow of a solution. |
I get the point, but as you just showed, it could be really useful to handle a custom operator ('SUBSTR', 'ISNULL'...) in the builder, I mean, at least for relations (otherwise, the @builder is enough for this need) |
These are not operators though, they are functions. As shown in the example, they can take multiple arguments and are thus not simply addded through some boolean flags. Actually, they can be composed in arbitrarily complex ways - too much power to expose directly to the end user (risk of SQL injection, resource exhaustion). I could see allowing the definition of custom order expressions in type Query {
users(
orderBy: _ @orderBy(expressions: [{ name: "month", builder: "UserBuilder@createdAtMonth" }])
): [User!]! @all
}
users(orderBy: { column: MONTH, direction: ASC }) { id } |
Well, looks great, but for this use I could just do a Here is my concrete business example (so that you can understand) : Of course, I need to first order the non-null value in the And I just can't find a way to Do someone know a simple way to achieve this ? |
You can always order on the client 😉 My feature proposal could be extended to work on relations too. Adapted to your example: type Query {
brands(
orderBy: _ @orderBy(
relations: [
{ relation: "offer", expressions: [{ name: "ORDER_NULL_LAST", builder: "Offer@orderNullLast" }])
]
)
): [User!]! @all
}
brands(orderBy: { offer: { column: ORDER_NULL_LAST, direction: ASC }}) { ... } |
Looks great to me! Thanks for your work. public function orderNullLast(Builder $builder) : Builder
{
return $builder->orderByRaw('ISNULL(`order`) ASC, `order` ASC');
} ? |
@orderBy
Looks like we have a good idea of how this could work. Some of the finer details will have to be fleshed out in the implementation:
I am not looking to implement this myself at the moment, but am open to reviewing a PR or doing it as paid work. |
What problem does this feature proposal attempt to solve?
This feature would allow requests to be sorted by null column values.
In example : to order a list of DateTime in SQL, providing
ISNULL(created_at) ASC, created_at ASC
allows date to be ordered right after non-null date values.
For the moment, it's only possible to do it by setting a @builder directive, and chain a raw SQL ordering. So it's ok when ordering on a simple table, but this is not as easy with a relation ordering
Which possible solutions should be considered?
In the OrderByDirection enum, maybe add 2 attributes ? NULL_ASC and NULL_DESC ? I'll stick to what is the simplest implementation to you guys
(and once again, thank's for the package)
The text was updated successfully, but these errors were encountered: