graphql-dotnet / graphql-dotnet/relay
Converting a large list to a connection.
- Dominant language
- C#
- Stars
- 74
- Forks
- 28
- PR merge metrics
- No merged PRs in 30d
Description
Hey,
So I used to do this to create my connection:
```
Connection()
.Name("liveStreams")
.Resolve(c =>
{
var liveStreams = liveStreamService.GetAudios();
var splitLiveStreams = ConnectionUtils.ToConection(liveStreams);
return splitLiveStreams;
});
```
However the code internally calls `.ToList()` which means that for every page it is returning all of my 25k records from the database and it would be incredibly slow obviously.
So instead of doing this I now do:
```
Connection()
.Name("liveStreams")
.Resolve(c =>
{
var offset = ConnectionUtils.OffsetOrDefault(c.After, 0);
var liveStreams = liveStreamService.GetAudios();
var newLiveStreams = liveStreams.Take(c.First.Value + (offset + 1));
var count = liveStreamService.GetCount();
var splitLiveStreams = ConnectionUtils.ToConnection(newLiveStreams, c, 0, count);
return splitLiveStreams;
});
```
`
public IQueryable GetAudios() {
return _repository.GetAll();
}
`
So this doesn't load the entire 25k records anymore, only the data I need.
Is this the correct way to do this or am I doing something wrong? It was very hard to figure this out and I feel it may be the wrong way.
I'm using Entity Framework core.
Thanks.
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.