GraphQL is awesome! But one of the most annoying parts to implement a GraphQL server is solving N+1 problem.
Use this package https://github.com/oney/sequelize-proxy
In general, there are two methods to solve N+1 problem.
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.
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.
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!
Create your free account to unlock your custom reading experience.