0

Absinthe 和 Dataloader 之间的集成对我来说是全新的,所以欢迎任何帮助。

我遇到以下错误:

** (FunctionClauseError) no function clause matching in anonymous fn/3 in Absinthe.Resolution.Helpers.dataloader/2

我的PostType

defmodule MyApp.Schema.Types.PostType do
  use Absinthe.Schema.Notation
  import Absinthe.Resolution.Helpers, only: [dataloader: 1]

  object :post_type do
    field(:id, :id)
    field(:title, :string)
    field(:body, :string)
    field(:published, :boolean)
    field :user, :user_type, resolve: dataloader(:user)
  end

  input_object :post_input_type do
    field(:title, non_null(:string))
    field(:body, non_null(:string))
    field(:published, non_null(:boolean))
  end
end

模式突变:

@desc "Create a post"
    field :create_post, type: :post_type do
      arg(:input, non_null(:post_input_type))
      middleware(Authorize, :any)
      resolve(&Resolvers.PostResolver.create_post/3)
    end

解析器:

def create_post(_, %{input: input}, %{context: %{current_user: current_user}}) do
    Map.merge(input, %{user_id: current_user.id})
    |> Blog.create_post()
  end
4

1 回答 1

0

助手的第一个参数dataloader应该是一个模块,而不是一个原子。

为了说明起见,我假设您的User模块在 中定义,MyApp.Accounts.User并且您有一个名为accounts.ex.

在里面accounts.ex,你应该有这个:

  def data() do
    Dataloader.Ecto.new(Repo)
  end

然后更改帖子类型中的用户字段以使用此:

field :user, :user_type, resolve: dataloader(User)

如果它仍然不起作用,我可以提供更多帮助。我目前正在尝试使用 Absinthe 构建一个应用程序,所以它在我的脑海中是新鲜的。

于 2020-07-15T12:07:07.230 回答