首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将Absinthe用于多到多个关系?

如何将Absinthe用于多到多个关系?
EN

Stack Overflow用户
提问于 2019-10-13 09:22:02
回答 1查看 1.1K关注 0票数 1

在我的凤凰应用程序中,我在一个艺术家和一个使用连接表artists_causes实现的事业模式之间有很多到很多关系。在我的艺术家模式中,我使用了many_to_many :causes, Cause, join_through: "artists_causes",在原因模式中,我使用了many_to_many :artists, Artist, join_through: "artists_causes",在我的CauseTypes模块中,我实现了一个cause对象,如下所示

代码语言:javascript
复制
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>的原因中查询艺术家。

代码语言:javascript
复制
query{
 causes{
  id
  artists {
            id
   }
 }
}

在与许多人的关系中,我是否遗漏了任何一小部分?

==========

更新

我更新了我的list_causes函数如下

代码语言:javascript
复制
def list_causes do    
   Repo.all(MyApp.Causes.Cause) 
end

代码语言:javascript
复制
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方法。我的助手是进口的,还有什么东西我可能会丢失吗?

EN

回答 1

Stack Overflow用户

发布于 2019-10-15 10:51:18

我认为你必须预先加载关系与艺术家曼努里从经济,然后把它传递给阿普辛斯。

例如,获取原因如下:

代码语言:javascript
复制
from(c in Cause,
  preload: [:artists],
  select: c
)
|> Repo.all()

附加

我解决这个问题的方法。

在查询对象中,我传递解析器模块函数引用。

代码语言:javascript
复制
resolve(&App.Resolver.get_all_causes/2)

使用解析器函数,我返回数据集。

代码语言:javascript
复制
def get_all_causes(_params, _info) do
  {:ok,
   from(c in Cause,
     preload: [:artists],
     select: c
   )
   |> Repo.all()}
end
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58362174

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档