Skip to content

Commit c424d8c

Browse files
committed
Starte work
1 parent a798fee commit c424d8c

26 files changed

+352
-280
lines changed

code/complete/GraphQL/Attendees/AttendeeMutations.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public async Task<CheckInAttendeePayload> CheckInAttendeeAsync(
4040
[Service] ITopicEventSender eventSender,
4141
CancellationToken cancellationToken)
4242
{
43-
Attendee attendee = await context.Attendees.FirstOrDefaultAsync(
43+
var attendee = await context.Attendees.FirstOrDefaultAsync(
4444
t => t.Id == input.AttendeeId, cancellationToken);
4545

4646
if (attendee is null)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using Microsoft.EntityFrameworkCore;
6+
using ConferencePlanner.GraphQL.Data;
7+
using ConferencePlanner.GraphQL.DataLoader;
8+
using HotChocolate;
9+
using HotChocolate.Types;
10+
using HotChocolate.Types.Relay;
11+
12+
namespace ConferencePlanner.GraphQL.Types
13+
{
14+
[Node]
15+
[ExtendObjectType]
16+
public class AttendeeNode
17+
{
18+
public async Task<IEnumerable<Session>> GetSessionsAsync(
19+
[Parent] Attendee attendee,
20+
[ScopedService] ApplicationDbContext dbContext,
21+
SessionByIdDataLoader sessionById,
22+
CancellationToken cancellationToken)
23+
{
24+
int[] speakerIds = await dbContext.Attendees
25+
.Where(a => a.Id == attendee.Id)
26+
.Include(a => a.SessionsAttendees)
27+
.SelectMany(a => a.SessionsAttendees.Select(t => t.SessionId))
28+
.ToArrayAsync(cancellationToken);
29+
30+
return await sessionById.LoadAsync(speakerIds, cancellationToken);
31+
}
32+
33+
[NodeResolver]
34+
public static Task<Attendee> GetAttendeeAsync(
35+
int id,
36+
AttendeeByIdDataLoader attendeeById,
37+
CancellationToken cancellationToken)
38+
=> attendeeById.LoadAsync(id, cancellationToken);
39+
}
40+
}

code/complete/GraphQL/Data/Attendee.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ public class Attendee
2121

2222
[StringLength(256)]
2323
public string? EmailAddress { get; set; }
24+
25+
[StringLength(256)]
26+
public string? Country { get; set; }
2427

2528
public ICollection<SessionAttendee> SessionsAttendees { get; set; } =
2629
new List<SessionAttendee>();

code/complete/GraphQL/DataLoader/AttendeeByIdDataLoader.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public class AttendeeByIdDataLoader : BatchDataLoader<int, Attendee>
1414
private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
1515

1616
public AttendeeByIdDataLoader(
17+
IDbContextFactory<ApplicationDbContext> dbContextFactory,
1718
IBatchScheduler batchScheduler,
18-
IDbContextFactory<ApplicationDbContext> dbContextFactory)
19-
: base(batchScheduler)
19+
DataLoaderOptions options)
20+
: base(batchScheduler, options)
2021
{
2122
_dbContextFactory = dbContextFactory ??
2223
throw new ArgumentNullException(nameof(dbContextFactory));

code/complete/GraphQL/DataLoader/SessionByIdDataLoader.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public class SessionByIdDataLoader : BatchDataLoader<int, Session>
1414
private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
1515

1616
public SessionByIdDataLoader(
17-
IBatchScheduler batchScheduler,
18-
IDbContextFactory<ApplicationDbContext> dbContextFactory)
19-
: base(batchScheduler)
17+
IDbContextFactory<ApplicationDbContext> dbContextFactory,
18+
IBatchScheduler batchScheduler,
19+
DataLoaderOptions options)
20+
: base(batchScheduler, options)
2021
{
2122
_dbContextFactory = dbContextFactory ??
2223
throw new ArgumentNullException(nameof(dbContextFactory));
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using ConferencePlanner.GraphQL.Data;
7+
using GreenDonut;
8+
using Microsoft.EntityFrameworkCore;
9+
10+
namespace ConferencePlanner.GraphQL.DataLoader
11+
{
12+
public class SessionBySpeakerIdDataLoader : GroupedDataLoader<int, Session>
13+
{
14+
private static readonly string _sessionCacheKey = GetCacheKeyType<SessionByIdDataLoader>();
15+
private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
16+
17+
public SessionBySpeakerIdDataLoader(
18+
IDbContextFactory<ApplicationDbContext> dbContextFactory,
19+
IBatchScheduler batchScheduler,
20+
DataLoaderOptions options)
21+
: base(batchScheduler, options)
22+
{
23+
_dbContextFactory = dbContextFactory ??
24+
throw new ArgumentNullException(nameof(dbContextFactory));
25+
}
26+
27+
protected override async Task<ILookup<int, Session>> LoadGroupedBatchAsync(
28+
IReadOnlyList<int> keys,
29+
CancellationToken cancellationToken)
30+
{
31+
await using ApplicationDbContext dbContext =
32+
_dbContextFactory.CreateDbContext();
33+
34+
List<SessionSpeaker> list = await dbContext.Speakers
35+
.Where(s => keys.Contains(s.Id))
36+
.Include(s => s.SessionSpeakers)
37+
.SelectMany(s => s.SessionSpeakers)
38+
.Include(s => s.Session)
39+
.ToListAsync(cancellationToken);
40+
41+
TryAddToCache(_sessionCacheKey, list, item => item.SessionId, item => item.Session!);
42+
43+
return list.ToLookup(t => t.SpeakerId, t => t.Session!);
44+
}
45+
}
46+
}

code/complete/GraphQL/DataLoader/SpeakerByIdDataLoader.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public class SpeakerByIdDataLoader : BatchDataLoader<int, Speaker>
1414
private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
1515

1616
public SpeakerByIdDataLoader(
17-
IBatchScheduler batchScheduler,
18-
IDbContextFactory<ApplicationDbContext> dbContextFactory)
19-
: base(batchScheduler)
17+
IDbContextFactory<ApplicationDbContext> dbContextFactory,
18+
IBatchScheduler batchScheduler,
19+
DataLoaderOptions options)
20+
: base(batchScheduler, options)
2021
{
2122
_dbContextFactory = dbContextFactory ??
2223
throw new ArgumentNullException(nameof(dbContextFactory));
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
using ConferencePlanner.GraphQL.Data;
7+
using GreenDonut;
8+
using Microsoft.EntityFrameworkCore;
9+
10+
namespace ConferencePlanner.GraphQL.DataLoader
11+
{
12+
public class SpeakerBySessionIdDataLoader : GroupedDataLoader<int, Speaker>
13+
{
14+
private static readonly string _speakerCacheKey = GetCacheKeyType<SpeakerByIdDataLoader>();
15+
private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
16+
17+
public SpeakerBySessionIdDataLoader(
18+
IDbContextFactory<ApplicationDbContext> dbContextFactory,
19+
IBatchScheduler batchScheduler,
20+
DataLoaderOptions options)
21+
: base(batchScheduler, options)
22+
{
23+
_dbContextFactory = dbContextFactory ??
24+
throw new ArgumentNullException(nameof(dbContextFactory));
25+
}
26+
27+
protected override async Task<ILookup<int, Speaker>> LoadGroupedBatchAsync(
28+
IReadOnlyList<int> keys,
29+
CancellationToken cancellationToken)
30+
{
31+
await using ApplicationDbContext dbContext =
32+
_dbContextFactory.CreateDbContext();
33+
34+
List<SessionSpeaker> list = await dbContext.Sessions
35+
.Where(s => keys.Contains(s.Id))
36+
.Include(s => s.SessionSpeakers)
37+
.SelectMany(s => s.SessionSpeakers)
38+
.Include(s => s.Speaker)
39+
.ToListAsync(cancellationToken);
40+
41+
TryAddToCache(_speakerCacheKey, list, item => item.SpeakerId, item => item.Speaker!);
42+
43+
return list.ToLookup(t => t.SessionId, t => t.Speaker!);
44+
}
45+
}
46+
}

code/complete/GraphQL/DataLoader/TrackByIdDataLoader.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,10 @@ public class TrackByIdDataLoader : BatchDataLoader<int, Track>
1414
private readonly IDbContextFactory<ApplicationDbContext> _dbContextFactory;
1515

1616
public TrackByIdDataLoader(
17-
IBatchScheduler batchScheduler,
18-
IDbContextFactory<ApplicationDbContext> dbContextFactory)
19-
: base(batchScheduler)
17+
IDbContextFactory<ApplicationDbContext> dbContextFactory,
18+
IBatchScheduler batchScheduler,
19+
DataLoaderOptions options)
20+
: base(batchScheduler, options)
2021
{
2122
_dbContextFactory = dbContextFactory ??
2223
throw new ArgumentNullException(nameof(dbContextFactory));

0 commit comments

Comments
 (0)