Skip to content

Commit 8e94289

Browse files
Added "AddDataLoader<TService, TImplementation>" executor overload that takes a factory (#7787)
1 parent a893304 commit 8e94289

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.DataLoader.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ public static IRequestExecutorBuilder AddDataLoader<T>(
3737
builder.Services.TryAddScoped<T>(sp => sp.GetDataLoader<T>());
3838
return builder;
3939
}
40+
41+
public static IRequestExecutorBuilder AddDataLoader<TService, TImplementation>(
42+
this IRequestExecutorBuilder builder,
43+
Func<IServiceProvider, TImplementation> factory)
44+
where TService : class, IDataLoader
45+
where TImplementation : class, TService
46+
{
47+
builder.Services.AddSingleton(new DataLoaderRegistration(typeof(TService), typeof(TImplementation), sp => factory(sp)));
48+
builder.Services.TryAddScoped<TImplementation>(sp => sp.GetDataLoader<TImplementation>());
49+
builder.Services.TryAddScoped<TService>(sp => sp.GetDataLoader<TService>());
50+
return builder;
51+
}
4052
}
4153

4254
file static class DataLoaderServiceProviderExtensions

src/HotChocolate/Core/test/Execution.Tests/Integration/DataLoader/DataLoaderTests.cs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,67 @@ await executor.ExecuteAsync(
472472
await snapshot.MatchMarkdownAsync();
473473
}
474474

475+
[Fact]
476+
public async Task ClassDataLoader_Resolve_From_DependencyInjection_Using_Factory()
477+
{
478+
var snapshot = new Snapshot();
479+
480+
// arrange
481+
var executor = await CreateExecutorAsync(
482+
c => c
483+
.AddQueryType<Query>()
484+
.AddDataLoader<ITestDataLoader, TestDataLoader>(sp =>
485+
new TestDataLoader(
486+
sp.GetRequiredService<IBatchScheduler>(),
487+
new DataLoaderOptions()))
488+
.ModifyRequestOptions(o => o.IncludeExceptionDetails = true)
489+
.UseRequest(
490+
next => async context =>
491+
{
492+
await next(context);
493+
494+
var dataLoader = (TestDataLoader)context.Services.GetRequiredService<ITestDataLoader>();
495+
496+
context.Result = OperationResultBuilder
497+
.FromResult(((IOperationResult)context.Result!))
498+
.AddExtension("loads", dataLoader.Loads)
499+
.Build();
500+
})
501+
.UseDefaultPipeline());
502+
503+
// act
504+
snapshot.Add(
505+
await executor.ExecuteAsync(
506+
OperationRequestBuilder.New()
507+
.SetDocument(
508+
@"{
509+
a: dataLoaderWithInterface(key: ""a"")
510+
b: dataLoaderWithInterface(key: ""b"")
511+
}")
512+
.Build()));
513+
514+
snapshot.Add(
515+
await executor.ExecuteAsync(
516+
OperationRequestBuilder.New()
517+
.SetDocument(
518+
@"{
519+
a: dataLoaderWithInterface(key: ""a"")
520+
}")
521+
.Build()));
522+
523+
snapshot.Add(
524+
await executor.ExecuteAsync(
525+
OperationRequestBuilder.New()
526+
.SetDocument(
527+
@"{
528+
c: dataLoaderWithInterface(key: ""c"")
529+
}")
530+
.Build()));
531+
532+
// assert
533+
await snapshot.MatchMarkdownAsync();
534+
}
535+
475536
[LocalFact]
476537
public async Task NestedDataLoader()
477538
{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# ClassDataLoader_Resolve_From_DependencyInjection_Using_Factory
2+
3+
## Result 1
4+
5+
```json
6+
{
7+
"data": {
8+
"a": "a",
9+
"b": "b"
10+
},
11+
"extensions": {
12+
"loads": [
13+
[
14+
"a",
15+
"b"
16+
]
17+
]
18+
}
19+
}
20+
```
21+
22+
## Result 2
23+
24+
```json
25+
{
26+
"data": {
27+
"a": "a"
28+
},
29+
"extensions": {
30+
"loads": [
31+
[
32+
"a"
33+
]
34+
]
35+
}
36+
}
37+
```
38+
39+
## Result 3
40+
41+
```json
42+
{
43+
"data": {
44+
"c": "c"
45+
},
46+
"extensions": {
47+
"loads": [
48+
[
49+
"c"
50+
]
51+
]
52+
}
53+
}
54+
```
55+

0 commit comments

Comments
 (0)