lighthouse icon indicating copy to clipboard operation
lighthouse copied to clipboard

Improve `@orderBy` directive to allow developer to specify null behaviour

Open jdjoshuadavison opened this issue 2 years ago • 2 comments

What problem does this feature proposal attempt to solve?

Sometimes it's important to be able to specify whether nulls always come first or always come last.

As a developer I should be able to specify whether nulls are returned in their default position, always come first or always come last.

This option could be extended to the user as an additional argument on the order argument - if it is, then it should be optional.

Which possible solutions should be considered?

As far as I'm aware, Laravel doesn't have a solution for this, so it would require using raw queries (orderByRaw), and I don't know the best way to handle that to ensure support for all the databases Laravel support (which is currently MariaDB 10.10+, MySQL 5.7+, PostgreSQL 11.0+, SQLite 3.8.8+, SQL Server 2017+)

Here's how to order by nulls first or nulls last in PostgreSQL and MySQL:

PostgreSQL You can simply add NULLS LAST or NULLS FIRST to the end of the raw queries.

ORDER BY column ASC NULLS LAST

MySQL Mysql doesn't have an explicit way to handle it, but I would just use IS NULL on my sorting column first, then add my sorting column as a secondary sort:

ORDER BY column IS NULL ASC, column ASC

(Use IS NULL ASC for nulls last and IS NULL DESC for nulls first)

jdjoshuadavison avatar Dec 14 '23 10:12 jdjoshuadavison

Sorting in PHP may be easier, but require a rework of how @orderBy is applied by Lighthouse.

spawnia avatar Dec 15 '23 12:12 spawnia

Hey hey hey! I've added the ability to set default order of NULLs for my @sortBy directive 🥳 Any feedback really appreciated!

Technical details can be found in the DatabaseSorter class (if someone wants to improve it or port it to Lighthouse).

PS: The query support will be added later.

LastDragon-ru avatar Jan 08 '24 09:01 LastDragon-ru