LastDragon-ru / lara-asp

Awesome Set of Packages for Laravel
MIT License
11 stars 1 forks source link

`@sortBy`: Nulls ordering (`NULLS FIRST`/`NULLS LAST`) #110

Closed LastDragon-ru closed 9 months ago

LastDragon-ru commented 9 months ago

NULLs order different in different databases. Sometimes we need to change it. There is no default/built-it support in Laravel nor Lighthouse.

Useful links:

There are two parts:

For the second part, I think we can add nulls extra operator that will change default behaviour. The problem (also related to default config) is: there is no way to know if the column/expression is nullable or not. This is mean that NULLS FIRST/NULLS LAST will be added into all ORDER BY clauses. It may be slow for databases without native NULLS FIRST/NULLS LAST support (MySQL and SQL Server). Unfortunately, I have no idea how to solve it.

The proposed syntax is shown below. Probably both fields should be required (to avoid ambiguity).

query {
    comments(order: [
        {nulls: {asc: First, desc: Last }},
        {user: {name: Asc}}
        {text: Desc}
    ])
}

Or better (it will affect only one clause):

query {
    comments(order: [
        {nullsFirst: {user: {name: Asc}}}
        {nullsLast: {text: Desc}}
    ])
}

Related to https://github.com/nuwave/lighthouse/issues/2486, https://github.com/nuwave/lighthouse/issues/2056

LastDragon-ru commented 9 months ago

Default implementation and default (global) settings

Available in the main branch 🎉 Any feedback greatly appreciated.

LastDragon-ru commented 9 months ago

The following syntax was added and will be available in the next major (v6) 🥳

query {
    comments(order: [
        {nullsFirst: {user: {name: Asc}}}
        {nullsLast: {text: Desc}}
    ])
}