brysgo / graphql-bookshelf

Some help defining GraphQL schema around BookshelfJS models
184 stars 12 forks source link

Optimization for relations of relation #25

Open hardfire opened 7 years ago

hardfire commented 7 years ago

I have created a small example where the model is like the following

 Student --- hasMany ---> Homework --- hasMany -----> Page

Now, when I load the following (note: viewer is the student) the there is one query for the student, one for the homework, but multiple for the pages. There are as many queries as there are pages. How can i optimize it so that there is only one query for the all the pages.

Best case scenario would be to load the whole thing at once, but i'd be happy for any pointers on how to optimize this.

{ 
 viewer(id: 3) {
    name
    homeworks{
      subject
      pages{
        content
      }
    }
  }
}

The code i used to test this is available at https://gist.github.com/hardfire/3ff69d3a602bf3c30dcbbca71d67d605

brysgo commented 7 years ago

When I built this, I was meaning to squeeze dataloader (https://github.com/facebook/dataloader) in to avoid situation. I would suggest playing with it.

First try solving the problem without graphql-bookshelf, then once you have solved it, you can think of ideas on how to bring it in.

hardfire commented 7 years ago

This makes total sense. I have already started using dataloader in my application. Right now alongside graphql-bookshelf. It would be interesting to see if we can hook dataloader into graphql-bookshelf to work automagically. Thanks for the response. I will close this issue now.

brysgo commented 7 years ago

I'm happy to leave this open until someone takes a shot at integrating this.