relay icon indicating copy to clipboard operation
relay copied to clipboard

Add RelayDataLoader to handle page requests

Open karrocon opened this issue 5 years ago • 2 comments

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.

karrocon avatar May 22 '20 07:05 karrocon

@sungam3r I suggest moving this suggestion to the relay repo.

Shane32 avatar Apr 23 '23 18:04 Shane32

As you see fit, I'm not ready to talk about relay-related stuff.

sungam3r avatar Apr 23 '23 21:04 sungam3r