Trouble querying with variable preload and field selections

Ecto friends,

I have a use case I am trying to address that needs to dynamically load one or more association(s) based on user selection. Say I have a schema type T with one or more associations say A1, A2. Depending on field selection I’d like to load the associations dynamically. For example given the following selection criteria

selection = [:id, {:a1, [:id]}, {:a2, [:id]}]

Should yield the following query

|> join(:left, [t], a1 in assoc(t, :a1))
|> preload([t, a1], [a1: a1])
|> join(:left, [t], a2 in assoc(t, :a2))
|> preload([t, a1, a2], [a2: a2])
|> select([t], [:id, {:a1, [:id]}, {:a2, [:id]})

This query works but it’s currently canned as I can seem to find a good way to dynamically inject the joins and preloads using Ecto expressions based on an ad-hoc selection criteria.

Any help/tips on how to best achieve this would be much appreciated.


Sorry, Please disregard!

Turns out just find my 2 new best friends: JoinExpr and QueryExpr.