Dataloader compilation error

Hi, I am trying to use dataloader in Absinthe, but am getting this error message on compile:

== Compilation error in file lib/book_list_web/schema.ex ==
** (CompileError) lib/book_list_web/schema.ex:8: cannot import Absinthe.Resolution.Helpers.dataloader/1 because it is undefined or private

Here is where the error occurs:

defmodule BookListWeb.Schema do

  use Absinthe.Schema
  use Absinthe.Ecto, repo: BookList.Repo

  alias BookList.BookSpace.Book

  import Absinthe.Resolution.Helpers, only: [dataloader: 1] . ### ERROR HERE
  import_types Absinthe.Type.Custom

Here are the related lines in mix.exs:

      {:plug_cowboy, "~> 1.0"},
      {:absinthe_ecto, "~> 0.1.0"},
      {:absinthe_plug, "~> 1.3.0"},
      {:dataloader, "~> 1.0.0"},

Absinthe needs to be recompiled now that Dataloader is around.

Try:

mix deps.compile absinthe

Hi, just tried that, but the error persists when I do iex -S mix phx.server

It seems you might still be on Absinthe 1.3

3 Likes

Yes, that was indeed it (version). Thanks so much!

You’re welcome!

I jumped the gun and thought you have the same problem I previously had.

For anyone having a similar problem, Absinthe only defines the Dataloader-related functions if Dataloader is around. If you add Dataloader as a new dependency after Absinthe has already been compiled, these functions wouldn’t be available. Recompiling Absinthe fixes this.

2 Likes

I’m having this same issue, even recompiling absinthe doesn’t fix it.

      {:phoenix, "~> 1.4.15"},
      {:phoenix_pubsub, "~> 1.1"},
      {:phoenix_ecto, "~> 4.0"},
      {:gettext, "~> 0.11"},
      {:nuki, in_umbrella: true},
      {:jason, "~> 1.0"},
      {:plug_cowboy, "~> 2.0"},
      {:dataloader, "~> 1.0"},
      {:absinthe_plug, "~> 1.4"},
      {:absinthe, "~> 1.4"},
      {:absinthe_ecto, "~> 0.1.3"}

I’m in an umbrella application, added absinthe and dataloader to the phoenix app within the umbrella.

Tried deleting _build and deps but the same result.

defmodule NukiWeb.Schema.ContentTypes do
  import Absinthe.Resolution.Helpers, only: [dataloader: 1]
  use Absinthe.Schema.Notation

# ====

$ iex -S mix phx.server
Erlang/OTP 21 [erts-10.1] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

==> nuki_web
Compiling 12 files (.ex)

== Compilation error in file lib/nuki_web/schema/content_types.ex ==
** (CompileError) lib/nuki_web/schema/content_types.ex:12: undefined function dataloader/1
    (elixir 1.10.2) src/elixir_locals.erl:114: anonymous fn/3 in :elixir_locals.ensure_no_undefined_local/3
    (stdlib 3.6) erl_eval.erl:680: :erl_eval.do_apply/6
    (elixir 1.10.2) lib/kernel/parallel_compiler.ex:304: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

Appreciate any help

1 Like

Hi Sergio! Are you still running into your issue? If you have a minimal example repo that I can clone, I can take a look.

Hey @aptinio appreciate your help! My project is barebones, just trying to get dataloader to connect two database tables. I can add you to my repo, what’s your github username?

It’s aptinio

Would be search users, after I explained the issue and invited Aptinio I figured out the issue through sheer luck.

In the file where you define your content types, makes sure you’re importing in the right order!

# BAD
defmodule NukiWeb.Schema.ContentTypes do
  import Absinthe.Resolution.Helpers, only: [dataloader: 1]
  use Absinthe.Schema.Notation

# GOOD
defmodule NukiWeb.Schema.ContentTypes do
  use Absinthe.Schema.Notation
  import Absinthe.Resolution.Helpers, only: [dataloader: 1]

@benwilson512 would love your insight if you have any on this issue - why does the Helpers module need to be imported AFTER the Notation module?

Ah, this is because use Absinthe.Schema.Notation does an import of the Helpers module which ends up overriding any imports done before.

2 Likes

Thanks Ben - that makes sense.

I wanted to chime in here with another cause of this compiler error.

We encountered this error when the developer setting up the project for the first time ran mix test. After some lengthy analysis, we narrowed down the problem to a development setup script that included the line MIX_ENV=test mix ecto.create. When setting up the project for the first time, this is the first command that compiles the project in the test environment. We thought this would compile the project the same way that MIX_ENV=test mix compile does, but apparently it doesn’t. The solution was to modify that line to this: MIX_ENV=test mix do compile, ecto.create. This fixed the issue. I’m not sure what the underlying cause is, and I’m not certain that it has do with with umbrella apps, but my guess is that it does. It seems to me that MIX_ENV=test ecto.create didn’t compile all the dependencies (including Dataloader and Absinthe), but only the ones that were part of the sub app that Ecto was in.

1 Like