-
-
Notifications
You must be signed in to change notification settings - Fork 722
/
TypeTrimmer.cs
195 lines (162 loc) · 5.24 KB
/
TypeTrimmer.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
using System;
using System.Collections.Generic;
using System.Linq;
using HotChocolate.Types;
#nullable enable
namespace HotChocolate.Configuration
{
internal sealed class TypeTrimmer
{
private readonly HashSet<TypeSystemObjectBase> _touched = new();
private readonly List<ObjectType> _rootTypes = new();
private readonly List<TypeSystemObjectBase> _discoveredTypes;
public TypeTrimmer(IEnumerable<TypeSystemObjectBase> discoveredTypes)
{
if (discoveredTypes is null)
{
throw new ArgumentNullException(nameof(discoveredTypes));
}
_discoveredTypes = discoveredTypes.ToList();
}
public void AddOperationType(ObjectType? operationType)
{
if (operationType is not null)
{
_rootTypes.Add(operationType);
}
}
public IReadOnlyCollection<TypeSystemObjectBase> Trim()
{
foreach (var directiveType in _discoveredTypes.OfType<DirectiveType>())
{
if (directiveType.IsExecutableDirective)
{
_touched.Add(directiveType);
}
}
foreach (ObjectType rootType in _rootTypes)
{
VisitRoot(rootType);
}
return _touched;
}
private void VisitRoot(ObjectType rootType)
{
Visit(rootType);
}
private void Visit(TypeSystemObjectBase type)
{
if (_touched.Add(type))
{
switch (type)
{
case ScalarType s:
VisitScalar(s);
break;
case EnumType e:
VisitEnum(e);
break;
case ObjectType o:
VisitObject(o);
break;
case UnionType u:
VisitUnion(u);
break;
case InterfaceType i:
VisitInterface(i);
break;
case DirectiveType d:
VisitDirective(d);
break;
case InputObjectType i:
VisitInput(i);
break;
}
}
}
private void VisitScalar(ScalarType type)
{
VisitDirectives(type);
}
private void VisitEnum(EnumType type)
{
VisitDirectives(type);
foreach (IEnumValue value in type.Values)
{
VisitDirectives(value);
}
}
private void VisitObject(ObjectType type)
{
VisitDirectives(type);
foreach (InterfaceType interfaceType in type.Implements)
{
VisitInterface(interfaceType);
}
foreach (ObjectField field in type.Fields)
{
VisitDirectives(field);
Visit((TypeSystemObjectBase)field.Type.NamedType());
foreach (Argument argument in field.Arguments)
{
VisitDirectives(argument);
Visit((TypeSystemObjectBase)argument.Type.NamedType());
}
}
}
private void VisitUnion(UnionType type)
{
VisitDirectives(type);
foreach (ObjectType objectType in type.Types.Values)
{
Visit(objectType);
}
}
private void VisitInterface(InterfaceType type)
{
VisitDirectives(type);
foreach (InterfaceField field in type.Fields)
{
VisitDirectives(field);
Visit((TypeSystemObjectBase)field.Type.NamedType());
foreach (Argument argument in field.Arguments)
{
VisitDirectives(argument);
Visit((TypeSystemObjectBase)argument.Type.NamedType());
}
}
foreach (IComplexOutputType complexType in
_discoveredTypes.OfType<IComplexOutputType>())
{
if (complexType.IsImplementing(type))
{
Visit((TypeSystemObjectBase)complexType);
}
}
}
private void VisitInput(InputObjectType type)
{
VisitDirectives(type);
foreach (InputField field in type.Fields)
{
VisitDirectives(field);
Visit((TypeSystemObjectBase)field.Type.NamedType());
}
}
private void VisitDirective(DirectiveType type)
{
foreach (Argument argument in type.Arguments)
{
VisitDirectives(argument);
Visit((TypeSystemObjectBase)argument.Type.NamedType());
}
}
private void VisitDirectives(IHasDirectives hasDirectives)
{
foreach (DirectiveType type in hasDirectives.Directives.Select(t => t.Type))
{
Visit(type);
}
}
}
}