Skip to content

Commit 4d525f1

Browse files
committed
Added fields to show-case defer and stream
1 parent a6ddabd commit 4d525f1

File tree

2 files changed

+45
-3
lines changed

2 files changed

+45
-3
lines changed

code/complete/GraphQL/GraphQL.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
</PropertyGroup>
1313

1414
<ItemGroup>
15-
<PackageReference Include="HotChocolate.AspNetCore" Version="12.0.0" />
16-
<PackageReference Include="HotChocolate.Data.EntityFramework" Version="12.0.0" />
17-
<PackageReference Include="HotChocolate.PersistedQueries.FileSystem" Version="12.0.0" />
15+
<PackageReference Include="HotChocolate.AspNetCore" Version="12.0.1" />
16+
<PackageReference Include="HotChocolate.Data.EntityFramework" Version="12.0.1" />
17+
<PackageReference Include="HotChocolate.PersistedQueries.FileSystem" Version="12.0.1" />
1818
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.0-preview.7.21378.4" />
1919
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.0-preview.7.21378.4">
2020
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>

code/complete/GraphQL/Speakers/SpeakerNode.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
using System;
12
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Runtime.CompilerServices;
25
using System.Threading;
36
using System.Threading.Tasks;
47
using ConferencePlanner.GraphQL.Data;
58
using ConferencePlanner.GraphQL.DataLoader;
69
using HotChocolate;
710
using HotChocolate.Types;
811
using HotChocolate.Types.Relay;
12+
using Microsoft.EntityFrameworkCore;
913

1014
namespace ConferencePlanner.GraphQL.Speakers
1115
{
@@ -26,5 +30,43 @@ public static Task<Speaker> GetSpeakerByIdAsync(
2630
SpeakerByIdDataLoader speakerById,
2731
CancellationToken cancellationToken)
2832
=> 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+
}
2971
}
3072
}

0 commit comments

Comments
 (0)