-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
ObjectType.cs
167 lines (136 loc) · 5.39 KB
/
ObjectType.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
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Configuration;
using HotChocolate.Language;
using HotChocolate.Resolvers;
using HotChocolate.Types.Descriptors;
using HotChocolate.Types.Descriptors.Definitions;
using static HotChocolate.Utilities.ErrorHelper;
#nullable enable
namespace HotChocolate.Types
{
public class ObjectType
: NamedTypeBase<ObjectTypeDefinition>
, IObjectType
{
private readonly List<InterfaceType> _interfaces = new List<InterfaceType>();
private Action<IObjectTypeDescriptor>? _configure;
private IsOfType? _isOfType;
protected ObjectType()
{
_configure = Configure;
Fields = FieldCollection<ObjectField>.Empty;
}
public ObjectType(Action<IObjectTypeDescriptor> configure)
{
_configure = configure;
Fields = FieldCollection<ObjectField>.Empty;
}
public override TypeKind Kind => TypeKind.Object;
public ObjectTypeDefinitionNode? SyntaxNode { get; private set; }
ISyntaxNode? IHasSyntaxNode.SyntaxNode => SyntaxNode;
public IReadOnlyList<InterfaceType> Interfaces => _interfaces;
IReadOnlyList<IInterfaceType> IComplexOutputType.Interfaces => Interfaces;
public FieldCollection<ObjectField> Fields { get; private set; }
IFieldCollection<IObjectField> IObjectType.Fields => Fields;
IFieldCollection<IOutputField> IComplexOutputType.Fields => Fields;
public bool IsOfType(IResolverContext context, object resolverResult) =>
_isOfType!.Invoke(context, resolverResult);
public bool IsImplementing(NameString interfaceTypeName) =>
_interfaces.Any(t => t.Name.Equals(interfaceTypeName));
public bool IsImplementing(InterfaceType interfaceType) =>
_interfaces.IndexOf(interfaceType) != -1;
public bool IsImplementing(IInterfaceType interfaceType) =>
interfaceType is InterfaceType i && _interfaces.IndexOf(i) != -1;
protected override ObjectTypeDefinition CreateDefinition(
ITypeDiscoveryContext context)
{
var descriptor = ObjectTypeDescriptor.FromSchemaType(
context.DescriptorContext,
GetType());
_configure!.Invoke(descriptor);
_configure = null;
return descriptor.CreateDefinition();
}
protected virtual void Configure(IObjectTypeDescriptor descriptor) { }
protected override void OnRegisterDependencies(
ITypeDiscoveryContext context,
ObjectTypeDefinition definition)
{
base.OnRegisterDependencies(context, definition);
context.RegisterDependencies(definition);
SetTypeIdentity(typeof(ObjectType<>));
}
protected override void OnCompleteType(
ITypeCompletionContext context,
ObjectTypeDefinition definition)
{
base.OnCompleteType(context, definition);
if (ValidateFields(context, definition))
{
_isOfType = definition.IsOfType;
SyntaxNode = definition.SyntaxNode;
var sortByName = context.DescriptorContext.Options.SortFieldsByName;
var fields = definition.Fields.Select(t => new ObjectField(t, sortByName)).ToList();
Fields = new FieldCollection<ObjectField>(fields, sortByName);
CompleteInterfacesHelper.Complete(
context, definition, RuntimeType, _interfaces, this, SyntaxNode);
CompleteIsOfType(context);
FieldInitHelper.CompleteFields(context, definition, Fields);
}
}
private void CompleteIsOfType(ITypeCompletionContext context)
{
if (_isOfType is null)
{
if (context.IsOfType != null)
{
IsOfTypeFallback isOfType = context.IsOfType;
_isOfType = (ctx, obj) => isOfType(this, ctx, obj);
}
else if (RuntimeType == typeof(object))
{
_isOfType = IsOfTypeWithName;
}
else
{
_isOfType = IsOfTypeWithClrType;
}
}
}
private bool ValidateFields(
ITypeCompletionContext context,
ObjectTypeDefinition definition)
{
ObjectFieldDefinition[] invalidFields =
definition.Fields.Where(t => t.Type is null).ToArray();
foreach (ObjectFieldDefinition field in invalidFields)
{
context.ReportError(ObjectType_UnableToInferOrResolveType(Name, this, field));
}
return invalidFields.Length == 0;
}
private bool IsOfTypeWithClrType(
IResolverContext context,
object? result)
{
if (result is null)
{
return true;
}
return RuntimeType.IsInstanceOfType(result);
}
private bool IsOfTypeWithName(
IResolverContext context,
object? result)
{
if (result is null)
{
return true;
}
Type type = result.GetType();
return Name.Equals(type.Name);
}
}
}