Here is some contexts code I wrote, for a graphql backend…
def list_player_games_query(%Player{id: player_id}, args) do
from(
g in list_games_query(args),
where: g.white_id == ^player_id or g.black_id == ^player_id
)
end
def list_player_games(%Player{} = player, args \\ []) do
player
|> list_player_games_query(args)
|> Repo.all
end
That is how it is organized in Crafting GraphQL. Again, I don’t pretend it is the best way… but It makes also sense to have queries inside contexts.
When You use the generator, it puts an alias to Repo, and imports Ecto.Query in the context.