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!