Skip to content
Merged
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
@@ -1,10 +1,11 @@
using HotChocolate.Execution.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Options;

namespace HotChocolate.AspNetCore.Warmup;

internal sealed class RequestExecutorWarmupService(
IRequestExecutorOptionsMonitor executorOptionsMonitor,
IOptionsMonitor<RequestExecutorSetup> optionsMonitor,
IRequestExecutorProvider provider) : IHostedService
{
public async Task StartAsync(CancellationToken cancellationToken)
Expand All @@ -13,8 +14,8 @@ public async Task StartAsync(CancellationToken cancellationToken)

foreach (var schemaName in provider.SchemaNames)
{
var setup = await executorOptionsMonitor.GetAsync(schemaName, cancellationToken);
var options = CreateSchemaOptions(setup);
var setup = optionsMonitor.Get(schemaName);
var options = setup.CreateSchemaOptions();

if (!options.LazyInitialization)
{
Expand All @@ -32,16 +33,4 @@ private async Task WarmupAsync(string schemaName, CancellationToken cancellation
{
await provider.GetExecutorAsync(schemaName, cancellationToken).ConfigureAwait(false);
}

private static SchemaOptions CreateSchemaOptions(RequestExecutorSetup setup)
{
var options = new SchemaOptions();

foreach (var configure in setup.SchemaOptionModifiers)
{
configure(options);
}

return options;
}
}

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,16 @@ public void CopyTo(RequestExecutorSetup options)
options.DefaultPipelineFactory = DefaultPipelineFactory;
}
}

internal SchemaOptions CreateSchemaOptions()
{
var options = new SchemaOptions();

foreach (var configure in SchemaOptionModifiers)
{
configure(options);
}

return options;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,6 @@ namespace Microsoft.Extensions.DependencyInjection;

internal static class InternalServiceCollectionExtensions
{
internal static IServiceCollection TryAddRequestExecutorFactoryOptionsMonitor(
this IServiceCollection services)
{
services.TryAddSingleton<IRequestExecutorOptionsMonitor>(
sp => new DefaultRequestExecutorOptionsMonitor(
sp.GetRequiredService<IOptionsMonitor<RequestExecutorSetup>>(),
sp.GetServices<IRequestExecutorOptionsProvider>()));
return services;
}

internal static IServiceCollection TryAddVariableCoercion(
this IServiceCollection services)
{
Expand Down Expand Up @@ -159,16 +149,6 @@ internal static IServiceCollection TryAddRequestExecutorResolver(
return services;
}

internal static IServiceCollection TryAddDefaultCaches(
this IServiceCollection services)
{
services.TryAddSingleton(_ => new PreparedOperationCacheOptions { Capacity = 256 });
services.TryAddSingleton<IDocumentCache>(
sp => new DefaultDocumentCache(
sp.GetRequiredService<PreparedOperationCacheOptions>().Capacity));
return services;
}

internal static IServiceCollection TryAddDefaultDocumentHashProvider(
this IServiceCollection services)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using HotChocolate;
using HotChocolate.Execution.Caching;
using HotChocolate.Execution.Configuration;
using HotChocolate.Language;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;

namespace Microsoft.Extensions.DependencyInjection;

public static partial class RequestExecutorBuilderExtensions
{
internal static IRequestExecutorBuilder AddDocumentCache(this IRequestExecutorBuilder builder)
{
builder.Services.TryAddKeyedSingleton<IDocumentCache>(
builder.Name,
static (sp, schemaName) =>
{
var optionsMonitor = sp.GetRequiredService<IOptionsMonitor<RequestExecutorSetup>>();
var setup = optionsMonitor.Get((string)schemaName!);
var options = setup.CreateSchemaOptions();

return new DefaultDocumentCache(options.OperationDocumentCacheSize);
});

return builder.ConfigureSchemaServices(
static (applicationServices, s) =>
s.AddSingleton<IDocumentCache>(schemaServices =>
{
var schemaName = schemaServices.GetRequiredService<ISchemaDefinition>().Name;
return applicationServices.GetRequiredKeyedService<IDocumentCache>(schemaName);
}));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,9 @@ public static IServiceCollection AddGraphQLCore(this IServiceCollection services

// core services
services
.TryAddRequestExecutorFactoryOptionsMonitor()
.TryAddTypeConverter()
.TryAddInputFormatter()
.TryAddInputParser()
.TryAddDefaultCaches()
.TryAddDefaultDocumentHashProvider()
.TryAddDefaultBatchDispatcher()
.TryAddDefaultDataLoaderRegistry()
Expand Down Expand Up @@ -156,6 +154,8 @@ private static DefaultRequestExecutorBuilder CreateBuilder(
builder.TryAddTypeInterceptor<DataLoaderRootFieldTypeInterceptor>();
builder.TryAddTypeInterceptor<RequirementsTypeInterceptor>();

builder.AddDocumentCache();

if (!services.Any(t =>
t.ServiceType == typeof(SchemaName)
&& t.ImplementationInstance is SchemaName s
Expand All @@ -169,25 +169,6 @@ private static DefaultRequestExecutorBuilder CreateBuilder(
return builder;
}

public static IServiceCollection AddDocumentCache(
this IServiceCollection services,
int capacity = 256)
{
services.RemoveAll<IDocumentCache>();
services.AddSingleton<IDocumentCache>(
_ => new DefaultDocumentCache(capacity));
return services;
}

public static IServiceCollection AddOperationCache(
this IServiceCollection services,
int capacity = 256)
{
services.RemoveAll<PreparedOperationCacheOptions>();
services.AddSingleton(_ => new PreparedOperationCacheOptions { Capacity = capacity });
return services;
}

public static IServiceCollection AddMD5DocumentHashProvider(
this IServiceCollection services,
HashFormat format = HashFormat.Base64)
Expand Down
Loading
Loading