Graphql 如何使用苦艾酒数据加载器处理多对多关系

Graphql 如何使用苦艾酒数据加载器处理多对多关系,graphql,elixir,phoenix-framework,absinthe,Graphql,Elixir,Phoenix Framework,Absinthe,在我的phoenix应用程序中,我使用联接表artists\u Cause实现了艺术家和原因模式之间的多对多关系。在我的艺术家模式中,我有many-to-many:Cause,Cause,join-by:“artists-u-Cause”在原因模式中,我有many-to-many:artists,Artist,join-by:“artists-u-Cause” 我将苦艾酒用于graphql,在我的CauseTypes模块中,我实现了一个CauseTypes对象,如下所示 defmodule My

在我的phoenix应用程序中,我使用联接表
artists\u Cause
实现了艺术家和原因模式之间的多对多关系。在我的艺术家模式中,我有
many-to-many:Cause,Cause,join-by:“artists-u-Cause”
在原因模式中,我有
many-to-many:artists,Artist,join-by:“artists-u-Cause”
我将苦艾酒用于graphql,在我的
CauseTypes
模块中,我实现了一个
CauseTypes
对象,如下所示

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
据我所知,对于苦艾酒数据加载器,数据加载器/1是我需要加载艺术家列表的内容。但是,当我在graphiql中运行下面的查询时,我无法从一个原因中查询艺术家,获取错误
artists:#exto.Association.NotLoaded

query{
 causes{
  id
  artists {
            id
   }
 }
}
在处理多对多关系的过程中,我是否遗漏了一些小东西

==========

更新 我更新了我的列表,如下所示

def list_causes do    
   Repo.all(MyApp.Causes.Cause) 
end


现在,我在POST/graphiql\n\n异常:\n\n**(FunctionClauseError)Absince.Resolution.Helpers.dataloader/1中的匿名fn/3中没有匹配的function子句,它可能指向
Absince.Resolution.Helpers.dataloader/1
方法。我已经导入了帮手,还有什么我可能遗漏的吗?

我想你必须在将其传递给苦艾酒之前,手动从埃克托预加载与艺术家的关系

例如,获取原因如下:

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()}
end

我认为你必须在把它交给苦艾酒之前,从埃克托手工预装与艺术家的关系

例如,获取原因如下:

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()}
end

您好@TreeFolk谢谢,我已经按照您的建议进行了更新,现在我在Absithe.Resolution.Helpers.dataloader/1中得到了错误
(FunctionClauseError)no function子句匹配的匿名fn/3,它可能指向ResolutionHelpers,可能我遗漏了什么吗?不幸的是,我的回答是基于我自己的苦艾酒经验,并且我没有使用Dataloader。我将添加我的解决方案示例。您好@TreeFolk谢谢,我已经按照您的建议进行了更新,现在在Absince.Resolution.Helpers.dataloader/1中得到了错误
(FunctionClauseError)no function子句匹配,它可能指向ResolutionHelpers,可能我遗漏了什么吗?不幸的是,我的回答是基于我自己的苦艾酒经验,并且我没有使用Dataloader。我将添加我的解决方案的示例。