Add RelayDataLoader to handle page requests
Summary
(Ongoing PR: graphql-dotnet/graphql-dotnet#1690)
As of now, it is quite annoying to orchestrate a GraphQL Relay-based server without having to create a lot of extra code. This is why I suggest including a new DataLoader (e.g. RelayDataLoader) to handle the page requests. Also, since EF Core is a natural match for the infrastructure layer of GraphQL servers, I have also implemented an extension method to relieve the pain of getting a page (based on the Relay specification) from a DbSet<T>.
Basic example
Consider the simplest use case: Having an AType that may connect to many elements of a given BType.
AType.cs
Connection<AType, BType>("bs", resolve: context =>
{
var loader = dataLoaderAccesor.Context.GetOrAddRelayBatchDataLoader<int, B>("ABConnection", (pageRequests, ct) => bRepository.ListBsAsync(pageRequests)));
return loader.LoadPageAsync(context.GetPageRequest(context.Source.Id));
});
BRepository.cs
public async Task<IDictionary<int, Connection<B>>> ListBsAsync(IEnumerable<PageRequest<int>> pageRequests) {
return await _context.Bs.ToConnectionDictionary(pageRequest, b => b.AId, b => b.Id.ToString());
}
Motivation
This approach greatly enhances both coding speed and readability of the code and makes it easier to maintain GraphQL Relay servers in .NET with EF Core.
@sungam3r I suggest moving this suggestion to the relay repo.
As you see fit, I'm not ready to talk about relay-related stuff.