Skip to content

Commit caba57d

Browse files
Cleanup
1 parent 4cd5f2e commit caba57d

File tree

12 files changed

+96
-30
lines changed

12 files changed

+96
-30
lines changed

src/HotChocolate/AspNetCore/src/AspNetCore/Warmup/RequestExecutorWarmupService.cs

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public async Task StartAsync(CancellationToken cancellationToken)
1414
foreach (var schemaName in provider.SchemaNames)
1515
{
1616
var setup = await executorOptionsMonitor.GetAsync(schemaName, cancellationToken);
17-
var options = CreateSchemaOptions(setup);
17+
var options = setup.CreateSchemaOptions();
1818

1919
if (!options.LazyInitialization)
2020
{
@@ -32,16 +32,4 @@ private async Task WarmupAsync(string schemaName, CancellationToken cancellation
3232
{
3333
await provider.GetExecutorAsync(schemaName, cancellationToken).ConfigureAwait(false);
3434
}
35-
36-
private static SchemaOptions CreateSchemaOptions(RequestExecutorSetup setup)
37-
{
38-
var options = new SchemaOptions();
39-
40-
foreach (var configure in setup.SchemaOptionModifiers)
41-
{
42-
configure(options);
43-
}
44-
45-
return options;
46-
}
4735
}

src/HotChocolate/Core/src/Execution/Configuration/DefaultRequestExecutorOptionsMonitor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
namespace HotChocolate.Execution.Configuration;
55

6+
// TODO: Do we still need this?
67
internal sealed class DefaultRequestExecutorOptionsMonitor(
78
IOptionsMonitor<RequestExecutorSetup> optionsMonitor,
89
IEnumerable<IRequestExecutorOptionsProvider> optionsProviders)

src/HotChocolate/Core/src/Execution/Configuration/RequestExecutorSetup.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,16 @@ public void CopyTo(RequestExecutorSetup options)
134134
options.DefaultPipelineFactory = DefaultPipelineFactory;
135135
}
136136
}
137+
138+
internal SchemaOptions CreateSchemaOptions()
139+
{
140+
var options = new SchemaOptions();
141+
142+
foreach (var configure in SchemaOptionModifiers)
143+
{
144+
configure(options);
145+
}
146+
147+
return options;
148+
}
137149
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using HotChocolate;
2+
using HotChocolate.Execution.Caching;
3+
using HotChocolate.Execution.Configuration;
4+
using HotChocolate.Language;
5+
using Microsoft.Extensions.DependencyInjection.Extensions;
6+
using Microsoft.Extensions.Options;
7+
8+
namespace Microsoft.Extensions.DependencyInjection;
9+
10+
public static partial class RequestExecutorBuilderExtensions
11+
{
12+
internal static IRequestExecutorBuilder AddDocumentCache(this IRequestExecutorBuilder builder)
13+
{
14+
builder.Services.TryAddKeyedSingleton<IDocumentCache>(
15+
builder.Name,
16+
static (sp, schemaName) =>
17+
{
18+
var optionsMonitor = sp.GetRequiredService<IOptionsMonitor<RequestExecutorSetup>>();
19+
var setup = optionsMonitor.Get((string)schemaName!);
20+
var options = setup.CreateSchemaOptions();
21+
22+
return new DefaultDocumentCache(options.OperationDocumentCacheSize);
23+
});
24+
25+
return builder.ConfigureSchemaServices(
26+
static (applicationServices, s) =>
27+
s.AddSingleton<IDocumentCache>(schemaServices =>
28+
{
29+
var schemaName = schemaServices.GetRequiredService<ISchemaDefinition>().Name;
30+
return applicationServices.GetRequiredKeyedService<IDocumentCache>(schemaName);
31+
}));
32+
}
33+
}

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorServiceCollectionExtensions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,8 @@ private static DefaultRequestExecutorBuilder CreateBuilder(
155155
builder.TryAddTypeInterceptor<DataLoaderRootFieldTypeInterceptor>();
156156
builder.TryAddTypeInterceptor<RequirementsTypeInterceptor>();
157157

158+
builder.AddDocumentCache();
159+
158160
if (!services.Any(t =>
159161
t.ServiceType == typeof(SchemaName)
160162
&& t.ImplementationInstance is SchemaName s

src/HotChocolate/Core/src/Execution/HotChocolate.Execution.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,9 @@
229229
<Compile Update="RequestExecutorManager.Warmup.cs">
230230
<DependentUpon>RequestExecutorManager.cs</DependentUpon>
231231
</Compile>
232+
<Compile Update="DependencyInjection\RequestExecutorBuilderExtensions.Caches.cs">
233+
<DependentUpon>RequestExecutorBuilderExtensions.cs</DependentUpon>
234+
</Compile>
232235
</ItemGroup>
233236

234237
<ItemGroup>

src/HotChocolate/Core/src/Execution/RequestExecutorManager.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ await _optionsMonitor.GetAsync(schemaName, cancellationToken)
172172
}
173173

174174
var schemaServices =
175-
await CreateSchemaServicesAsync(context, setup, options, typeModuleChangeMonitor, cancellationToken)
175+
await CreateSchemaServicesAsync(context, setup, typeModuleChangeMonitor, cancellationToken)
176176
.ConfigureAwait(false);
177177

178178
var registeredExecutor = new RegisteredExecutor(
@@ -242,7 +242,6 @@ private static async Task RunEvictionEvents(RegisteredExecutor registeredExecuto
242242
private async Task<ServiceProvider> CreateSchemaServicesAsync(
243243
ConfigurationContext context,
244244
RequestExecutorSetup setup,
245-
SchemaOptions schemaOptions,
246245
TypeModuleChangeMonitor typeModuleChangeMonitor,
247246
CancellationToken cancellationToken)
248247
{
@@ -269,7 +268,6 @@ await typeModuleChangeMonitor.ConfigureAsync(context, cancellationToken)
269268

270269
serviceCollection.AddSingleton(new SchemaVersionInfo(version));
271270

272-
serviceCollection.AddSingleton(schemaOptions);
273271
serviceCollection.AddSingleton(executorOptions);
274272
serviceCollection.AddSingleton<IRequestExecutorOptionsAccessor>(
275273
static sp => sp.GetRequiredService<RequestExecutorOptions>());
@@ -285,7 +283,7 @@ await typeModuleChangeMonitor.ConfigureAsync(context, cancellationToken)
285283
serviceCollection.AddSingleton<IPreparedOperationCache>(
286284
static sp =>
287285
{
288-
var options = sp.GetRequiredService<SchemaOptions>();
286+
var options = sp.GetRequiredService<ISchemaDefinition>().GetOptions();
289287
return new DefaultPreparedOperationCache(options.PreparedOperationCacheSize);
290288
});
291289

@@ -294,12 +292,6 @@ await typeModuleChangeMonitor.ConfigureAsync(context, cancellationToken)
294292
static sp => sp.GetRootServiceProvider().GetRequiredService<ParserOptions>());
295293
serviceCollection.AddSingleton(
296294
static sp => sp.GetRootServiceProvider().GetRequiredService<IDocumentHashProvider>());
297-
serviceCollection.AddSingleton<IDocumentCache>(
298-
static sp =>
299-
{
300-
var options = sp.GetRequiredService<SchemaOptions>();
301-
return new DefaultDocumentCache(options.OperationDocumentCacheSize);
302-
});
303295

304296
serviceCollection.TryAddDiagnosticEvents();
305297
serviceCollection.TryAddOperationExecutors();

src/HotChocolate/Core/src/Types/Extensions/SchemaExtensions.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Diagnostics.CodeAnalysis;
2+
using HotChocolate.Features;
23
using HotChocolate.Language;
34
using HotChocolate.Types;
45
using TypeThrowHelper = HotChocolate.Utilities.ThrowHelper;
@@ -295,4 +296,18 @@ public static ITypeSystemMember GetMember(
295296

296297
throw TypeThrowHelper.Schema_GetMember_TypeNotFound(coordinate);
297298
}
299+
300+
/// <summary>
301+
/// Gets the <see cref="IReadOnlySchemaOptions"/> of the <paramref name="schema"/>.
302+
/// </summary>
303+
/// <param name="schema">
304+
/// The schema to get the options for.
305+
/// </param>
306+
/// <returns>
307+
/// The schema options.
308+
/// </returns>
309+
internal static IReadOnlySchemaOptions GetOptions(this ISchemaDefinition schema)
310+
{
311+
return schema.Features.GetRequired<IReadOnlySchemaOptions>();
312+
}
298313
}

src/HotChocolate/Core/src/Types/SchemaBuilder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ private SchemaBuilder()
3636
typeInterceptors.TryAdd(new MiddlewareValidationTypeInterceptor());
3737
typeInterceptors.TryAdd(new SemanticNonNullTypeInterceptor());
3838
typeInterceptors.TryAdd(new StoreGlobalPagingOptionsTypeInterceptor());
39+
typeInterceptors.TryAdd(new StoreGlobalSchemaOptionsTypeInterceptor());
3940

4041
Features.Set(typeInterceptors);
4142
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using HotChocolate.Configuration;
2+
using HotChocolate.Types.Descriptors.Configurations;
3+
4+
namespace HotChocolate;
5+
6+
internal sealed class StoreGlobalSchemaOptionsTypeInterceptor : TypeInterceptor
7+
{
8+
public override void OnBeforeCompleteType(
9+
ITypeCompletionContext completionContext,
10+
TypeSystemConfiguration configuration)
11+
{
12+
if (configuration is SchemaTypeConfiguration schemaDef)
13+
{
14+
var options = completionContext.DescriptorContext.Options;
15+
schemaDef.Features.Set(options);
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)