dataloader icon indicating copy to clipboard operation
dataloader copied to clipboard

Dataloader.Ecto throws an error if you try to load a non-ecto struct

Open axelson opened this issue 6 years ago • 2 comments
trafficstars

Dataloader.Ecto currently throws an UndefinedFunctionError if you try to load a non-ecto struct. However, I believe that this is a scenario that Dataloader.Ecto should be able to handle because that non-ecto struct could be derived from data that is in the database.

Here is an example error for an example non-ecto struct named %Dataloader.MentionTag{} (that represents entries that can be mentioned in a chat program like Slack):

     ** (UndefinedFunctionError) function Dataloader.MentionTag.__schema__/1 is undefined or private
     code: |> Dataloader.load(Test, {:many, MentionTag}, name: "everything")
     stacktrace:
       (dataloader) Dataloader.MentionTag.__schema__(:primary_key)
       (dataloader) lib/dataloader/ecto.ex:367: Dataloader.Source.Dataloader.Ecto.normalize_value/2
       (dataloader) lib/dataloader/ecto.ex:343: Dataloader.Source.Dataloader.Ecto.get_keys/2
       (dataloader) lib/dataloader/ecto.ex:247: Dataloader.Source.Dataloader.Ecto.fetch/3
       (dataloader) lib/dataloader/ecto.ex:287: Dataloader.Source.Dataloader.Ecto.load/3
       (elixir) lib/enum.ex:1925: Enum."-reduce/3-lists^foldl/2-0-"/3
       (dataloader) lib/dataloader.ex:128: Dataloader.load_many/4
       test/dataloader/ecto_test.exs:372: (test)

Is this a scenario that Dataloader.Ecto could be expanded to support?

axelson avatar Dec 03 '18 21:12 axelson

My main concern here is that I don't know how any of the Dataloader.Ecto callbacks could work. The query one for example wouldn't be able to use or return a queryable. It'd always require a custom run_batch run and a bunch of the variables there won't make a ton of sense.

What would Dataloader.Ecto offer for these types offer over Dataloader.KV ?

benwilson512 avatar Dec 03 '18 21:12 benwilson512

Yeah, I believe that you'd have to use run_batch instead of query to load the data.

What would Dataloader.Ecto offer for these types offer over Dataloader.KV?

If 95% of the data that I want to load is backed by a queryable, but the other 5% is not I don't want to need to either have:

  • Both a Dataloader.Ecto source and a Dataloader.KV source
  • Be forced to only use Dataloader.KV just because of the 5% of data/queries that are not queryables

axelson avatar Dec 03 '18 22:12 axelson

In this case, the recommended course of action is to use the KV source. The Ecto source exists to interface with Ecto schemas.

benwilson512 avatar Oct 31 '22 04:10 benwilson512