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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple, independent, and ordered sortKey and orderBy directions #421

Merged
merged 2 commits into from
Aug 16, 2020

Conversation

airhorns
Copy link
Collaborator

@airhorns airhorns commented Jul 30, 2020

The API for sortKey right now lets you sort by multiple keys but only in one direction for all of them, which is a bit limiting, especially if you want to let API consumers pass arbitrary sorts or lists of sorts to paginated connections. orderBy lets you sort by as many keys as you like with independent directions per key -- I think sortKey should support the same!

This changes the API for sortKey from this:

{
  order: "asc",
  key: ["created_at", "id"]
}

to this:

[
  {order: "asc", key: "created_at"},
  {order: "asc", key: "id"},
]

which is more expressive, allowing different orders per key, and keeps the key property of ordering the keys so we know which to sort by first. orderBy's object syntax kind of has that implicitly from object iteration order, but this makes it explicit which I think is better and less surprising.

The API is backwards compatible, if an object is passed to sortKey the old logic that applies the one order to all the keys will still run, and the new format only kicks in if an array is passed. 馃巿

I considered using orderBy's same style of {[key: string]: Order} but I think this is more explicit. Relying on object insertion order to power super key API semantics like how things are sorted seems a little haphazard to me, and it means that the SQL output becomes super dependent on how an ordering thunk is implemented, not just it's output. To be honest I'd consider switching orderBy to be consistent and use the same explicitly ordered array of key/order pairs. An array of strings would also be good to support things like #321 and #313. If #418 means a big breaking change, it might be a good time to swap in that behaviour. Again, if an array were used, I think it'd be possible to make the API change backwards compatible way so people migrating to 3.0 didn't have to change their code just to make my API design OCD satisfied, but for new people integrating join-monster things would be that much better.

@airhorns airhorns changed the title WIP Multiple sort orders WIP Multiple, independent sortKey directions Jul 30, 2020
@airhorns airhorns force-pushed the multiple-sort-orders branch 2 times, most recently from 37a49e5 to 1442a7e Compare July 30, 2020 16:09
@ennoente ennoente self-assigned this Jul 30, 2020
@ennoente ennoente requested a review from lorensr July 30, 2020 16:25
@ennoente
Copy link
Contributor

Hey there,
I like the idea! Gonna check it out and test it.
We should definitely upgrade/migrate to graphql 15

@airhorns airhorns force-pushed the multiple-sort-orders branch 3 times, most recently from fd5556f to 0054ad2 Compare July 31, 2020 17:32
@lorensr
Copy link
Member

lorensr commented Jul 31, 2020

This is great! I think I'd deprecate the object syntax in the docs for now, and then in a later release, remove the code for it.

@airhorns
Copy link
Collaborator Author

airhorns commented Aug 1, 2020

Do you folks want to land a similar change for orderBy too for a 3.0 release?

@lorensr
Copy link
Member

lorensr commented Aug 1, 2020

Yeah, would be good for the recommended APIs to match

@airhorns airhorns force-pushed the multiple-sort-orders branch 3 times, most recently from 512297c to 5b24a7b Compare August 1, 2020 03:51
@airhorns airhorns changed the title WIP Multiple, independent sortKey directions WIP Multiple, independent, and sorted sortKey and orderBy directions Aug 1, 2020
@airhorns airhorns changed the title WIP Multiple, independent, and sorted sortKey and orderBy directions WIP Multiple, independent, and ordered sortKey and orderBy directions Aug 1, 2020
@airhorns
Copy link
Collaborator Author

airhorns commented Aug 1, 2020

Ok. this is updated to change both sortKey and orderBy to look a bit similar. The new APIs for both use {column: string, direction: "asc" | "desc"}[] arrays to specify the sort / order verbosely, but both still accept the old style too. I think for orderBy the old style is still really handy for simple cases where you only want to order by one column, so I left the documentation for that around, establishing it as a shorthand instead of deprecated. For sortKey however I don't think it's quite the same. I have a couple test fails to chase down but it's fully working on Postgres!

@airhorns airhorns marked this pull request as ready for review August 3, 2020 14:41
@airhorns airhorns changed the title WIP Multiple, independent, and ordered sortKey and orderBy directions Multiple, independent, and ordered sortKey and orderBy directions Aug 3, 2020
@airhorns
Copy link
Collaborator Author

This sucker is good to go!

@alexbbt alexbbt self-requested a review August 12, 2020 20:25
Copy link
Contributor

@alexbbt alexbbt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me!

docs/CHANGELOG.md Outdated Show resolved Hide resolved
@ennoente ennoente merged commit 57b3700 into join-monster:master Aug 16, 2020
@airhorns airhorns deleted the multiple-sort-orders branch August 16, 2020 21:59
@lorensr
Copy link
Member

lorensr commented Aug 17, 2020

Thanks again airhorns! Published in join-monster@3.0.0-alpha.2

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

Successfully merging this pull request may close these issues.

None yet

4 participants