Search code examples
reactjsrelayjsgraphqlgraphql-js

How to accumulate resolve calls for a single request?


When a graphql request hits my server it jumps into several different resolve functions depending on the query. But I don't want to hit my database with sometimes dozens of small requests (basically a select in each resolve function). That would not really scale very well.

Is there a way to accumulate all these resolve calls for a single graphql request - so that at the end I can do some magic and build only a single select to my database and then resolve all promises?

I can see a few ways of building something myself by accumulating all the promises I return in the resolve functions, but I don't know when all resolve functions for a single request have been called.

Is there a solution to this problem? How does Facebook deal with that scenario?

Thank you


Solution

  • Batching all resolve calls into a single database query is an active area of exploration for GraphQL implementations and isn't supported in the reference implementation. Instead, we recommend using dataloader. Using dataloader can help ensure that you fetch each record from the storage layer once, even if it is queried multiple times.