-
-
Notifications
You must be signed in to change notification settings - Fork 724
/
ObjectTypeDescriptor.cs
459 lines (379 loc) · 14.1 KB
/
ObjectTypeDescriptor.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Reflection;
using HotChocolate.Language;
using HotChocolate.Types.Descriptors.Definitions;
using HotChocolate.Types.Helpers;
using HotChocolate.Utilities;
using static HotChocolate.Properties.TypeResources;
using static HotChocolate.Types.FieldBindingFlags;
#nullable enable
namespace HotChocolate.Types.Descriptors;
public class ObjectTypeDescriptor
: DescriptorBase<ObjectTypeDefinition>
, IObjectTypeDescriptor
{
private readonly List<ObjectFieldDescriptor> _fields = new();
protected ObjectTypeDescriptor(IDescriptorContext context, Type clrType)
: base(context)
{
if (clrType is null)
{
throw new ArgumentNullException(nameof(clrType));
}
Definition.RuntimeType = clrType;
Definition.Name = context.Naming.GetTypeName(clrType, TypeKind.Object);
Definition.Description = context.Naming.GetTypeDescription(clrType, TypeKind.Object);
}
protected ObjectTypeDescriptor(IDescriptorContext context)
: base(context)
{
Definition.RuntimeType = typeof(object);
}
protected ObjectTypeDescriptor(
IDescriptorContext context,
ObjectTypeDefinition definition)
: base(context)
{
Definition = definition ?? throw new ArgumentNullException(nameof(definition));
foreach (var field in definition.Fields)
{
_fields.Add(ObjectFieldDescriptor.From(Context, field));
}
}
protected internal override ObjectTypeDefinition Definition { get; protected set; } = new();
protected ICollection<ObjectFieldDescriptor> Fields => _fields;
protected override void OnCreateDefinition(
ObjectTypeDefinition definition)
{
if (!Definition.AttributesAreApplied && Definition.FieldBindingType is not null)
{
Context.TypeInspector.ApplyAttributes(
Context,
this,
Definition.FieldBindingType);
Definition.AttributesAreApplied = true;
}
foreach (var field in _fields)
{
if (field.Definition.Ignore)
{
// if this definition is used for a type extension we need a
// binding to a field which shall be ignored. In case this is a
// definition for the type it will be ignored by the type initialization.
Definition.FieldIgnores.Add(
new ObjectFieldBinding(field.Definition.Name, ObjectFieldBindingType.Field));
}
}
var fields = TypeMemHelper.RentObjectFieldDefinitionMap();
var handledMembers = TypeMemHelper.RentMemberSet();
foreach (var fieldDescriptor in _fields)
{
var fieldDefinition = fieldDescriptor.CreateDefinition();
if (!fieldDefinition.Ignore && !string.IsNullOrEmpty(fieldDefinition.Name))
{
fields[fieldDefinition.Name] = fieldDefinition;
}
if (fieldDefinition.Member is { } member)
{
handledMembers.Add(member);
}
}
OnCompleteFields(fields, handledMembers);
// if we find fields that match field name that are ignored we will
// remove them from the field map.
foreach (var ignore in Definition.FieldIgnores)
{
fields.Remove(ignore.Name);
}
Definition.Fields.Clear();
Definition.Fields.AddRange(fields.Values);
TypeMemHelper.Return(fields);
TypeMemHelper.Return(handledMembers);
base.OnCreateDefinition(definition);
}
internal void InferFieldsFromFieldBindingType()
{
var fields = TypeMemHelper.RentObjectFieldDefinitionMap();
var handledMembers = TypeMemHelper.RentMemberSet();
InferFieldsFromFieldBindingType(fields, handledMembers);
TypeMemHelper.Return(fields);
TypeMemHelper.Return(handledMembers);
}
protected void InferFieldsFromFieldBindingType(
IDictionary<string, ObjectFieldDefinition> fields,
ISet<MemberInfo> handledMembers)
{
var skip = false;
HashSet<string>? subscribeRes = null;
Dictionary<MemberInfo, string>? subscribeResLook = null;
if (Definition.Fields.IsImplicitBinding() &&
Definition.FieldBindingType is not null)
{
var inspector = Context.TypeInspector;
var naming = Context.Naming;
var type = Definition.FieldBindingType;
var isExtension = Definition.IsExtension;
var includeStatic = (Definition.FieldBindingFlags & Static) == Static;
var members = inspector.GetMembers(type, isExtension, includeStatic);
foreach (var member in members)
{
var name = naming.GetMemberName(member, MemberKind.ObjectField);
if (handledMembers.Add(member) &&
!fields.ContainsKey(name) &&
IncludeField(ref skip, ref subscribeRes, ref subscribeResLook, members, member))
{
var descriptor = ObjectFieldDescriptor.New(
Context,
member,
Definition.RuntimeType,
type);
if (subscribeResLook is not null &&
subscribeResLook.TryGetValue(member, out var with))
{
descriptor.Definition.SubscribeWith = with;
}
if (isExtension && inspector.IsMemberIgnored(member))
{
descriptor.Ignore();
}
_fields.Add(descriptor);
handledMembers.Add(member);
// the create definition call will trigger the OnCompleteField call
// on the field description and trigger the initialization of the
// fields arguments.
fields[name] = descriptor.CreateDefinition();
}
}
}
static bool IncludeField(
ref bool skip,
ref HashSet<string>? subscribeResolver,
ref Dictionary<MemberInfo, string>? subscribeResolverLookup,
ReadOnlySpan<MemberInfo> allMembers,
MemberInfo current)
{
// if there is now with declared we can include all members.
if (skip)
{
return true;
}
if (subscribeResolver is null)
{
foreach (var member in allMembers)
{
if (member.IsDefined(typeof(SubscribeAttribute)) &&
member.GetCustomAttribute<SubscribeAttribute>() is { With: not null } a)
{
subscribeResolver ??= new HashSet<string>();
subscribeResolverLookup ??= new Dictionary<MemberInfo, string>();
subscribeResolver.Add(a.With);
subscribeResolverLookup.Add(member, a.With);
}
}
skip = subscribeResolver is null;
}
return !subscribeResolver?.Contains(current.Name) ?? true;
}
}
protected virtual void OnCompleteFields(
IDictionary<string, ObjectFieldDefinition> fields,
ISet<MemberInfo> handledMembers) { }
public IObjectTypeDescriptor SyntaxNode(
ObjectTypeDefinitionNode? objectTypeDefinition)
{
Definition.SyntaxNode = objectTypeDefinition;
return this;
}
public IObjectTypeDescriptor Name(string value)
{
Definition.Name = value;
return this;
}
public IObjectTypeDescriptor Description(string? value)
{
Definition.Description = value;
return this;
}
[Obsolete("Use Implements.")]
public IObjectTypeDescriptor Interface<TInterface>()
where TInterface : InterfaceType
=> Implements<TInterface>();
[Obsolete("Use Implements.")]
public IObjectTypeDescriptor Interface<TInterface>(
TInterface type)
where TInterface : InterfaceType
=> Implements(type);
[Obsolete("Use Implements.")]
public IObjectTypeDescriptor Interface(
NamedTypeNode namedType)
=> Implements(namedType);
public IObjectTypeDescriptor Implements<T>()
where T : InterfaceType
{
if (typeof(T) == typeof(InterfaceType))
{
throw new ArgumentException(
ObjectTypeDescriptor_InterfaceBaseClass);
}
Definition.Interfaces.Add(
Context.TypeInspector.GetTypeRef(typeof(T)));
return this;
}
public IObjectTypeDescriptor Implements<T>(T type)
where T : InterfaceType
{
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
Definition.Interfaces.Add(
new SchemaTypeReference(
type));
return this;
}
public IObjectTypeDescriptor Implements(NamedTypeNode type)
{
if (type is null)
{
throw new ArgumentNullException(nameof(type));
}
Definition.Interfaces.Add(TypeReference.Create(type, TypeContext.Output));
return this;
}
public IObjectTypeDescriptor IsOfType(IsOfType? isOfType)
{
Definition.IsOfType = isOfType ?? throw new ArgumentNullException(nameof(isOfType));
return this;
}
public IObjectFieldDescriptor Field(string name)
{
var fieldDescriptor = _fields.Find(t => t.Definition.Name.EqualsOrdinal(name));
if (fieldDescriptor is not null)
{
return fieldDescriptor;
}
fieldDescriptor = ObjectFieldDescriptor.New(Context, name);
_fields.Add(fieldDescriptor);
return fieldDescriptor;
}
public IObjectFieldDescriptor Field<TResolver>(
Expression<Func<TResolver, object?>> propertyOrMethod) =>
Field<TResolver, object?>(propertyOrMethod);
public IObjectFieldDescriptor Field(
MemberInfo propertyOrMethod)
{
if (propertyOrMethod is null)
{
throw new ArgumentNullException(nameof(propertyOrMethod));
}
if (propertyOrMethod is PropertyInfo || propertyOrMethod is MethodInfo)
{
var fieldDescriptor = _fields.Find(t => t.Definition.Member == propertyOrMethod);
if (fieldDescriptor is not null)
{
return fieldDescriptor;
}
fieldDescriptor = ObjectFieldDescriptor.New(
Context,
propertyOrMethod,
Definition.RuntimeType,
propertyOrMethod.ReflectedType ?? Definition.RuntimeType);
_fields.Add(fieldDescriptor);
return fieldDescriptor;
}
throw new ArgumentException(
ObjectTypeDescriptor_MustBePropertyOrMethod,
nameof(propertyOrMethod));
}
public IObjectFieldDescriptor Field<TResolver, TPropertyType>(
Expression<Func<TResolver, TPropertyType>> propertyOrMethod)
{
if (propertyOrMethod is null)
{
throw new ArgumentNullException(nameof(propertyOrMethod));
}
var member = propertyOrMethod.TryExtractMember();
if (member is PropertyInfo or MethodInfo)
{
var fieldDescriptor = _fields.Find(t => t.Definition.Member == member);
if (fieldDescriptor is not null)
{
return fieldDescriptor;
}
fieldDescriptor = ObjectFieldDescriptor.New(
Context,
member,
Definition.RuntimeType,
typeof(TResolver));
_fields.Add(fieldDescriptor);
return fieldDescriptor;
}
if (member is null)
{
var fieldDescriptor = ObjectFieldDescriptor.New(
Context,
propertyOrMethod,
Definition.RuntimeType,
typeof(TResolver));
_fields.Add(fieldDescriptor);
return fieldDescriptor;
}
throw new ArgumentException(
ObjectTypeDescriptor_MustBePropertyOrMethod,
nameof(member));
}
public IObjectTypeDescriptor Directive<T>(T directiveInstance)
where T : class
{
Definition.AddDirective(directiveInstance, Context.TypeInspector);
return this;
}
public IObjectTypeDescriptor Directive<T>()
where T : class, new()
{
Definition.AddDirective(new T(), Context.TypeInspector);
return this;
}
public IObjectTypeDescriptor Directive(string name, params ArgumentNode[] arguments)
{
Definition.AddDirective(name, arguments);
return this;
}
public IObjectTypeDescriptor ExtendsType(Type extendsType)
{
Definition.ExtendsType = extendsType;
return this;
}
public IObjectTypeDescriptor ExtendsType<T>()
{
Definition.ExtendsType = typeof(T);
return this;
}
public static ObjectTypeDescriptor New(
IDescriptorContext context) =>
new(context);
public static ObjectTypeDescriptor New(
IDescriptorContext context,
Type clrType) =>
new(context, clrType);
public static ObjectTypeDescriptor<T> New<T>(
IDescriptorContext context) =>
new(context);
public static ObjectTypeExtensionDescriptor<T> NewExtension<T>(
IDescriptorContext context) =>
new(context);
public static ObjectTypeDescriptor FromSchemaType(
IDescriptorContext context,
Type schemaType) =>
new(context, schemaType) { Definition = { RuntimeType = typeof(object) } };
public static ObjectTypeDescriptor From(
IDescriptorContext context,
ObjectTypeDefinition definition) =>
new(context, definition);
public static ObjectTypeDescriptor<T> From<T>(
IDescriptorContext context,
ObjectTypeDefinition definition) =>
new(context, definition);
}