Loading ActiveRelation Model with dataloader but want to paginate it #4817
-
Hi, I am currently tring to load model with built in dataloader and paginate it with connection_type. field :products, Types::Objects:Product.connection_type, null: false
def products
dataloader.with(Sources::ProductById).load_all(prepared_ids)
end but it loads all product that in Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
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 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! |
Beta Was this translation helpful? Give feedback.
Hi! I think your best bet is to return an ActiveRecord relation from the resolver, for example:
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!