在我的凤凰应用程序中,我在一个艺术家和一个使用连接表artists_causes实现的事业模式之间有很多到很多关系。在我的艺术家模式中,我使用了many_to_many :causes, Cause, join_through: "artists_causes",在原因模式中,我使用了many_to_many :artists, Artist, join_through: "artists_causes",在我的CauseTypes模块中,我实现了一个cause对象,如下所示
defmodule MyAppWeb.Schema.CauseTypes do
@moduledoc """
All types for causes
"""
use Absinthe.Schema.Notation
import Absinthe.Resolution.Helpers, only: [dataloader: 1, dataloader: 3]
object :cause do
field :id, :id
field :artists, list_of(:artist), resolve: dataloader(Artists)
end
def dataloader do
alias MyApp.{Artists, Causes}
loader = Dataloader.new
|> Dataloader.add_source(Causes, Causes.datasource())
|> Dataloader.add_source(Artists, Artists.datasource())
end
def context(ctx) do
Map.put(ctx, :loader, dataloader())
end
def plugins do
[Absinthe.Middleware.Dataloader] ++ Absinthe.Plugin.defaults()
end
end根据我的理解,使用Absinthe,dataloader/1就是我需要加载的艺术家列表。然而,当我在石墨库中运行下面的查询时,我无法从获得错误artists: #Ecto.Association.NotLoaded<association :artists is not loaded>的原因中查询艺术家。
query{
causes{
id
artists {
id
}
}
}在与许多人的关系中,我是否遗漏了任何一小部分?
==========
更新
我更新了我的list_causes函数如下
def list_causes do
Repo.all(MyApp.Causes.Cause)
end至
def list_causes do
Repo.all(from c in Cause,
left_join: ac in "artists_causes", on: c.id == ac.cause_id,
left_join: a in Artist, on: a.id == ac.artist_id,
preload: [:artists]
)
end现在,我得到了错误FunctionClauseError at POST /graphiql\n\nException:\n\n ** (FunctionClauseError) no function clause matching in anonymous fn/3 in Absinthe.Resolution.Helpers.dataloader/1,它可能指向Absinthe.Resolution.Helpers.dataloader/1方法。我的助手是进口的,还有什么东西我可能会丢失吗?
发布于 2019-10-15 10:51:18
我认为你必须预先加载关系与艺术家曼努里从经济,然后把它传递给阿普辛斯。
例如,获取原因如下:
from(c in Cause,
preload: [:artists],
select: c
)
|> Repo.all()附加
我解决这个问题的方法。
在查询对象中,我传递解析器模块函数引用。
resolve(&App.Resolver.get_all_causes/2)使用解析器函数,我返回数据集。
def get_all_causes(_params, _info) do
{:ok,
from(c in Cause,
preload: [:artists],
select: c
)
|> Repo.all()}
endhttps://stackoverflow.com/questions/58362174
复制相似问题