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?

1 Answer 1

1

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

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 try something like this: github.com/Shopify/graphql-batch/blob/master/examples/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.