Using Dataloader with a domain layer

I have an app that has a domain layer in between the database and graphql.

It goes Graphql resovler -> hits db or wherever to get data -> passes that to the domain which is just pure functions.

The result of the domain functions is what is returned from the resolvers. This is okay, but we get into problems when trying to use dataloader. Imagine:

object :author do
  field(:name, :string)
end

object :post do
  field(:name, :string)
  field(:author, :author) do
    resolve(fn parent, _, _ ->
      
    end
  end
end

query do
  field(:posts, list_of(:posts)) do
    resolve(fn %{id: id}, _ -> 
       Db.get_post(Post, id)
       # Because this returns an embedded schema, the Ecto dataloader source wont work.
       |> Domain.new_post()
    end
  end
end

Is there a way to get the requests to the db batched with dataloader, but still return domain structs from the resolvers?

If your objects are still keyed by unique ID, instead of using Dataloader.Ecto you can implement a Dataloader.KV store.

The documentation for it isn’t complete, so you might want to check its source code: https://github.com/absinthe-graphql/dataloader/blob/master/lib/dataloader/kv.ex