Absinthe Dataloader fetches assocs even if already preloaded

Say the Absinthe resolver returns a struct with an assoc that has already been loaded, e.g.

%Organization{
  name: "Acme",
  members: []
}

When using Dataloader with Absinthe to load the :members for the organization, dataloader will still execute a query to fetch the members while it was already loaded (set to []).

I found this old merge:

But the above GH comments don’t really state why we ignore already preloaded associations. It only mentions consistency but I do not see why we need to execute preload queries again if those had already been executed against the db, leading to double db requests.

Any idea as to why?

An example how you can sort it out:

    field :members, non_null(list_of(non_null(:member_type))) do
      resolve(fn parent, args, context ->
        case parent.members do
          %Ecto.Association.NotLoaded{} ->
            dataloader(:source, :members).(parent, args, context)

          members->
            {:ok, members}
        end
      end)
    end
1 Like