-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
FilterProvider.cs
108 lines (88 loc) · 3.6 KB
/
FilterProvider.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.Collections.Generic;
using HotChocolate.Resolvers;
using HotChocolate.Types;
using HotChocolate.Types.Descriptors;
using HotChocolate.Utilities;
using static HotChocolate.Data.DataResources;
using static HotChocolate.Data.ThrowHelper;
using static HotChocolate.Data.ErrorHelper;
namespace HotChocolate.Data.Filters
{
public abstract class FilterProvider<TContext>
: Convention<FilterProviderDefinition>,
IFilterProvider,
IFilterProviderConvention
where TContext : IFilterVisitorContext
{
private readonly List<IFilterFieldHandler<TContext>> _fieldHandlers =
new List<IFilterFieldHandler<TContext>>();
private Action<IFilterProviderDescriptor<TContext>>? _configure;
protected FilterProvider()
{
_configure = Configure;
}
public FilterProvider(Action<IFilterProviderDescriptor<TContext>> configure)
{
_configure = configure ??
throw new ArgumentNullException(nameof(configure));
}
internal new FilterProviderDefinition? Definition => base.Definition;
public IReadOnlyCollection<IFilterFieldHandler> FieldHandlers => _fieldHandlers;
protected override FilterProviderDefinition CreateDefinition(IConventionContext context)
{
if (_configure is null)
{
throw new InvalidOperationException(FilterProvider_NoConfigurationSpecified);
}
var descriptor = FilterProviderDescriptor<TContext>.New();
_configure(descriptor);
_configure = null;
return descriptor.CreateDefinition();
}
void IFilterProviderConvention.Initialize(IConventionContext context)
{
base.Initialize(context);
}
void IFilterProviderConvention.OnComplete(IConventionContext context)
{
OnComplete(context);
}
protected override void OnComplete(IConventionContext context)
{
if (Definition.Handlers.Count == 0)
{
throw FilterProvider_NoHandlersConfigured(this);
}
IServiceProvider services = new DictionaryServiceProvider(
(typeof(IFilterProvider), this),
(typeof(IConventionContext), context),
(typeof(IDescriptorContext), context.DescriptorContext),
(typeof(ITypeInspector), context.DescriptorContext.TypeInspector))
.Include(context.Services);
foreach ((Type Type, IFilterFieldHandler? Instance) handler in Definition.Handlers)
{
switch (handler.Instance)
{
case null when services.TryGetOrCreateService(
handler.Type,
out IFilterFieldHandler<TContext>? service):
_fieldHandlers.Add(service);
break;
case null:
throw FilterProvider_UnableToCreateFieldHandler(this, handler.Type);
case IFilterFieldHandler<TContext> casted:
_fieldHandlers.Add(casted);
break;
}
}
}
protected virtual void Configure(IFilterProviderDescriptor<TContext> descriptor) { }
public abstract FieldMiddleware CreateExecutor<TEntityType>(NameString argumentName);
public virtual void ConfigureField(
NameString argumentName,
IObjectFieldDescriptor descriptor)
{
}
}
}