我的 Absinthe graphql 模式中有一个对象,如下所示:
object :match do
field(:id, non_null(:id))
field(:opponent, non_null(:string))
@desc "The number of votes that have been cast so far."
field(:vote_count, non_null(:integer), resolve: &MatchResolver.get_vote_count/2)
# etc...
end
我正在使用一个解析器,vote_count
它使用 parent 执行 ecto 查询match
。但是,如果查询匹配列表,这将遇到 n+1 查询问题。它目前看起来像这样:
def get_vote_count(_root, %{source: %Match{} = match}) do
count = match |> Ecto.assoc(:votes) |> Repo.aggregate(:count, :id)
{:ok, count}
end
我已经在使用dataloader来批量加载子实体,但是在使用Absinthe 提供的函数时,我似乎无法让自定义run_batch
函数工作。Absinthe.Resolution.Helpers.dataloader
使用 dataloader/ecto 实现自定义批量查询的推荐方法是什么?有人可以举个例子,包括架构定义部分吗?