Ash FrameworkAF
Ash Framework3y ago
8 replies
drumusician

sort many_to_many

I thought I had asked this question before, but can't seem to find it...

It seems like a very common use case, but not sure how to go about this.

Is it possible to sort a many_to_many relationship based on a field in the join table.

ie. if I have a Playlist:
defmodule Playlist do
  relationships do
    has_many :playlist_entries, PlaylistEntry
    many_to_many :items, Item
  end
end

and an Item:
defmodule Item do
...
end


And a PlaylistEntry:

defmodule PlaylistEntry
  relationships do
    belongs_to :item, Item
    belongs_to :playlist, Playlist
  end

  attributes do
    attribute :position, :integer
  end
end


And I want to simply load this in graphql by just adding the items:

query  {
  playlist {
    items {
      name
      //ordered by position in playlist_entry
    }
  }
}


I know that in graphql not all values that are not needed in the read are selected, but apart from graphQL is there a way to get this sorting happening.

Thanks all!
Was this page helpful?