graphql-dotnet / graphql-dotnet/relay
Add RelayDataLoader to handle page requests
- Dominant language
- C#
- Stars
- 74
- Forks
- 28
- PR merge metrics
- No merged PRs in 30d
Description
## 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.
### Basic example
Consider the simplest use case: Having an AType that may connect to many elements of a given BType.
_AType.cs_
```
Connection("bs", resolve: context =>
{
var loader = dataLoaderAccesor.Context.GetOrAddRelayBatchDataLoader("ABConnection", (pageRequests, ct) => bRepository.ListBsAsync(pageRequests)));
return loader.LoadPageAsync(context.GetPageRequest(context.Source.Id));
});
```
_BRepository.cs_
```
public async Task>> ListBsAsync(IEnumerable> 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.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.