Absinthe dataloader and multi table query

I’m making a booking system where a profile has “availability” which defines the times someone can book them. I’m wanting to use dataloader to load profile(s) and their availability. But their availability is determined by multiple tables which isn’t just a relationship that Dataloader.Ecto source would know about.

To determine availability there’s 3 tables:

# defines their standard recurring availability
schema "standard_time_slots" do
  field(:iso_dow, :integer)
  field(:start_time, :time)
  field(:end_time, :time)

  belongs_to :profile, Profile
end
# defines a specific date time to include/block out
schema "specific_time_slots" do
  field :start_datetime, :naive_datetime
  field :end_datetime, :naive_datetime
  field :type, :string

  belongs_to :profile, Profile
end
schema "bookings" do
  field :start_datetime, :naive_datetime
  field :end_datetime, :naive_datetime

  belongs_to :profile, Profile
  belongs_to :user, User
end

Profile

schema "profiles" do
  has_many :standard_time_slots, StandardTimeSlot
  has_many :specific_time_slots, SpecificTimeSlot
  has_many :bookings, Booking
end

If I wasn’t using absinthe/graphql I could just run a query using the 3 tables to work out the availability (maybe even save it as a db function or view :thinking:) and then just call it when needed in the controller.

With absinthe/graphql I’m not entirely sure how to approach this one and any help would be appreciated

2 Likes

If anyone stumbles across this thread the way I solved this was to create a schema backed by a db view. So just run the usual mix phx.gen.context and in the migration create your view.