GraphQL is awesome! But one of the most annoying parts to implement a GraphQL server is solving N+1 problem.
TL;DR
Use this packageĀ https://github.com/oney/sequelize-proxy
Dive in
In general, there are two methods to solve N+1 problem.
First method:
ParsingĀ info: GraphQLResolveInfoĀ in query/mutation levels to determine what data the whole operation needs and pre-fetch all data in one single SQL query.
One inconvenience of this method(join-monsterĀ facing) is you have to declareĀ dependenciesĀ of fields to make pre-fetching know how to construct the one single query.
Another inconvenience of this method(postgraphile,Ā prismaĀ facing) is the whole GraphQL type schema are predetermined and bound to your database schema. You canāt change your GraphQL schema.
Second method:
Make SQL queries in any deeper resolvers, but use something like dataloader to collect all queries and combine them to single one. This method is more free comparing to first method because you donāt have to declare any dependency and bind to database schema.
The disadvantage of this method is we have to declare a data loader for each model in our ORM (Even though you can write some function to wrap this functionality). Besides, dataloader way can easily solve belongsTo relation but are hard to solve hasMany relation. When querying hasMany relation with where and order, it will be more complex for dataloaders to collect queries from different resolvers and make one SQL query.
Solution
So, use this packageĀ https://github.com/oney/sequelize-proxy
It creates built-in dataloaders to collect hasOne, belongsTo, hasMany, belongsToMany relation query with where, order, etc. and make a single SQL query to optimize. You donāt have to deal with any of these by yourself.
If you have any questions, let me know in Community. Any suggestions are also welcome!