I am trying to order an ecto association by a given column. But as seen on this old ecto issue this does not seem possible, instead, it’s possible to preload with order.
I have games, with many nodes. I would like to sort the nodes association by the index column.
My Game schema is
schema "games" do
...
has_many :nodes, Node
timestamps()
end
and here my node schema
@primary_key false
schema "nodes" do
belongs_to :game, Game
field :index, :integer, primary_key: true
...
timestamps()
end
The problem I am having is with Absinthe, because I am using the association to resolve the list of nodes belonging to a game. But they are not ordered like I would… by the index column…
in the Schema.Types
# Has_many
@desc "List of nodes"
field :nodes, list_of(:node), resolve: assoc(:nodes)
Is it possible to use something like this?
has_many :nodes, Node, order_by: :index
or is it possible to use something like that?
field :nodes, list_of(:node), resolve: specific_resolver_function_with_ordered_nodes_inside
Is this still supported in Absinthe v1.4.x? I see it still in the docs but when I tried to do the same thing in my code, it still returns the assoc unsorted in the JSON response.