-
-
Notifications
You must be signed in to change notification settings - Fork 725
/
SchemaSerializer.cs
404 lines (346 loc) · 12.5 KB
/
SchemaSerializer.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using HotChocolate.Language;
using HotChocolate.Types;
using HotChocolate.Utilities.Introspection;
namespace HotChocolate
{
public static class SchemaSerializer
{
public static string Serialize(ISchema schema)
{
if (schema is null)
{
throw new ArgumentNullException(nameof(schema));
}
var sb = new StringBuilder();
using var stringWriter = new StringWriter(sb);
using var documentWriter = new DocumentWriter(stringWriter);
DocumentNode document = SerializeSchema(schema);
var serializer = new SchemaSyntaxSerializer(true);
serializer.Visit(document, documentWriter);
return sb.ToString();
}
public static void Serialize(ISchema schema, TextWriter textWriter)
{
if (schema is null)
{
throw new ArgumentNullException(nameof(schema));
}
if (textWriter is null)
{
throw new ArgumentNullException(nameof(textWriter));
}
using var documentWriter = new DocumentWriter(textWriter);
DocumentNode document = SerializeSchema(schema);
var serializer = new SchemaSyntaxSerializer(true);
serializer.Visit(document, documentWriter);
}
public static DocumentNode SerializeSchema(
ISchema schema,
bool includeSpecScalars = false)
{
if (schema is null)
{
throw new ArgumentNullException(nameof(schema));
}
var typeDefinitions = GetNonScalarTypes(schema)
.Select(SerializeNonScalarTypeDefinition)
.OfType<IDefinitionNode>()
.ToList();
if (schema.QueryType != null
|| schema.MutationType != null
|| schema.SubscriptionType != null)
{
typeDefinitions.Insert(0, SerializeSchemaTypeDefinition(schema));
}
IEnumerable<DirectiveDefinitionNode> directiveTypeDefinitions =
schema.DirectiveTypes
.OrderBy(t => t.Name.ToString(), StringComparer.Ordinal)
.Select(SerializeDirectiveTypeDefinition);
typeDefinitions.AddRange(directiveTypeDefinitions);
IEnumerable<ScalarTypeDefinitionNode> scalarTypeDefinitions =
schema.Types
.OfType<ScalarType>()
.Where(t => includeSpecScalars || !BuiltInTypes.IsBuiltInType(t.Name))
.OrderBy(t => t.Name.ToString(), StringComparer.Ordinal)
.Select(SerializeScalarType);
typeDefinitions.AddRange(scalarTypeDefinitions);
return new DocumentNode(null, typeDefinitions);
}
private static IEnumerable<INamedType> GetNonScalarTypes(
ISchema schema)
{
return schema.Types
.Where(IsPublicAndNoScalar)
.OrderBy(t => t.Name.ToString(), StringComparer.Ordinal)
.GroupBy(t => (int)t.Kind)
.OrderBy(t => t.Key)
.SelectMany(t => t);
}
private static bool IsPublicAndNoScalar(INamedType type)
{
if (type.IsIntrospectionType() || type is ScalarType)
{
return false;
}
return true;
}
private static DirectiveDefinitionNode SerializeDirectiveTypeDefinition(
DirectiveType directiveType)
{
var arguments = directiveType.Arguments
.Select(SerializeInputField)
.ToList();
var locations = directiveType.Locations
.Select(l => new NameNode(l.MapDirectiveLocation().ToString()))
.ToList();
return new DirectiveDefinitionNode
(
null,
new NameNode(directiveType.Name),
SerializeDescription(directiveType.Description),
directiveType.IsRepeatable,
arguments,
locations
);
}
private static SchemaDefinitionNode SerializeSchemaTypeDefinition(
ISchema schema)
{
var operations = new List<OperationTypeDefinitionNode>();
if (schema.QueryType != null)
{
operations.Add(SerializeOperationType(
schema.QueryType,
OperationType.Query));
}
if (schema.MutationType != null)
{
operations.Add(SerializeOperationType(
schema.MutationType,
OperationType.Mutation));
}
if (schema.SubscriptionType != null)
{
operations.Add(SerializeOperationType(
schema.SubscriptionType,
OperationType.Subscription));
}
var directives = schema.Directives
.Select(SerializeDirective)
.ToList();
return new SchemaDefinitionNode
(
null,
SerializeDescription(schema.Description),
directives,
operations
);
}
private static OperationTypeDefinitionNode SerializeOperationType(
ObjectType type,
OperationType operation)
{
return new OperationTypeDefinitionNode
(
null,
operation,
SerializeNamedType(type)
);
}
private static ITypeDefinitionNode SerializeNonScalarTypeDefinition(
INamedType namedType) =>
namedType switch
{
ObjectType type => SerializeObjectType(type),
InterfaceType type => SerializeInterfaceType(type),
InputObjectType type => SerializeInputObjectType(type),
UnionType type => SerializeUnionType(type),
EnumType type => SerializeEnumType(type),
_ => throw new NotSupportedException()
};
private static ObjectTypeDefinitionNode SerializeObjectType(
ObjectType objectType)
{
var directives = objectType.Directives
.Select(SerializeDirective)
.ToList();
var interfaces = objectType.Interfaces
.Select(SerializeNamedType)
.ToList();
var fields = objectType.Fields
.Where(t => !t.IsIntrospectionField)
.Select(SerializeObjectField)
.ToList();
return new ObjectTypeDefinitionNode
(
null,
new NameNode(objectType.Name),
SerializeDescription(objectType.Description),
directives,
interfaces,
fields
);
}
private static InterfaceTypeDefinitionNode SerializeInterfaceType(
InterfaceType interfaceType)
{
var directives = interfaceType.Directives
.Select(SerializeDirective)
.ToList();
var fields = interfaceType.Fields
.Select(SerializeObjectField)
.ToList();
return new InterfaceTypeDefinitionNode
(
null,
new NameNode(interfaceType.Name),
SerializeDescription(interfaceType.Description),
directives,
Array.Empty<NamedTypeNode>(),
fields
);
}
private static InputObjectTypeDefinitionNode SerializeInputObjectType(
InputObjectType inputObjectType)
{
var directives = inputObjectType.Directives
.Select(SerializeDirective)
.ToList();
var fields = inputObjectType.Fields
.Select(SerializeInputField)
.ToList();
return new InputObjectTypeDefinitionNode
(
null,
new NameNode(inputObjectType.Name),
SerializeDescription(inputObjectType.Description),
directives,
fields
);
}
private static UnionTypeDefinitionNode SerializeUnionType(UnionType unionType)
{
var directives = unionType.Directives
.Select(SerializeDirective)
.ToList();
var types = unionType.Types.Values
.Select(SerializeNamedType)
.ToList();
return new UnionTypeDefinitionNode
(
null,
new NameNode(unionType.Name),
SerializeDescription(unionType.Description),
directives,
types
);
}
private static EnumTypeDefinitionNode SerializeEnumType(EnumType enumType)
{
var directives = enumType.Directives
.Select(SerializeDirective)
.ToList();
var values = enumType.Values
.Select(SerializeEnumValue)
.ToList();
return new EnumTypeDefinitionNode
(
null,
new NameNode(enumType.Name),
SerializeDescription(enumType.Description),
directives,
values
);
}
private static EnumValueDefinitionNode SerializeEnumValue(IEnumValue enumValue)
{
var directives = enumValue.Directives
.Select(SerializeDirective)
.ToList();
return new EnumValueDefinitionNode
(
null,
new NameNode(enumValue.Name),
SerializeDescription(enumValue.Description),
directives
);
}
private static ScalarTypeDefinitionNode SerializeScalarType(
ScalarType scalarType)
{
return new ScalarTypeDefinitionNode
(
null,
new NameNode(scalarType.Name),
SerializeDescription(scalarType.Description),
Array.Empty<DirectiveNode>()
);
}
private static FieldDefinitionNode SerializeObjectField(IOutputField field)
{
var arguments = field.Arguments
.Select(SerializeInputField)
.ToList();
var directives = field.Directives
.Select(SerializeDirective)
.ToList();
return new FieldDefinitionNode
(
null,
new NameNode(field.Name),
SerializeDescription(field.Description),
arguments,
SerializeType(field.Type),
directives
);
}
private static InputValueDefinitionNode SerializeInputField(
IInputField inputValue)
{
return new InputValueDefinitionNode
(
null,
new NameNode(inputValue.Name),
SerializeDescription(inputValue.Description),
SerializeType(inputValue.Type),
inputValue.DefaultValue,
inputValue.Directives.Select(SerializeDirective).ToList()
);
}
private static ITypeNode SerializeType(IType type)
{
if (type is NonNullType nt)
{
return new NonNullTypeNode(null, (INullableTypeNode)SerializeType(nt.Type));
}
if (type is ListType lt)
{
return new ListTypeNode(null, SerializeType(lt.ElementType));
}
if (type is INamedType namedType)
{
return SerializeNamedType(namedType);
}
throw new NotSupportedException();
}
private static NamedTypeNode SerializeNamedType(INamedType namedType)
{
return new NamedTypeNode(null, new NameNode(namedType.Name));
}
private static DirectiveNode SerializeDirective(IDirective directiveType)
{
return directiveType.ToNode(true);
}
private static StringValueNode SerializeDescription(string description)
{
return string.IsNullOrEmpty(description)
? null
: new StringValueNode(description);
}
}
}