Merging queries from multiple loaders #5
thomasheyenbrock
started this conversation in
Ideas
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
A rough idea of mine that I wanna validate by posting it here 🙂
Context
Remix does a great job of avoiding request waterfalls by calling all the loaders of the nested routes in parallel. Depending on your application and what your loaders are doing, this might lead to multiple loaders having to fetch the same entity from the database at the same time.
A typical case I can imaging would be a user object. Given the current session, multiple loaders might need to fetch the current user from the database in order to accumulate their data.
General purpose solutions
Something like
dataloader
might be a general purpose solution for this problem if you do not use GraphQL.How
remix-graphql
can helpIf you use
remix-graphql
to process loader requests with GraphQL, the library could actually help you a lot with this! Since GraphQL has a strongly typed schema, we can merge multiple queries from different loaders into one big query that needs to be processed. Once that query is resolved, each loader receives the data that it specifically requested.So if all loaders contain a request for the user (e.g.
{ me { firstName, lastName } }
), theme
field only has to be resolved once instead of resolving it for each loader separately, thus avoiding multiple database queries for the same user.Loose thoughts
remix-graphql
user opts in to by providing akey
argument in the call toprocessRequestWithGraphql
. All queries from loaders with the same key will then be merged together and executed in one run.Feedback appreciated ❤️ 👇
Beta Was this translation helpful? Give feedback.
All reactions