Where to put "cross context preload"

When faced with these sorts of questions, I settled on only ever passing query functions that exist in the same context as the function that will eventually interact with the Repo.

Blog.list_posts(fn query ->
  query
  |> Blog.preload_post_likes()
end)

I’m not sure this is the right answer, but I did it for predictability, explicitness, and to protect against changes within the context. From outside the context, I don’t have to care if preload_post_likes directly specifies the query or if it delegates out to something in SocialMedia as that may change in the future.

2 Likes