Lars-Jørgen Kristiansen
02/20/2019, 2:53 PMPrisma client has a built-in dataloader, solving the N+1 problem and ensuring the Prisma API is queried efficiently.
dpetrick
02/20/2019, 3:56 PMtim2
02/20/2019, 4:57 PM[
{"query": "...", "variables": "..."},
{"query": "...", "variables": "..."},
{"query": "...", "variables": "..."}
]
This is array-based batching aka “Apollo Batching”, as they came up with the idea.
Now the next level batching is alias-based batching.
That would look like this, using GraphQL aliases:
{
query1: workspace()...
query2: workspace()...
}
This, however is fairly complicated to implement and such an implementation doesn’t exist in the ecosystem yet. We will pretty soon add this to the Prisma Client, though. Then the client will use these two batching methods together to achieve optimal performance.
In case you may wonder, how a few ctx.prisma.workspace().workers()
calls may get batched together, you need to understand the JS event loop.
In your example, the workers
resolver is being executed in parallel, which means that a process.nextTick()
callback would be executed after these calls to the client have been done. This under the hood gets batched by the Prisma Client without you noticing anything and sent like that to the server.
We could indeed improve the debugging output there, it is quite confusing and should let the user know, that batching is active. Could you create an issue for that in our Github repo https://github.com/prisma/prisma/issues/new?template=feature_request.md ?
You can read more about the dataloader pattern here: https://github.com/facebook/dataloaderLars-Jørgen Kristiansen
02/20/2019, 6:03 PMLars-Jørgen Kristiansen
02/06/2020, 8:50 PMtim2
02/07/2020, 10:22 AMfindOne(id: 5)
into findMany(where: { id_in: [1,2,4] })
and them match them backLars-Jørgen Kristiansen
02/07/2020, 10:24 AMLars-Jørgen Kristiansen
02/07/2020, 10:26 AMconst resolvers = {
Query: {
users: () => prisma.users(),
messages: () => prisma.messages()
},
Message: {
author: ({ id }) => prisma.message({ id }).author() // To use dataloader here I need the author ID!
},
User: {
messages: ({ id }) => prisma.user({ id }).messages()
}
};
Lars-Jørgen Kristiansen
02/07/2020, 10:35 AM