Skip to content

Commit

Permalink
Added "AddDataLoader<TService, TImplementation>" executor overload th…
Browse files Browse the repository at this point in the history
…at takes a factory (#7803)
  • Loading branch information
danielreynolds1 authored Dec 3, 2024
1 parent e9b607d commit 1b0e798
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ public static IRequestExecutorBuilder AddDataLoader<T>(
builder.Services.TryAddScoped<T>(sp => sp.GetDataLoader<T>());
return builder;
}

public static IRequestExecutorBuilder AddDataLoader<TService, TImplementation>(
this IRequestExecutorBuilder builder,
Func<IServiceProvider, TImplementation> factory)
where TService : class, IDataLoader
where TImplementation : class, TService
{
builder.Services.AddSingleton(new DataLoaderRegistration(typeof(TService), typeof(TImplementation), sp => factory(sp)));
builder.Services.TryAddScoped<TImplementation>(sp => sp.GetDataLoader<TImplementation>());
builder.Services.TryAddScoped<TService>(sp => sp.GetDataLoader<TService>());
return builder;
}
}

file static class DataLoaderServiceProviderExtensions
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,67 @@ await executor.ExecuteAsync(
await snapshot.MatchMarkdownAsync();
}

[Fact]
public async Task ClassDataLoader_Resolve_From_DependencyInjection_Using_Factory()
{
var snapshot = new Snapshot();

// arrange
var executor = await CreateExecutorAsync(
c => c
.AddQueryType<Query>()
.AddDataLoader<ITestDataLoader, TestDataLoader>(sp =>
new TestDataLoader(
sp.GetRequiredService<IBatchScheduler>(),
new DataLoaderOptions()))
.ModifyRequestOptions(o => o.IncludeExceptionDetails = true)
.UseRequest(
next => async context =>
{
await next(context);

var dataLoader = (TestDataLoader)context.Services.GetRequiredService<ITestDataLoader>();

context.Result = OperationResultBuilder
.FromResult(((IOperationResult)context.Result!))
.AddExtension("loads", dataLoader.Loads)
.Build();
})
.UseDefaultPipeline());

// act
snapshot.Add(
await executor.ExecuteAsync(
OperationRequestBuilder.New()
.SetDocument(
@"{
a: dataLoaderWithInterface(key: ""a"")
b: dataLoaderWithInterface(key: ""b"")
}")
.Build()));

snapshot.Add(
await executor.ExecuteAsync(
OperationRequestBuilder.New()
.SetDocument(
@"{
a: dataLoaderWithInterface(key: ""a"")
}")
.Build()));

snapshot.Add(
await executor.ExecuteAsync(
OperationRequestBuilder.New()
.SetDocument(
@"{
c: dataLoaderWithInterface(key: ""c"")
}")
.Build()));

// assert
await snapshot.MatchMarkdownAsync();
}

[LocalFact]
public async Task NestedDataLoader()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# ClassDataLoader_Resolve_From_DependencyInjection_Using_Factory

## Result 1

```json
{
"data": {
"a": "a",
"b": "b"
},
"extensions": {
"loads": [
[
"a",
"b"
]
]
}
}
```

## Result 2

```json
{
"data": {
"a": "a"
},
"extensions": {
"loads": [
[
"a"
]
]
}
}
```

## Result 3

```json
{
"data": {
"c": "c"
},
"extensions": {
"loads": [
[
"c"
]
]
}
}
```

0 comments on commit 1b0e798

Please sign in to comment.