Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integration with Dataloader #168

Open
dolfinus opened this issue Nov 8, 2020 · 6 comments
Open

Integration with Dataloader #168

dolfinus opened this issue Nov 8, 2020 · 6 comments

Comments

@dolfinus
Copy link
Contributor

dolfinus commented Nov 8, 2020

Hi.

I've tried to use both absinthe-relay (for pagination) and dataloader (for handling N+1 problem) in the same project.
But I haven't found some working way to combine them in the documentation.

So I've impletented a simple helper module which allows to integrate these libraries wich each other:
https://github.com/dolfinus/arkenston-backend/blob/8a9dce9ae09da7341a6e3ea54be9106358cbc7ec/lib/arkenston_web/schema/helpers/pagination.ex#L16-L40
https://github.com/dolfinus/arkenston-backend/blob/8a9dce9ae09da7341a6e3ea54be9106358cbc7ec/lib/arkenston_web/schema/helpers/association.ex#L12-L16
https://github.com/dolfinus/arkenston-backend/blob/8a9dce9ae09da7341a6e3ea54be9106358cbc7ec/lib/arkenston/helper/query_helper.ex#L242

Now I can use paginated macros like:
https://github.com/dolfinus/arkenston-backend/blob/8a9dce9ae09da7341a6e3ea54be9106358cbc7ec/lib/arkenston_web/schema/helpers/revision.ex#L35
https://github.com/dolfinus/arkenston-backend/blob/8a9dce9ae09da7341a6e3ea54be9106358cbc7ec/lib/arkenston_web/schema/query/user.ex#L15

How about to include such a helper to absinthe-relay? IMHO, it'll be helpful.

@benwilson512
Copy link
Contributor

Hey @dolfinus, thanks for these helpers. Until recently, this kind of integration wasn't possible because there wasn't an easy way for Ecto (and by extension dataloader) do unique limits for each parent. However now that Dataloader does support limits, this sort of thing can re-evaluated again.

@allenwyma
Copy link

hi @benwilson512 circling back from our conversation on slack. I'm trying to think about how we can do this in a good way. Here's my idea:

import Absinthe.Relay.Resolution.Helpers, only: [dataloader: 1]

then for the type:

object :parent do
    field :id, :integer
    field :child, resolve: dataloader(Module, order: [desc: :inserted_at])
end

I think the most difficult part is trying to get the sorting correct, so we may consider the above OR maybe rely on the dataloader source to specify the sort?

@woylie
Copy link

woylie commented Mar 6, 2021

I'm using both Absinthe Relay and Dataloader in a project as well, but haven't used Dataloader for Relay connection fields yet. In my case, I'd require the sorting to be handled by the query function of the Dataloader source, without the need to specify the order as in the code example above (potentially taking the sort field/order from query arguments).

@dolfinus
Copy link
Contributor Author

dolfinus commented Mar 6, 2021

IMHO, sorting should be handled by a query function, because some of users could want to pass ordering as an argument. There should be no hard coded values.

@benwilson512
Copy link
Contributor

benwilson512 commented Mar 7, 2021

Agreed about sorting. @HangingClowns I guess I'm a bit confused by your example snippet. That code works today. The "integration with dataloader" part of this issue has to do with combining connection fields with dataloader. Using dataloader on node entity subfields works just fine already.

@allenwyma
Copy link

hi @benwilson512 from what I understand, paginating the data using relay doesnt' work? hence i was thinking we can do this: import Absinthe.Relay.Resolution.Helpers, only: [dataloader: 1], notice how it's importing from Absinthe.Relay INSTEAD of just Absinthe. Maybe the name is too ambiguous, though?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants