feat: improve performance (esp. n+1) by batching requests #107
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Prisma will perform query optimizations at the engine level if multiple similar queries are sent inside the same tick, by automatically batching them together into a single larger query. This feature dramatically improves performance for graphQL servers, avoiding the "n+1" problem. By default, Prisma will batch requests by the transaction ID if it is present, but this behaviour prevents automatic batching from working when using Yates, since all queries are executed inside an interactive transaction. To get around this we by monkey patching the batching function to use the Yates ID as the batch ID. To get the batching to work we also need to ensure that all the requests we might want to batch together are generated inside the same tick. This means that all the requests per-tick that have the same role and context values will be batched together, allowing the in-built prisma batch optimizations to work for us. This is why we use process.nextTick and the tickActive flag to ensure we only tick once at a time. See: