Skip to content

Loading ActiveRelation Model with dataloader but want to paginate it #4817

Answered by rmosolgo
tomocrafter asked this question in Q&A
Discussion options

You must be logged in to vote

Hi! I think your best bet is to return an ActiveRecord relation from the resolver, for example:

def products 
  Product.where(id: prepared_ids)
end 

Then, GraphQL-Ruby's ActiveRecordRelationConnection will apply limit and offset according the client's provided arguments.

In short, batch-loading relations is very hard because you can't load different pages of different relations at the same time. AFAIK only Postgresql "Window functions" can do this, see https://pganalyze.com/blog/efficient-graphql-queries-in-ruby-on-rails-and-postgres#batch-loading-many-records for a discussion of why. I hope that helps!

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@tomocrafter
Comment options

Answer selected by tomocrafter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants