Skip to content

Commit

Permalink
Fixed SetOptions. (#6123)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelstaib authored May 6, 2023
1 parent 26b3433 commit 916b771
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/HotChocolate/Core/src/Types/SchemaBuilder.Setup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public sealed partial class DescriptorContext : IDescriptorContext
private readonly Dictionary<(Type, string?), IConvention> _conventionInstances = new();
private readonly IReadOnlyDictionary<(Type, string?), List<CreateConvention>> _conventions;
private readonly Dictionary<string, ISchemaDirective> _schemaDirectives = new();

private readonly IServiceProvider _schemaServices;
private readonly ServiceHelper _serviceHelper;
private readonly Func<IReadOnlySchemaOptions> _options;

private TypeDiscoveryHandler[]? _typeDiscoveryHandlers;
private INamingConventions? _naming;
Expand All @@ -38,16 +38,16 @@ public sealed partial class DescriptorContext : IDescriptorContext
public event EventHandler<SchemaCompletedEventArgs>? SchemaCompleted;

private DescriptorContext(
IReadOnlySchemaOptions options,
Func<IReadOnlySchemaOptions> options,
IReadOnlyDictionary<(Type, string?), List<CreateConvention>> conventions,
IServiceProvider schemaServices,
IDictionary<string, object?> contextData,
SchemaBuilder.LazySchema schema,
TypeInterceptor typeInterceptor)
{

_options = options;
Schema = schema;
Options = options;
_conventions = conventions;
_schemaServices = schemaServices;
_serviceHelper = new ServiceHelper(_schemaServices);
Expand Down Expand Up @@ -75,7 +75,7 @@ void OnSchemaOnCompleted(object? sender, EventArgs args)
public IServiceProvider Services => _schemaServices;

/// <inheritdoc />
public IReadOnlySchemaOptions Options { get; }
public IReadOnlySchemaOptions Options => _options();

/// <inheritdoc />
public INamingConventions Naming
Expand Down Expand Up @@ -284,15 +284,28 @@ internal static DescriptorContext Create(
IDictionary<string, object?>? 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<CreateConvention>>(),
services ?? new EmptyServiceProvider(),
contextData ?? new Dictionary<string, object?>(),
schema ?? new SchemaBuilder.LazySchema(),
typeInterceptor ?? new AggregateTypeInterceptor());

internal static DescriptorContext Create(
Func<IReadOnlySchemaOptions> options,
IServiceProvider? services = null,
IReadOnlyDictionary<(Type, string?), List<CreateConvention>>? conventions = null,
IDictionary<string, object?>? contextData = null,
SchemaBuilder.LazySchema? schema = null,
TypeInterceptor? typeInterceptor = null)
=> new DescriptorContext(
options,
conventions ?? new Dictionary<(Type, string?), List<CreateConvention>>(),
services ?? new EmptyServiceProvider(),
contextData ?? new Dictionary<string, object?>(),
schema ?? new SchemaBuilder.LazySchema(),
typeInterceptor ?? new AggregateTypeInterceptor());
}

private sealed class NoOpStringBuilderPool : ObjectPool<StringBuilder>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Query>()
.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";
}
}

0 comments on commit 916b771

Please sign in to comment.