0

I have a model like this

class Foo
  has_many :bars
end

and a query like this

query foos(
  $offset: Int
  $sort_by: String
  $should_paginate: Boolean
) {
  foos(
    offset: $offset
    sort_by: $sort_by
    should_paginate: $should_paginate
  ) {
    id
    name
    bars {

When I fetch this query, I get one select * from "foos" for each bar that's in the collection.

How can I have this all be smarter and do fewer SQL queries?

John Bachir
  • 22,495
  • 29
  • 154
  • 227

1 Answers1

1

Look at https://github.com/Shopify/graphql-batch

It uses allows you to lazy-load your associations at once on demand.

Ahmed Al Hafoudh
  • 8,281
  • 1
  • 18
  • 34
  • It solves the n+1 problem, but not the other problems w/ association-based loading, which is that you will not be able to sort/filter by child attributes. Best way I've found to do _that_ is to use a database view and create another model/type in graphql-ruby for that view – hedgehogrider Apr 19 '20 at 01:04
  • @hedgehogrider try something like this: https://github.com/Shopify/graphql-batch/blob/master/examples/association_loader.rb – Ahmed Al Hafoudh Apr 19 '20 at 19:07