From 916b7714162cf5e7b098aa686eabf14e21d235da Mon Sep 17 00:00:00 2001 From: Michael Staib Date: Sat, 6 May 2023 16:59:56 +0300 Subject: [PATCH] Fixed SetOptions. (#6123) --- .../Core/src/Types/SchemaBuilder.Setup.cs | 2 +- .../Conventions/DescriptorContext.cs | 29 ++++++++++---- ...torBuilderExtensions_SchemaOptionsTests.cs | 40 +++++++++++++++++++ 3 files changed, 62 insertions(+), 9 deletions(-) create mode 100644 src/HotChocolate/Core/test/Execution.Tests/DependencyInjection/RequestExecutorBuilderExtensions_SchemaOptionsTests.cs diff --git a/src/HotChocolate/Core/src/Types/SchemaBuilder.Setup.cs b/src/HotChocolate/Core/src/Types/SchemaBuilder.Setup.cs index 02bdc9a6531..71fc1340b0b 100644 --- a/src/HotChocolate/Core/src/Types/SchemaBuilder.Setup.cs +++ b/src/HotChocolate/Core/src/Types/SchemaBuilder.Setup.cs @@ -84,7 +84,7 @@ public static DescriptorContext CreateContext( var typeInterceptor = new AggregateTypeInterceptor(); var context = DescriptorContext.Create( - builder._options, + () => builder._options, services, builder._conventions, builder._contextData, diff --git a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DescriptorContext.cs b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DescriptorContext.cs index 0876feb2799..3771daea21f 100644 --- a/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DescriptorContext.cs +++ b/src/HotChocolate/Core/src/Types/Types/Descriptors/Conventions/DescriptorContext.cs @@ -27,9 +27,9 @@ public sealed partial class DescriptorContext : IDescriptorContext private readonly Dictionary<(Type, string?), IConvention> _conventionInstances = new(); private readonly IReadOnlyDictionary<(Type, string?), List> _conventions; private readonly Dictionary _schemaDirectives = new(); - private readonly IServiceProvider _schemaServices; private readonly ServiceHelper _serviceHelper; + private readonly Func _options; private TypeDiscoveryHandler[]? _typeDiscoveryHandlers; private INamingConventions? _naming; @@ -38,7 +38,7 @@ public sealed partial class DescriptorContext : IDescriptorContext public event EventHandler? SchemaCompleted; private DescriptorContext( - IReadOnlySchemaOptions options, + Func options, IReadOnlyDictionary<(Type, string?), List> conventions, IServiceProvider schemaServices, IDictionary contextData, @@ -46,8 +46,8 @@ private DescriptorContext( TypeInterceptor typeInterceptor) { + _options = options; Schema = schema; - Options = options; _conventions = conventions; _schemaServices = schemaServices; _serviceHelper = new ServiceHelper(_schemaServices); @@ -75,7 +75,7 @@ void OnSchemaOnCompleted(object? sender, EventArgs args) public IServiceProvider Services => _schemaServices; /// - public IReadOnlySchemaOptions Options { get; } + public IReadOnlySchemaOptions Options => _options(); /// public INamingConventions Naming @@ -284,15 +284,28 @@ internal static DescriptorContext Create( IDictionary? contextData = null, SchemaBuilder.LazySchema? schema = null, TypeInterceptor? typeInterceptor = null) - { - return new( - options ?? new SchemaOptions(), + => new DescriptorContext( + () => (options ??= new SchemaOptions()), + conventions ?? new Dictionary<(Type, string?), List>(), + services ?? new EmptyServiceProvider(), + contextData ?? new Dictionary(), + schema ?? new SchemaBuilder.LazySchema(), + typeInterceptor ?? new AggregateTypeInterceptor()); + + internal static DescriptorContext Create( + Func options, + IServiceProvider? services = null, + IReadOnlyDictionary<(Type, string?), List>? conventions = null, + IDictionary? contextData = null, + SchemaBuilder.LazySchema? schema = null, + TypeInterceptor? typeInterceptor = null) + => new DescriptorContext( + options, conventions ?? new Dictionary<(Type, string?), List>(), services ?? new EmptyServiceProvider(), contextData ?? new Dictionary(), schema ?? new SchemaBuilder.LazySchema(), typeInterceptor ?? new AggregateTypeInterceptor()); - } private sealed class NoOpStringBuilderPool : ObjectPool { diff --git a/src/HotChocolate/Core/test/Execution.Tests/DependencyInjection/RequestExecutorBuilderExtensions_SchemaOptionsTests.cs b/src/HotChocolate/Core/test/Execution.Tests/DependencyInjection/RequestExecutorBuilderExtensions_SchemaOptionsTests.cs new file mode 100644 index 00000000000..08f4bd292f1 --- /dev/null +++ b/src/HotChocolate/Core/test/Execution.Tests/DependencyInjection/RequestExecutorBuilderExtensions_SchemaOptionsTests.cs @@ -0,0 +1,40 @@ +using HotChocolate.Configuration; +using HotChocolate.Types.Descriptors; +using Microsoft.Extensions.DependencyInjection; + +namespace HotChocolate.Execution.DependencyInjection; + +public class RequestExecutorBuilderExtensionsSchemaOptionsTests +{ + [Fact] + public async Task SetOptions_ValidatePipelineOrder_False() + { + var interceptor = new OptionsInterceptor(); + + await new ServiceCollection() + .AddGraphQLServer() + .AddType() + .SetOptions(new SchemaOptions { ValidatePipelineOrder = false }) + .TryAddTypeInterceptor(interceptor) + .BuildRequestExecutorAsync(); + + Assert.False(interceptor.Options.ValidatePipelineOrder); + } + + private sealed class OptionsInterceptor : TypeInterceptor + { + public IReadOnlySchemaOptions Options { get; private set; } = default!; + + public override void OnBeforeCreateSchema( + IDescriptorContext context, + ISchemaBuilder schemaBuilder) + { + Options = context.Options; + } + } + + public class Query + { + public string Abc() => "abc"; + } +}