Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ public static IRequestExecutorBuilder AddHttpRequestInterceptor<T>(

return builder.ConfigureSchemaServices(s => s
.RemoveAll<IHttpRequestInterceptor>()
.AddSingleton<IHttpRequestInterceptor, T>(sp =>
ActivatorUtilities.GetServiceOrCreateInstance<T>(sp.GetCombinedServices())));
.AddSingleton<IHttpRequestInterceptor, T>());
}

/// <summary>
Expand Down Expand Up @@ -60,7 +59,7 @@ public static IRequestExecutorBuilder AddHttpRequestInterceptor<T>(

return builder.ConfigureSchemaServices(s => s
.RemoveAll<IHttpRequestInterceptor>()
.AddSingleton<IHttpRequestInterceptor, T>(sp => factory(sp.GetCombinedServices())));
.AddSingleton<IHttpRequestInterceptor, T>(factory));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ public static IRequestExecutorBuilder AddSocketSessionInterceptor<T>(
where T : class, ISocketSessionInterceptor =>
builder.ConfigureSchemaServices(s => s
.RemoveAll<ISocketSessionInterceptor>()
.AddSingleton<ISocketSessionInterceptor, T>(
sp => ActivatorUtilities.GetServiceOrCreateInstance<T>(sp.GetCombinedServices())));
.AddSingleton<ISocketSessionInterceptor, T>());

/// <summary>
/// Adds an interceptor for GraphQL socket sessions to the GraphQL configuration.
Expand All @@ -52,7 +51,7 @@ public static IRequestExecutorBuilder AddSocketSessionInterceptor<T>(
where T : class, ISocketSessionInterceptor =>
builder.ConfigureSchemaServices(s => s
.RemoveAll<ISocketSessionInterceptor>()
.AddSingleton<ISocketSessionInterceptor, T>(sp => factory(sp.GetCombinedServices())));
.AddSingleton<ISocketSessionInterceptor, T>(factory));

private static IRequestExecutorBuilder AddSubscriptionServices(
this IRequestExecutorBuilder builder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public static IRequestExecutorBuilder AddErrorFilter<T>(
ArgumentNullException.ThrowIfNull(factory);

return builder.ConfigureSchemaServices(
s => s.AddSingleton<IErrorFilter, T>(
sp => factory(sp.GetCombinedServices())));
s => s.AddSingleton<IErrorFilter, T>(factory));
}

public static IRequestExecutorBuilder AddErrorFilter<T>(
Expand All @@ -42,8 +41,7 @@ public static IRequestExecutorBuilder AddErrorFilter<T>(

builder.Services.TryAddSingleton<T>();
return builder.ConfigureSchemaServices(
s => s.AddSingleton<IErrorFilter, T>(
sp => sp.GetRootServiceProvider().GetRequiredService<T>()));
s => s.AddSingleton<IErrorFilter, T>());
}

public static IServiceCollection AddErrorFilter(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ public static IRequestExecutorBuilder AddDiagnosticEventListener<T>(
{
builder.Services.TryAddSingleton<T>();
builder.ConfigureSchemaServices(
s => s.AddSingleton(
sp => (IExecutionDiagnosticEventListener)sp.GetRootServiceProvider().GetRequiredService<T>()));
static s => s.AddSingleton(
static sp => (IExecutionDiagnosticEventListener)sp.GetRequiredService<T>()));
}
else if (typeof(IDataLoaderDiagnosticEventListener).IsAssignableFrom(typeof(T)))
{
builder.Services.TryAddSingleton<T>();
builder.Services.AddSingleton(s => (IDataLoaderDiagnosticEventListener)s.GetRequiredService<T>());
builder.Services.AddSingleton(
static s => (IDataLoaderDiagnosticEventListener)s.GetRequiredService<T>());
}
else if (typeof(T).IsDefined(typeof(DiagnosticEventSourceAttribute), true))
{
Expand All @@ -35,7 +36,7 @@ public static IRequestExecutorBuilder AddDiagnosticEventListener<T>(
{
var attribute = typeof(T).GetCustomAttributes(typeof(DiagnosticEventSourceAttribute), true).First();
var listener = ((DiagnosticEventSourceAttribute)attribute).Listener;
s.AddSingleton(listener, sp => sp.GetRootServiceProvider().GetRequiredService<T>());
s.AddSingleton(listener, sp => sp.GetRequiredService<T>());
});
}
else
Expand All @@ -48,23 +49,21 @@ public static IRequestExecutorBuilder AddDiagnosticEventListener<T>(

public static IRequestExecutorBuilder AddDiagnosticEventListener<T>(
this IRequestExecutorBuilder builder,
Func<IServiceProvider, T> diagnosticEventListener)
Func<IServiceProvider, T> factory)
where T : class
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(diagnosticEventListener);
ArgumentNullException.ThrowIfNull(factory);

if (typeof(IExecutionDiagnosticEventListener).IsAssignableFrom(typeof(T)))
{
builder.ConfigureSchemaServices(
s => s.AddSingleton(
sp => (IExecutionDiagnosticEventListener)diagnosticEventListener(
sp.GetCombinedServices())));
s => s.AddSingleton(sp => (IExecutionDiagnosticEventListener)factory(sp)));
}
else if (typeof(IDataLoaderDiagnosticEventListener).IsAssignableFrom(typeof(T)))
{
builder.Services.AddSingleton(
s => (IDataLoaderDiagnosticEventListener)diagnosticEventListener(s));
s => (IDataLoaderDiagnosticEventListener)factory(s));
}
else if (typeof(T).IsDefined(typeof(DiagnosticEventSourceAttribute), true))
{
Expand All @@ -78,13 +77,13 @@ public static IRequestExecutorBuilder AddDiagnosticEventListener<T>(
builder.ConfigureSchemaServices(s =>
{
var listener = attribute.Listener;
s.AddSingleton(listener, sp => diagnosticEventListener(sp.GetCombinedServices()));
s.AddSingleton(listener, factory);
});
}
else
{
var listener = attribute.Listener;
builder.Services.AddSingleton(listener, diagnosticEventListener);
builder.Services.AddSingleton(listener, factory);
}
}
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ public static IRequestExecutorBuilder AddOperationCompilerOptimizer<T>(
ArgumentNullException.ThrowIfNull(builder);

builder.ConfigureSchemaServices(
sc => sc.AddSingleton<IOperationCompilerOptimizer>(
sp => factory(sp.GetCombinedServices())));
sc => sc.AddSingleton<IOperationCompilerOptimizer>(factory));
return builder;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,10 @@ public static IRequestExecutorBuilder AddTransactionScopeHandler<T>(

return ConfigureSchemaServices(
builder,
services =>
static services =>
{
// we remove all handlers from the schema DI
services.RemoveAll(typeof(ITransactionScopeHandler));

// and then reference the transaction scope handler from the global DI.
services.AddSingleton<ITransactionScopeHandler>(
s => s.GetRootServiceProvider().GetRequiredService<T>());
services.RemoveAll<ITransactionScopeHandler>();
services.AddSingleton<ITransactionScopeHandler, T>();
});
}

Expand All @@ -50,7 +46,7 @@ public static IRequestExecutorBuilder AddTransactionScopeHandler<T>(
/// <param name="builder">
/// The request executor builder.
/// </param>
/// <param name="create">
/// <param name="factory">
/// A factory to create the transaction scope.
/// </param>
/// <returns>
Expand All @@ -59,17 +55,17 @@ public static IRequestExecutorBuilder AddTransactionScopeHandler<T>(
/// <exception cref="ArgumentNullException"></exception>
public static IRequestExecutorBuilder AddTransactionScopeHandler(
this IRequestExecutorBuilder builder,
Func<IServiceProvider, ITransactionScopeHandler> create)
Func<IServiceProvider, ITransactionScopeHandler> factory)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(create);
ArgumentNullException.ThrowIfNull(factory);

return ConfigureSchemaServices(
builder,
services =>
{
services.RemoveAll(typeof(ITransactionScopeHandler));
services.AddSingleton(sp => create(sp.GetCombinedServices()));
services.RemoveAll<ITransactionScopeHandler>();
services.AddSingleton(factory);
});
}

Expand Down Expand Up @@ -101,10 +97,7 @@ internal static IRequestExecutorBuilder TryAddNoOpTransactionScopeHandler(

return ConfigureSchemaServices(
builder,
services =>
{
services.TryAddSingleton<ITransactionScopeHandler>(
sp => sp.GetRootServiceProvider().GetRequiredService<NoOpTransactionScopeHandler>());
});
static services =>
services.TryAddSingleton<ITransactionScopeHandler, NoOpTransactionScopeHandler>());
}
}
3 changes: 2 additions & 1 deletion src/HotChocolate/Core/src/Types/SchemaBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,12 @@ public ISchemaBuilder SetTypeResolver(IsOfTypeFallback isOfType)
}

/// <inheritdoc />
// TODO: Should this be just SetServices?
public ISchemaBuilder AddServices(IServiceProvider services)
{
ArgumentNullException.ThrowIfNull(services);

_services = _services is null ? services : new CombinedServiceProvider(_services, services);
_services = services;

return this;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using HotChocolate.Fusion.Configuration;

namespace Microsoft.Extensions.DependencyInjection;

public static partial class CoreFusionGatewayBuilderExtensions
{
/// <summary>
/// Resolves an instance of <typeparamref name="TService"/> from the application
/// service provider and makes it available as a singleton through the schema
/// service provider.
/// </summary>
/// <typeparam name="TService">
/// The type of service.
/// </typeparam>
public static IFusionGatewayBuilder AddApplicationService<TService>(
this IFusionGatewayBuilder builder)
where TService : class
{
return builder.ConfigureSchemaServices(
static (sp, sc) => sc.AddSingleton(sp.GetRequiredService<TService>()));
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -363,9 +363,6 @@ private void AddCoreServices(
FusionOptions options,
FusionRequestOptions requestOptions)
{
services.AddSingleton<IRootServiceProviderAccessor>(
new RootServiceProviderAccessor(_applicationServices));

services.AddSingleton(static _ => new RequestExecutorAccessor());
services.AddSingleton(static sp => sp.GetRequiredService<RequestExecutorAccessor>().RequestExecutor);
services.AddSingleton<IRequestExecutor>(sp => sp.GetRequiredService<FusionRequestExecutor>());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ public async Task Executor_Resolution_Should_Be_Parallel()
cts.Dispose();
}

[Fact(Skip = "SomeService needs to be registered with the schema services")]
[Fact]
public async Task WarmupTask_Should_Be_Able_To_Access_Schema_And_Regular_Services()
{
// arrange
Expand All @@ -398,6 +398,7 @@ public async Task WarmupTask_Should_Be_Able_To_Access_Schema_And_Regular_Service
services
.AddGraphQLGateway()
.AddInMemoryConfiguration(CreateConfiguration().Schema)
.AddApplicationService<SomeService>()
.AddWarmupTask<CustomWarmupTask>();
var provider = services.BuildServiceProvider();
var manager = provider.GetRequiredService<FusionRequestExecutorManager>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ public static IRequestExecutorBuilder AddAzureBlobStorageOperationDocumentStorag
ArgumentNullException.ThrowIfNull(containerClientFactory);

return builder.ConfigureSchemaServices(
s => s.AddAzureBlobStorageOperationDocumentStorage(
sp => containerClientFactory(sp.GetCombinedServices())));
s => s.AddAzureBlobStorageOperationDocumentStorage(containerClientFactory));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ public static IRequestExecutorBuilder AddRedisOperationDocumentStorage(
ArgumentNullException.ThrowIfNull(databaseFactory);

return builder.ConfigureSchemaServices(
s => s.AddRedisOperationDocumentStorage(
sp => databaseFactory(sp.GetCombinedServices()),
queryExpiration));
s => s.AddRedisOperationDocumentStorage(databaseFactory, queryExpiration));
}

/// <summary>
Expand All @@ -58,7 +56,7 @@ public static IRequestExecutorBuilder AddRedisOperationDocumentStorage(

return builder.ConfigureSchemaServices(
s => s.AddRedisOperationDocumentStorage(
sp => multiplexerFactory(sp.GetCombinedServices()).GetDatabase(),
sp => multiplexerFactory(sp).GetDatabase(),
queryExpiration));
}

Expand Down
Loading