1
+ using System ;
1
2
using System . Collections . Generic ;
3
+ using System . Linq ;
4
+ using System . Runtime . CompilerServices ;
2
5
using System . Threading ;
3
6
using System . Threading . Tasks ;
4
7
using ConferencePlanner . GraphQL . Data ;
5
8
using ConferencePlanner . GraphQL . DataLoader ;
6
9
using HotChocolate ;
7
10
using HotChocolate . Types ;
8
11
using HotChocolate . Types . Relay ;
12
+ using Microsoft . EntityFrameworkCore ;
9
13
10
14
namespace ConferencePlanner . GraphQL . Speakers
11
15
{
@@ -26,5 +30,43 @@ public static Task<Speaker> GetSpeakerByIdAsync(
26
30
SpeakerByIdDataLoader speakerById ,
27
31
CancellationToken cancellationToken )
28
32
=> speakerById . LoadAsync ( id , cancellationToken ) ;
33
+
34
+ public async Task < IEnumerable < Session > > GetSessionsExpensiveAsync (
35
+ [ Parent ] Speaker speaker ,
36
+ SessionBySpeakerIdDataLoader sessionBySpeakerId ,
37
+ CancellationToken cancellationToken )
38
+ {
39
+ await Task . Delay ( new Random ( ) . Next ( 1000 , 3000 ) , cancellationToken ) ;
40
+
41
+ return await sessionBySpeakerId . LoadAsync ( speaker . Id , cancellationToken ) ;
42
+ }
43
+
44
+ public async IAsyncEnumerable < Session > GetSessionsStreamAsync (
45
+ [ Parent ] Speaker speaker ,
46
+ [ Service ] IDbContextFactory < ApplicationDbContext > contextFactory ,
47
+ [ EnumeratorCancellation ] CancellationToken cancellationToken )
48
+ {
49
+ var random = new Random ( ) ;
50
+
51
+ await Task . Delay ( random . Next ( 500 , 1000 ) , cancellationToken ) ;
52
+
53
+ await using var context = contextFactory . CreateDbContext ( ) ;
54
+
55
+ var stream = ( IAsyncEnumerable < SessionSpeaker > ) context . Speakers
56
+ . Where ( s => s . Id == speaker . Id )
57
+ . Include ( s => s . SessionSpeakers )
58
+ . SelectMany ( s => s . SessionSpeakers )
59
+ . Include ( s => s . Session ) ;
60
+
61
+ await foreach ( var item in stream . WithCancellation ( cancellationToken ) )
62
+ {
63
+ if ( item . Session is not null )
64
+ {
65
+ yield return item . Session ;
66
+ }
67
+
68
+ await Task . Delay ( random . Next ( 100 , 300 ) , cancellationToken ) ;
69
+ }
70
+ }
29
71
}
30
72
}
0 commit comments