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
Expand Up @@ -154,7 +154,6 @@ public T GetConventionOrDefault<T>(
CreateConventions<T>(scope, out convention, out var extensions);

convention ??= convention as T;
convention ??= _serviceHelper.GetService<T>();
convention ??= factory();

if (convention is Convention init)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Reflection;
using HotChocolate.Execution.Configuration;
using HotChocolate.Language;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
Expand Down Expand Up @@ -244,6 +246,48 @@ public async Task WarmupTask_Should_Be_Able_To_Access_Schema_And_Regular_Service
cts.Dispose();
}

[Fact]
public async Task EvictExecutor_With_Custom_TypeInspector_Should_Rebuild_Without_Init_Exception()
{
// arrange
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(50));
var executorEvictedResetEvent = new SemaphoreSlim(0, 1);

var manager = new ServiceCollection()
.AddGraphQL()
.AddQueryType<Issue6695Query>()
.TryAddConvention<ITypeInspector, TuneDataTypeInspector>()
.Services
.BuildServiceProvider()
.GetRequiredService<RequestExecutorManager>();

manager.Subscribe(new RequestExecutorEventObserver(@event =>
{
if (@event.Type == RequestExecutorEventType.Evicted)
{
executorEvictedResetEvent.Release();
}
}));

// act
var initialExecutor = await manager.GetExecutorAsync(cancellationToken: cts.Token);
var initialResult = await initialExecutor.ExecuteAsync("{ ping }");

manager.EvictExecutor();

await executorEvictedResetEvent.WaitAsync(cts.Token);

var rebuiltExecutor = await manager.GetExecutorAsync(cancellationToken: cts.Token);
var rebuiltResult = await rebuiltExecutor.ExecuteAsync("{ ping }");

// assert
Assert.Empty(initialResult.ExpectOperationResult().Errors);
Assert.Empty(rebuiltResult.ExpectOperationResult().Errors);
Assert.NotNull(initialResult.ExpectOperationResult().Data);
Assert.NotNull(rebuiltResult.ExpectOperationResult().Data);
Assert.NotSame(initialExecutor, rebuiltExecutor);
}

#pragma warning disable CS9113 // Parameter is unread.
private sealed class CustomWarmupTask(IDocumentCache documentCache, SomeService service) : IRequestExecutorWarmupTask
#pragma warning restore CS9113 // Parameter is unread.
Expand All @@ -259,4 +303,18 @@ private sealed class TriggerableTypeModule : TypeModule
{
public void TriggerChange() => OnTypesChanged();
}

public sealed class Issue6695Query
{
public string Ping() => "pong";
}

private sealed class TuneDataTypeInspector : DefaultTypeInspector
{
public override bool IsMemberIgnored(MemberInfo member)
=> base.IsMemberIgnored(member) || member.IsDefined(typeof(ApiIgnoreAttribute));
}

[AttributeUsage(AttributeTargets.Property | AttributeTargets.Method)]
private sealed class ApiIgnoreAttribute : Attribute;
}
75 changes: 0 additions & 75 deletions src/HotChocolate/Core/test/Types.Tests/SchemaBuilderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1326,81 +1326,6 @@ public void AddConvention_ServiceDependency()
Assert.Equal(dependencyOfConvention, ((TestConventionServiceDependency)convention).Dependency);
}

[Fact]
public void AddConvention_Through_ServiceCollection()
{
// arrange
var services = new ServiceCollection();
services.AddTransient<ITestConvention, TestConvention>();
var provider = services.BuildServiceProvider();

// act
var schema = SchemaBuilder.New()
.AddServices(provider)
.AddType<ConventionTestType>()
.AddQueryType(d => d
.Name("Query")
.Field("foo")
.Resolve("bar"))
.Create();

// assert
var testType = schema.Types.GetType<ConventionTestType>("ConventionTestType");
var convention = testType.Context.GetConventionOrDefault<ITestConvention>(new TestConvention2());
Assert.IsType<TestConvention>(convention);
}

[Fact]
public void AddConvention_Through_ServiceCollection_ProvideImplementation()
{
// arrange
var services = new ServiceCollection();
var conventionImpl = new TestConvention();
services.AddSingleton<ITestConvention>(conventionImpl);
var provider = services.BuildServiceProvider();

// act
var schema = SchemaBuilder.New()
.AddServices(provider)
.AddType<ConventionTestType>()
.AddQueryType(d => d
.Name("Query")
.Field("foo")
.Resolve("bar"))
.Create();

// assert
var testType = schema.Types.GetType<ConventionTestType>("ConventionTestType");
var convention = testType.Context.GetConventionOrDefault<ITestConvention>(new TestConvention2());
Assert.IsType<TestConvention>(convention);
Assert.Equal(convention, conventionImpl);
}

[Fact]
public void AddConvention_Through_ServiceCollection_And_SchemaBuilderOverrides()
{
// arrange
var services = new ServiceCollection();
services.AddSingleton<IConvention, TestConvention>();
var provider = services.BuildServiceProvider();

// act
var schema = SchemaBuilder.New()
.AddServices(provider)
.AddConvention(typeof(IConvention), typeof(TestConvention2))
.AddType<ConventionTestType>()
.AddQueryType(d => d
.Name("Query")
.Field("foo")
.Resolve("bar"))
.Create();

// assert
var testType = schema.Types.GetType<ConventionTestType>("ConventionTestType");
var convention = testType.Context.GetConventionOrDefault<ITestConvention>(new TestConvention2());
Assert.IsType<TestConvention2>(convention);
}

[Fact]
public void AddConvention_NamingConvention()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.ComponentModel.DataAnnotations;
using HotChocolate.Types.Descriptors;
using Microsoft.Extensions.DependencyInjection;

namespace HotChocolate.Types;

Expand All @@ -21,14 +20,11 @@ public void Ignore_RequiredAttribute()
{
// arrange
var inspector = new DefaultTypeInspector(ignoreRequiredAttribute: true);
var services = new ServiceCollection()
.AddSingleton(typeof(ITypeInspector), inspector)
.BuildServiceProvider();

// act & assert
SchemaBuilder.New()
.AddQueryType<Foo>()
.AddServices(services)
.AddConvention<ITypeInspector>(inspector)
.Create()
.ToString()
.MatchSnapshot();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,6 @@ namespace HotChocolate.Types.Descriptors;

public class DescriptorContextTests
{
[Fact]
public void Create_With_Custom_NamingConventions()
{
// arrange
var options = new SchemaOptions();
var namingConventions = new DefaultNamingConventions(
new XmlDocumentationProvider(
new XmlDocumentationFileResolver(),
new NoOpStringBuilderPool()));
var services = new ServiceCollection()
.AddSingleton(typeof(INamingConventions), namingConventions)
.BuildServiceProvider();

// act
var context = DescriptorContext.Create(
options,
services,
new FeatureCollection(),
new SchemaBuilder.LazySchema(),
new AggregateTypeInterceptor());

// assert
Assert.Equal(namingConventions, context.Naming);
Assert.NotNull(context.TypeInspector);
Assert.Equal(options, context.Options);
}

[Fact]
public void Create_With_Custom_NamingConventions_AsIConvention()
{
Expand Down Expand Up @@ -65,30 +38,6 @@ public void Create_With_Custom_NamingConventions_AsIConvention()
Assert.Equal(options, context.Options);
}

[Fact]
public void Create_With_Custom_TypeInspector()
{
// arrange
var options = new SchemaOptions();
var inspector = new DefaultTypeInspector();
var services = new ServiceCollection()
.AddSingleton(typeof(ITypeInspector), inspector)
.BuildServiceProvider();

// act
var context = DescriptorContext.Create(
options,
services,
new FeatureCollection(),
new SchemaBuilder.LazySchema(),
new AggregateTypeInterceptor());

// assert
Assert.Equal(inspector, context.TypeInspector);
Assert.NotNull(context.Naming);
Assert.Equal(options, context.Options);
}

[Fact]
public void Create_Without_Services()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,10 @@ public void FilterInputType_Should_ThrowException_WhenNoConventionIsRegistered()
// act
// assert
var exception = Assert.Throws<SchemaException>(builder.Create);
exception.Message.MatchSnapshot();
Assert.Contains(
"No filter convention found for scope `Foo`. Register a convention with "
+ "`AddConvention<IFilterConvention, TYourConvention>(\"Foo\")` on the schema builder.",
exception.Message);
}

[Fact]
Expand All @@ -281,7 +284,9 @@ public void FilterInputType_Should_ThrowException_WhenNoConventionIsRegisteredDe
// act
// assert
var exception = Assert.Throws<SchemaException>(builder.Create);
exception.Message.MatchSnapshot();
Assert.Contains(
"No default filter convention found. Call `AddFiltering()` on the schema builder.",
exception.Message);
}

[Fact]
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,10 @@ public void SortInputType_Should_ThrowException_WhenNoConventionIsRegistered()
// act
// assert
var exception = Assert.Throws<SchemaException>(builder.Create);
exception.Message.MatchSnapshot();
Assert.Contains(
"No sorting convention found for scope `Foo`. Register a convention with "
+ "`AddConvention<ISortConvention, TYourConvention>(\"Foo\")` on the schema builder.",
exception.Message);
}

[Fact]
Expand All @@ -202,7 +205,9 @@ public void SortInputType_Should_ThrowException_WhenNoConventionIsRegisteredDefa
// act
// assert
var exception = Assert.Throws<SchemaException>(builder.Create);
exception.Message.MatchSnapshot();
Assert.Contains(
"No default sorting convention found. Call `AddSorting()` on the schema builder.",
exception.Message);
}

[Fact]
Expand Down

This file was deleted.

This file was deleted.

Loading