-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
DescriptorContext.cs
203 lines (172 loc) · 6.93 KB
/
DescriptorContext.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using HotChocolate.Configuration;
using HotChocolate.Utilities;
#nullable enable
namespace HotChocolate.Types.Descriptors
{
public sealed class DescriptorContext : IDescriptorContext
{
private readonly Dictionary<(Type, string?), IConvention> _conventions =
new Dictionary<(Type, string?), IConvention>();
private readonly IReadOnlyDictionary<(Type, string?), List<CreateConvention>>
_convFactories;
private readonly IServiceProvider _services;
private INamingConventions? _naming;
private ITypeInspector? _inspector;
public event EventHandler<SchemaCompletedEventArgs>? SchemaCompleted;
private DescriptorContext(
IReadOnlySchemaOptions options,
IReadOnlyDictionary<(Type, string?), List<CreateConvention>> convFactories,
IServiceProvider services,
IDictionary<string, object?> contextData,
SchemaBuilder.LazySchema schema,
ISchemaInterceptor schemaInterceptor,
ITypeInterceptor typeInterceptor)
{
Options = options;
_convFactories = convFactories;
_services = services;
ContextData = contextData;
SchemaInterceptor = schemaInterceptor;
TypeInterceptor = typeInterceptor;
schema.Completed += OnSchemaOnCompleted;
void OnSchemaOnCompleted(object sender, EventArgs args)
{
SchemaCompleted?.Invoke(this, new SchemaCompletedEventArgs(schema.Schema));
}
}
public IServiceProvider Services => _services;
public IReadOnlySchemaOptions Options { get; }
public INamingConventions Naming
{
get
{
if (_naming is null)
{
_naming = GetConventionOrDefault<INamingConventions>(
() => new DefaultNamingConventions(Options.UseXmlDocumentation));
}
return _naming;
}
}
public ITypeInspector TypeInspector
{
get
{
if (_inspector is null)
{
_inspector = this.GetConventionOrDefault<ITypeInspector>(
new DefaultTypeInspector());
}
return _inspector;
}
}
public ISchemaInterceptor SchemaInterceptor { get; }
public ITypeInterceptor TypeInterceptor { get; }
public IDictionary<string, object?> ContextData { get; }
public T GetConventionOrDefault<T>(
Func<T> defaultConvention,
string? scope = null)
where T : class, IConvention
{
if (defaultConvention is null)
{
throw new ArgumentNullException(nameof(defaultConvention));
}
if (_conventions.TryGetValue((typeof(T), scope), out IConvention? conv) &&
conv is T castedConvention)
{
return castedConvention;
}
CreateConventions<T>(
scope,
out IConvention? createdConvention,
out IList<IConventionExtension>? extensions);
createdConvention ??= createdConvention as T;
createdConvention ??= _services.GetService(typeof(T)) as T;
createdConvention ??= defaultConvention();
if (createdConvention is Convention init)
{
ConventionContext conventionContext =
ConventionContext.Create(scope, _services, this);
init.Initialize(conventionContext);
MergeExtensions(conventionContext, init, extensions);
init.OnComplete(conventionContext);
}
if (createdConvention is T createdConventionOfT)
{
_conventions[(typeof(T), scope)] = createdConventionOfT;
return createdConventionOfT;
}
throw ThrowHelper.Convention_ConventionCouldNotBeCreated(typeof(T), scope);
}
private void CreateConventions<T>(
string? scope,
out IConvention? createdConvention,
out IList<IConventionExtension> extensions)
{
createdConvention = null;
extensions = new List<IConventionExtension>();
if (_convFactories.TryGetValue(
(typeof(T), scope),
out List<CreateConvention>? factories))
{
for (var i = 0; i < factories.Count; i++)
{
IConvention conv = factories[i](_services);
if (conv is IConventionExtension extension)
{
extensions.Add(extension);
}
else
{
if (createdConvention is not null)
{
throw ThrowHelper.Convention_TwoConventionsRegisteredForScope(
typeof(T),
createdConvention,
conv,
scope);
}
createdConvention = conv;
}
}
}
}
private static void MergeExtensions(
IConventionContext context,
Convention convention,
IList<IConventionExtension> extensions)
{
for (var m = 0; m < extensions.Count; m++)
{
if (extensions[m] is Convention extensionConvention)
{
extensionConvention.Initialize(context);
extensions[m].Merge(context, convention);
extensionConvention.OnComplete(context);
}
}
}
internal static DescriptorContext Create(
IReadOnlySchemaOptions? options = null,
IServiceProvider? services = null,
IReadOnlyDictionary<(Type, string?), List<CreateConvention>>? conventions = null,
IDictionary<string, object?>? contextData = null,
SchemaBuilder.LazySchema? schema = null,
ISchemaInterceptor? schemaInterceptor = null,
ITypeInterceptor? typeInterceptor = null)
{
return new DescriptorContext(
options ?? new SchemaOptions(),
conventions ?? new Dictionary<(Type, string?), List<CreateConvention>>(),
services ?? new EmptyServiceProvider(),
contextData ?? new Dictionary<string, object?>(),
schema ?? new SchemaBuilder.LazySchema(),
schemaInterceptor ?? new AggregateSchemaInterceptor(),
typeInterceptor ?? new AggregateTypeInterceptor());
}
}
}