我尝试使用Absinthe#Batch进行简单查询,但正在执行Ecto查询的函数不会返回任何结果。
我添加了一些跟踪来计算db上的记录。 预计会始终返回1,但不会。
解析器:
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
助手方法:
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
一些追踪:
# Before batch: 1
# Before batch: 1
# Before batch: 1
# On the helper method: 0
# Inside batch: 1
# Inside batch: 1
# Inside batch: 1
答案 0 :(得分:1)
正如benwilson512提到here,问题是数据库连接不是shared。
所以,我修正了它:
before do
Ecto.Adapters.SQL.Sandbox.mode(Repo, {:shared, self()})
end