Skip to content

Commit

Permalink
Simplify DI Support (#6994)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored Mar 19, 2024
1 parent 717ded9 commit 4450784
Show file tree
Hide file tree
Showing 100 changed files with 1,018 additions and 2,019 deletions.
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"sdk": {
"version": "8.0.202",
"version": "8.0.101",
"rollForward": "latestMajor"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ public static async Task<IRequestExecutor> CreateAsync()
.AddGraphQL()
.AddApolloFederation()
.AddQueryType<Query>()
.RegisterService<Data>()
.BuildRequestExecutorAsync();
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,5 @@ public static async Task<IRequestExecutor> CreateAsync()
.AddGraphQL()
.AddApolloFederation()
.AddQueryType<QueryType>()
.RegisterService<Data>()
.BuildRequestExecutorAsync();
}
10 changes: 0 additions & 10 deletions src/HotChocolate/Core/src/Abstractions/ScopedServiceAttribute.cs

This file was deleted.

18 changes: 2 additions & 16 deletions src/HotChocolate/Core/src/Abstractions/ServiceAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,8 @@ public class ServiceAttribute : Attribute
/// <summary>
/// Marks a resolver parameter as a service that shall be injected by the execution engine.
/// </summary>
/// <param name="kind">
/// The scope of the service.
/// </param>
public ServiceAttribute(ServiceKind kind = ServiceKind.Default)
public ServiceAttribute()
{
Kind = kind;
}

#if NET8_0_OR_GREATER
Expand All @@ -26,24 +22,14 @@ public ServiceAttribute(ServiceKind kind = ServiceKind.Default)
/// <param name="key">
/// A key that shall be used to resolve the service.
/// </param>
/// <param name="kind">
/// The scope of the service.
/// </param>
public ServiceAttribute(string key, ServiceKind kind = ServiceKind.Default)
public ServiceAttribute(string key)
{
Key = key;
Kind = kind;
}

/// <summary>
/// Gets the key that shall be used to resolve the service.
/// </summary>
public string? Key { get; }
#endif

/// <summary>
/// Gets the service kind which specifies the way the service
/// shall be injected and handled by the execution engine.
/// </summary>
public ServiceKind Kind { get; }
}
38 changes: 0 additions & 38 deletions src/HotChocolate/Core/src/Abstractions/ServiceKind.cs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using HotChocolate.Execution.DependencyInjection;
using HotChocolate.Execution.Processing;
using HotChocolate.Execution.Processing.Tasks;
using HotChocolate.Resolvers;
using HotChocolate.Utilities;

namespace Microsoft.Extensions.DependencyInjection;
Expand All @@ -17,28 +18,17 @@ namespace Microsoft.Extensions.DependencyInjection;
///
/// The <see cref="OperationContextFactory"/> MUST be a singleton.
/// </summary>
internal sealed class OperationContextFactory : IFactory<OperationContext>
internal sealed class OperationContextFactory(
IFactory<ResolverTask> resolverTaskFactory,
ResultPool resultPool,
ITypeConverter typeConverter,
AggregateServiceScopeInitializer serviceScopeInitializer)
: IFactory<OperationContext>
{
private readonly IFactory<ResolverTask> _resolverTaskFactory;
private readonly ResultPool _resultPool;
private readonly ITypeConverter _typeConverter;

public OperationContextFactory(
IFactory<ResolverTask> resolverTaskFactory,
ResultPool resultPool,
ITypeConverter typeConverter)
{
_resolverTaskFactory = resolverTaskFactory ??
throw new ArgumentNullException(nameof(resolverTaskFactory));
_resultPool = resultPool ??
throw new ArgumentNullException(nameof(resultPool));
_typeConverter = typeConverter ??
throw new ArgumentNullException(nameof(typeConverter));
}

public OperationContext Create()
=> new OperationContext(
_resolverTaskFactory,
new ResultBuilder(_resultPool),
_typeConverter);
resolverTaskFactory,
new ResultBuilder(resultPool),
typeConverter,
serviceScopeInitializer);
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using HotChocolate.Execution.Configuration;
using HotChocolate.Resolvers;

// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection;

public static partial class RequestExecutorBuilderExtensions
{
/// <summary>
/// Adds a initializer to copy state between a request scoped service instance and
/// a resolver scoped service instance.
/// </summary>
/// <param name="builder">
/// The request executor builder.
/// </param>
/// <param name="initializer">
/// The initializer that is used to initialize the service scope.
/// </param>
/// <typeparam name="TService">
/// The type of the service that shall be initialized.
/// </typeparam>
/// <returns></returns>
/// <exception cref="ArgumentNullException">
/// The <paramref name="builder"/> is <c>null</c>.
/// </exception>
public static IRequestExecutorBuilder AddScopedServiceInitializer<TService>(
this IRequestExecutorBuilder builder,
Action<TService, TService> initializer)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}

if (initializer == null)
{
throw new ArgumentNullException(nameof(initializer));
}

builder.Services.AddSingleton<IServiceScopeInitializer>(new DelegateServiceInitializer<TService>(initializer));
return builder;
}
}

file sealed class DelegateServiceInitializer<TService>(
Action<TService, TService> initializer)
: ServiceInitializer<TService>
{
private readonly Action<TService, TService> _initializer = initializer ??
throw new ArgumentNullException(nameof(initializer));

protected override void Initialize(TService requestScopeService, TService resolverScopeService)
=> _initializer(requestScopeService, resolverScopeService);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using HotChocolate.Execution.Processing;
using HotChocolate.Fetching;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.ObjectPool;

Expand All @@ -36,6 +37,7 @@ public static IServiceCollection AddGraphQLCore(this IServiceCollection services
services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
services.TryAddSingleton<DefaultRequestContextAccessor>();
services.TryAddSingleton<IRequestContextAccessor>(sp => sp.GetRequiredService<DefaultRequestContextAccessor>());
services.TryAddSingleton<AggregateServiceScopeInitializer>();

services.TryAddSingleton<ObjectPool<StringBuilder>>(sp =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@
<Compile Update="DependencyInjection\RequestExecutorBuilderExtensions.Optimizer.cs">
<DependentUpon>RequestExecutorBuilderExtensions.cs</DependentUpon>
</Compile>
<Compile Update="DependencyInjection\RequestExecutorBuilderExtensions.ResolverCompiler.cs">
<DependentUpon>RequestExecutorBuilderExtensions.cs</DependentUpon>
</Compile>
<Compile Update="DependencyInjection\RequestExecutorBuilderExtensions.TransactionScope.cs">
<DependentUpon>RequestExecutorBuilderExtensions.cs</DependentUpon>
</Compile>
Expand Down Expand Up @@ -210,6 +207,15 @@
<Compile Update="Batching\BatchExecutor.Enumerable.cs">
<DependentUpon>BatchExecutor.cs</DependentUpon>
</Compile>
<Compile Update="DependencyInjection\RequestExecutorBuilderExtensions.InputParser.cs">
<DependentUpon>RequestExecutorBuilderExtensions.cs</DependentUpon>
</Compile>
<Compile Update="DependencyInjection\RequestExecutorBuilderExtensions.TypeDiscovery.cs">
<DependentUpon>RequestExecutorBuilderExtensions.cs</DependentUpon>
</Compile>
<Compile Update="DependencyInjection\RequestExecutorBuilderExtensions.Services.cs">
<DependentUpon>RequestExecutorBuilderExtensions.cs</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using HotChocolate.Execution.Instrumentation;
using HotChocolate.Execution.Processing.Tasks;
using HotChocolate.Fetching;
using HotChocolate.Resolvers;
using HotChocolate.Types;
using HotChocolate.Utilities;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -19,6 +20,7 @@ internal sealed partial class OperationContext
private readonly WorkScheduler _workScheduler;
private readonly DeferredWorkScheduler _deferredWorkScheduler;
private readonly ResultBuilder _resultBuilder;
private readonly AggregateServiceScopeInitializer _serviceScopeInitializer;
private IRequestContext _requestContext = default!;
private ISchema _schema = default!;
private IErrorHandler _errorHandler = default!;
Expand All @@ -38,12 +40,14 @@ internal sealed partial class OperationContext
public OperationContext(
IFactory<ResolverTask> resolverTaskFactory,
ResultBuilder resultBuilder,
ITypeConverter typeConverter)
ITypeConverter typeConverter,
AggregateServiceScopeInitializer serviceScopeInitializer)
{
_resolverTaskFactory = resolverTaskFactory;
_workScheduler = new(this);
_deferredWorkScheduler = new();
_workScheduler = new WorkScheduler(this);
_deferredWorkScheduler = new DeferredWorkScheduler();
_resultBuilder = resultBuilder;
_serviceScopeInitializer = serviceScopeInitializer;
Converter = typeConverter;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using HotChocolate.Resolvers;
using HotChocolate.Types;

namespace HotChocolate.Execution.Processing;
Expand All @@ -22,4 +23,9 @@ internal sealed partial class OperationContext
/// Gets access to the input parser.
/// </summary>
public InputParser InputParser => _inputParser;

/// <summary>
/// Gets the service scope initializer.
/// </summary>
public AggregateServiceScopeInitializer ServiceScopeInitializer => _serviceScopeInitializer;
}
Loading

0 comments on commit 4450784

Please sign in to comment.