Skip to content

Commit 17d7873

Browse files
Less generics
1 parent 204af98 commit 17d7873

26 files changed

+531
-628
lines changed

src/HotChocolate/Core/src/Execution/DependencyInjection/RequestExecutorBuilderExtensions.Services.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public static IRequestExecutorBuilder AddScopedServiceInitializer<TService>(
3636

3737
/// <summary>
3838
/// Resolves an instance of <typeparamref name="TService"/> from the application
39-
/// service provider and makes it available as a singleton through the schema
39+
/// service provider and makes it available as a Singleton through the schema
4040
/// service provider.
4141
/// </summary>
4242
/// <typeparam name="TService">

src/HotChocolate/Data/src/Data/DataResources.Designer.cs

Lines changed: 204 additions & 509 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/HotChocolate/Data/src/Data/DataResources.resx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,13 +170,13 @@
170170
<value>The provided value for filter `{0}` of type {1} is invalid. Null values are not supported.</value>
171171
</data>
172172
<data name="FilterProvider_UnableToCreateFieldHandler" xml:space="preserve">
173-
<value>Unable to create field handler `{0}` for filter provider `{1}`.</value>
173+
<value>Unable to create field handler for filter provider `{0}`.</value>
174174
</data>
175175
<data name="SortProvider_UnableToCreateFieldHandler" xml:space="preserve">
176-
<value>Unable to create field handler `{0}` for sort provider `{1}`.</value>
176+
<value>Unable to create field handler for sort provider `{0}`.</value>
177177
</data>
178178
<data name="SortProvider_UnableToCreateOperationHandler" xml:space="preserve">
179-
<value>Unable to create operation handler `{0}` for sort provider `{1}`.</value>
179+
<value>Unable to create operation handler for sort provider `{0}`.</value>
180180
</data>
181181
<data name="SortEnumValue_ValueIsNull" xml:space="preserve">
182182
<value>The inner value of enum value cannot be null or empty</value>
@@ -197,7 +197,7 @@
197197
<value>No configuration was specified.</value>
198198
</data>
199199
<data name="ProjectionProvider_UnableToCreateFieldHandler" xml:space="preserve">
200-
<value>Unable to create field handler `{0}` for projection convention `{1}`.</value>
200+
<value>Unable to create field handler for projection provider `{0}`.</value>
201201
</data>
202202
<data name="ProjectionProvider_NoHandlersConfigured" xml:space="preserve">
203203
<value>The projection convention `{0}` does not specify and field handler.</value>

src/HotChocolate/Data/src/Data/ErrorHelper.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,13 @@ public static IError CreateNonNullError<T>(
6666

6767
public static ISchemaError ProjectionConvention_UnableToCreateFieldHandler(
6868
IProjectionProvider convention,
69-
Type fieldHandler) =>
69+
Exception exception) =>
7070
SchemaErrorBuilder.New()
7171
.SetMessage(
72-
DataResources.FilterProvider_UnableToCreateFieldHandler,
73-
fieldHandler.FullName ?? fieldHandler.Name,
72+
DataResources.ProjectionProvider_UnableToCreateFieldHandler,
7473
convention.GetType().FullName ?? convention.GetType().Name)
7574
.SetExtension(nameof(convention), convention)
76-
.SetExtension(nameof(fieldHandler), fieldHandler)
75+
.SetException(exception)
7776
.Build();
7877

7978
public static IError ProjectionProvider_CouldNotProjectFiltering(IValueNode node) =>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace HotChocolate.Data.Filters;
2+
3+
public readonly struct FilterFieldHandlerConfiguration
4+
{
5+
private readonly Func<FilterProviderContext, IFilterFieldHandler>? _factory;
6+
private readonly IFilterFieldHandler? _instance;
7+
8+
public FilterFieldHandlerConfiguration(IFilterFieldHandler instance)
9+
{
10+
_instance = instance;
11+
}
12+
13+
public FilterFieldHandlerConfiguration(Func<FilterProviderContext, IFilterFieldHandler> factory)
14+
{
15+
_factory = factory;
16+
}
17+
18+
public IFilterFieldHandler<TContext> Create<TContext>(FilterProviderContext context)
19+
where TContext : IFilterVisitorContext
20+
{
21+
if (_instance is not null)
22+
{
23+
if (_instance is not IFilterFieldHandler<TContext> handler)
24+
{
25+
throw new InvalidOperationException(
26+
$"Expected handler to be of type IFilterFieldHandler<{typeof(TContext).Name}>");
27+
}
28+
29+
return handler;
30+
}
31+
32+
if (_factory is null)
33+
{
34+
throw new InvalidOperationException("Expected to have either a factory or an instance.");
35+
}
36+
37+
var instance = _factory(context);
38+
39+
if (instance is not IFilterFieldHandler<TContext> casted)
40+
{
41+
throw new InvalidOperationException(
42+
$"Expected handler to be of type IFilterFieldHandler<{typeof(TContext).Name}>");
43+
}
44+
45+
return casted;
46+
}
47+
}

src/HotChocolate/Data/src/Data/Filters/Visitor/FilterProvider.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace HotChocolate.Data.Filters;
1212
/// </summary>
1313
/// <typeparam name="TContext">The type of the context</typeparam>
1414
public abstract class FilterProvider<TContext>
15-
: Convention<FilterProviderConfiguration<TContext>>
15+
: Convention<FilterProviderConfiguration>
1616
, IFilterProvider
1717
, IFilterProviderConvention
1818
where TContext : IFilterVisitorContext
@@ -31,13 +31,13 @@ protected FilterProvider()
3131
protected FilterProvider(Action<IFilterProviderDescriptor<TContext>> configure)
3232
=> _configure = configure ?? throw new ArgumentNullException(nameof(configure));
3333

34-
internal new FilterProviderConfiguration<TContext>? Configuration => base.Configuration;
34+
internal new FilterProviderConfiguration? Configuration => base.Configuration;
3535

3636
/// <inheritdoc />
3737
public IReadOnlyCollection<IFilterFieldHandler> FieldHandlers => _fieldHandlers;
3838

3939
/// <inheritdoc />
40-
protected override FilterProviderConfiguration<TContext> CreateConfiguration(IConventionContext context)
40+
protected override FilterProviderConfiguration CreateConfiguration(IConventionContext context)
4141
{
4242
if (_configure is null)
4343
{
@@ -68,7 +68,7 @@ void IFilterProviderConvention.Complete(IConventionContext context)
6868
/// <inheritdoc />
6969
protected internal override void Complete(IConventionContext context)
7070
{
71-
if (Configuration!.HandlerFactories.Count == 0)
71+
if (Configuration!.FieldHandlerConfigurations.Count == 0)
7272
{
7373
throw FilterProvider_NoHandlersConfigured(this);
7474
}
@@ -92,19 +92,17 @@ protected internal override void Complete(IConventionContext context)
9292
context.DescriptorContext.InputFormatter
9393
);
9494

95-
foreach (var factory in Configuration.HandlerFactories)
95+
foreach (var handlerConfiguration in Configuration.FieldHandlerConfigurations)
9696
{
9797
try
9898
{
99-
var handler = factory(providerContext);
99+
var handler = handlerConfiguration.Create<TContext>(providerContext);
100100

101101
_fieldHandlers.Add(handler);
102102
}
103-
catch
103+
catch (Exception exception)
104104
{
105-
// TODO: Proper exception
106-
throw new InvalidOperationException();
107-
// throw FilterProvider_UnableToCreateFieldHandler(this, type);
105+
throw FilterProvider_UnableToCreateFieldHandler(this, exception);
108106
}
109107
}
110108
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
namespace HotChocolate.Data.Filters;
22

3-
public class FilterProviderConfiguration<TContext> where TContext : IFilterVisitorContext
3+
public class FilterProviderConfiguration
44
{
5-
public IList<Func<FilterProviderContext, IFilterFieldHandler<TContext>>> HandlerFactories { get; } = [];
5+
public IList<FilterFieldHandlerConfiguration> FieldHandlerConfigurations { get; } = [];
66
}

src/HotChocolate/Data/src/Data/Filters/Visitor/FilterProviderDescriptor.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,23 @@ protected FilterProviderDescriptor()
88
{
99
}
1010

11-
protected FilterProviderConfiguration<TContext> Configuration { get; } = new();
11+
protected FilterProviderConfiguration Configuration { get; } = new();
1212

13-
public FilterProviderConfiguration<TContext> CreateConfiguration() => Configuration;
13+
public FilterProviderConfiguration CreateConfiguration() => Configuration;
1414

1515
public IFilterProviderDescriptor<TContext> AddFieldHandler<TFieldHandler>(
1616
Func<FilterProviderContext, TFieldHandler> factory)
1717
where TFieldHandler : IFilterFieldHandler<TContext>
1818
{
19-
// TODO: Find a better way
20-
Configuration.HandlerFactories.Add(ctx => factory(ctx));
19+
Configuration.FieldHandlerConfigurations.Add(new FilterFieldHandlerConfiguration(ctx => factory(ctx)));
2120
return this;
2221
}
2322

2423
public IFilterProviderDescriptor<TContext> AddFieldHandler<TFieldHandler>(
2524
TFieldHandler fieldHandler)
2625
where TFieldHandler : IFilterFieldHandler<TContext>
2726
{
28-
// TODO: Find a better way
29-
Configuration.HandlerFactories.Add(_ => fieldHandler);
27+
Configuration.FieldHandlerConfigurations.Add(new FilterFieldHandlerConfiguration(fieldHandler));
3028
return this;
3129
}
3230

src/HotChocolate/Data/src/Data/Filters/Visitor/FilterProviderExtensions.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
namespace HotChocolate.Data.Filters;
55

66
public abstract class FilterProviderExtensions<TContext>
7-
: ConventionExtension<FilterProviderConfiguration<TContext>>,
7+
: ConventionExtension<FilterProviderConfiguration>,
88
IFilterProviderExtension,
99
IFilterProviderConvention
1010
where TContext : IFilterVisitorContext
@@ -34,7 +34,7 @@ void IFilterProviderConvention.Complete(IConventionContext context)
3434
Complete(context);
3535
}
3636

37-
protected override FilterProviderConfiguration<TContext> CreateConfiguration(IConventionContext context)
37+
protected override FilterProviderConfiguration CreateConfiguration(IConventionContext context)
3838
{
3939
if (_configure is null)
4040
{
@@ -60,9 +60,9 @@ public override void Merge(IConventionContext context, Convention convention)
6060
// Provider extensions should be applied by default before the default handlers, as
6161
// the interceptor picks up the first handler. A provider extension should adds more
6262
// specific handlers than the default providers
63-
for (var i = Configuration.HandlerFactories.Count - 1; i >= 0; i--)
63+
for (var i = Configuration.FieldHandlerConfigurations.Count - 1; i >= 0; i--)
6464
{
65-
target.HandlerFactories.Insert(0, Configuration.HandlerFactories[i]);
65+
target.FieldHandlerConfigurations.Insert(0, Configuration.FieldHandlerConfigurations[i]);
6666
}
6767
}
6868
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace HotChocolate.Data.Projections;
2+
3+
public readonly struct ProjectionFieldHandlerConfiguration
4+
{
5+
private readonly Func<ProjectionProviderContext, IProjectionFieldHandler>? _factory;
6+
private readonly IProjectionFieldHandler? _instance;
7+
8+
public ProjectionFieldHandlerConfiguration(IProjectionFieldHandler instance)
9+
{
10+
_instance = instance;
11+
}
12+
13+
public ProjectionFieldHandlerConfiguration(Func<ProjectionProviderContext, IProjectionFieldHandler> factory)
14+
{
15+
_factory = factory;
16+
}
17+
18+
public IProjectionFieldHandler Create(ProjectionProviderContext context)
19+
{
20+
if (_instance is not null)
21+
{
22+
return _instance;
23+
}
24+
25+
if (_factory is null)
26+
{
27+
throw new InvalidOperationException("Expected to have either a factory or an instance.");
28+
}
29+
30+
return _factory(context);
31+
}
32+
}

0 commit comments

Comments
 (0)