Skip to content
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

Improve orderBy directive to allow developer to specify null behaviour #2486

Open
jdjoshuadavison opened this issue Dec 14, 2023 · 2 comments
Labels
enhancement A feature or improvement

Comments

@jdjoshuadavison
Copy link

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)

@spawnia spawnia added the enhancement A feature or improvement label Dec 15, 2023
@spawnia
Copy link
Collaborator

spawnia commented Dec 15, 2023

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

@LastDragon-ru
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A feature or improvement
Projects
None yet
Development

No branches or pull requests

3 participants