Dataloader is undefined when using MIX_ENV=prod

Hello!

I’m quite stuck on this one. I use Absinthe + Dataloader. When running my project locally everything works absolutely fine. However when I run commands such as mix mix ecto.migrate, setting MIX_ENV=prod it gives the error:
undefined function dataloader/2

The file its complaining about is:

defmodule ScribeWeb.Schema.AnalyticsTypes do
  use Absinthe.Schema.Notation
  import Absinthe.Resolution.Helpers

  object :tablet_view do
    field :viewed_by, :user, resolve: dataloader(Scribe.Accounts, :user)
    field(:inserted_at, :date, name: "viewed_at")
    field :tablet, :tablet, resolve: dataloader(Scribe.Tablets)
  end
end

My mix.exs is:

defmodule Scribe.MixProject do
  use Mix.Project

  def project do
    [
      app: :scribe,
      releases: [
        scribe: [
          include_erts: true,
          include_executables_for: [:unix],
          applications: [
            runtime_tools: :permanent
          ]
        ]
      ],
      version: "0.1.0",
      elixir: "~> 1.5",
      elixirc_paths: elixirc_paths(Mix.env()),
      compilers: [:phoenix, :gettext] ++ Mix.compilers(),
      start_permanent: Mix.env() == :prod,
      aliases: aliases(),
      deps: deps()
    ]
  end

  # Configuration for the OTP application.
  #
  # Type `mix help compile.app` for more information.
  def application do
    [
      mod: {Scribe.Application, []},
      extra_applications: [:logger, :runtime_tools, :ueberauth_github, :ueberauth_google]
    ]
  end

  # Specifies which paths to compile per environment.
  defp elixirc_paths(:test), do: ["lib", "test", "test/support"]
  defp elixirc_paths(_), do: ["lib"]

  # Specifies your project dependencies.
  #
  # Type `mix help deps` for examples and options.
  defp deps do
    [
      {:phoenix, "~> 1.4.9"},
      {:phoenix_html, "~> 2.14"},
      {:phoenix_pubsub, "~> 1.1"},
      {:phoenix_ecto, "~> 4.0"},
      {:ecto_sql, "~> 3.1"},
      {:postgrex, ">= 0.0.0"},
      {:gettext, "~> 0.11"},
      {:jason, "~> 1.0"},
      {:plug_cowboy, "~> 2.0"},
      {:absinthe, "~> 1.4"},
      {:absinthe_plug, "~> 1.4"},
      {:argon2_elixir, "~> 2.0"},
      {:timex, "~> 3.5"},
      {:ueberauth_github, "~> 0.7"},
      {:ueberauth_google, "~> 0.8"},
      {:cors_plug, "~> 1.5"},
      {:hashids, "~> 2.0"},
      {:bamboo, "~> 1.4"},
      {:hackney, "~> 1.15.2"},
      {:dataloader, "~> 1.0.0"},
      {:poison, "~> 3.1"},
      {:httpoison, "~> 1.6"},
      {:appsignal, "~> 1.0"},
      {:algolia, "~> 0.8.0"}
    ]
  end

  # Aliases are shortcuts or tasks specific to the current project.
  # For example, to create, migrate and run the seeds file at once:
  #
  #     $ mix ecto.setup
  #
  # See the documentation for `Mix` for more info on aliases.
  defp aliases do
    [
      "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
      "ecto.reset": ["ecto.drop", "ecto.setup"],
      test: ["ecto.create --quiet", "ecto.migrate", "test"]
    ]
  end
end

Would massively appreciate any help, very stumped on this one

@Francesco consider doing rm -rf _build and recompiling. If you added dataloader after absinthe already had cached compilation artifacts it may not have been recompiled with the helpers available.

Thankyou, you are a lifesaver! Did the trick perfectly :slight_smile:

1 Like