Reputation: 548
I'm trying to get working a simple query with Absinthe#Batch, but the function where the Ecto query is being executed doesn't return any result.
I added some tracing to count the records on the db. It is expected to return always 1, but doesn't.
The Resolver:
def get_cards_for_stage(stage, _args, _ctx) do
IO.puts "Before batch: #{OfferSid.Repo.aggregate(from(p in "pipeline_cards"), :count, :id)}"
batch({__MODULE__, :pipeline_cards_by_stage_ids}, stage.id, fn batch_results ->
IO.puts "Inside batch: #{OfferSid.Repo.aggregate(from(p in "pipeline_cards"), :count, :id)}"
{:ok, Map.get(batch_results, stage.id)}
end)
end
The Helper method:
def pipeline_cards_by_stage_ids(_args, stages_ids) do
IO.puts "On the helper method: #{OfferSid.Repo.aggregate(Ecto.Query.from(p in "pipeline_cards"), :count, :id)}"
cards = OfferSid.pipeline_cards_by_stage_ids(stages_ids)
Map.new(cards, fn s -> {s.pipeline_stage_id, s} end)
end
Some tracing:
# Before batch: 1
# Before batch: 1
# Before batch: 1
# On the helper method: 0
# Inside batch: 1
# Inside batch: 1
# Inside batch: 1
Upvotes: 0
Views: 219
Reputation: 548
As benwilson512 mentioned here, the problem was the db connection wasn't shared.
So, I fixed it adding:
before do
Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
end
Upvotes: 1