We are using graphql-ruby in one of our internal projects: A Rails API backend serving a React Native Web frontend. I'm curious as to what is considered best practice in regard to handling ordering of returned results.
One option I see is that we provide both order_direction
and field_to_order_by
arguments, and the client must explicitly state each for the query (providing defaults as well, of course).
One way to handle this would be
if (sort_column = args[:sort_by])
if (direction = args[:direction])
users = users.order(sort_column.to_sym => direction.to_sym)
else
users = users.order(sort_column.to_sym) # default sort order
end
end
Another option of course would be to provide all results in a pre-defined direction (ASC
or DESC
) and have the client itself reorder. This seems very inefficient, however. As there's a real dearth of information on how to approach this, I'm curious what's considered best practice.
Any help appreciated!
As best practice ordering results should be made as far down the server as possible (database).
But, as I understand you're questioning between ordering results on the server side (GraphQL API) or on the frontend side (React Native Application), so:
I would recommend providing the client application with an option to get the results in a specific order and handle the sorting on the Server API, that way the client application should only display the results without the need of spending time processing them.