This project demonstrates N+1 query issues when the resolver execution strategy
is set to Serial
.
There are two types defined. These types are User and Post. A user has many posts and a post belongs to an author (User).
In Program.cs we can toggle between serial and parallel execution strategies.
Run the project with the debugger attached and a breakpoint within the UserDataLoader.GetUsersAsync method.
Now run the following query:
query {
posts {
author {
id
}
}
}
We are asking for all posts and for each post, the author id. The expected behavior is that rather than executing a query for each post to fetch the author, we should fetch all authors in a batch.
You can observe actual behavior when the breakpoint is hit. If method is executed multiple times in one request, then there is an N+1 issue. In this event, you'll notice that when the method is called, it is passing a single id at a time.
Now within Program.cs, change ExecutionStrategy.Serial
to ExecutionStrategy.Parallel
. Now repeat the Recreation step except
that this time, you should only observe the dataloader being executed once.