Repository navigation
Expand file tree
/
Copy pathSchemaPrinter.java
More file actions
499 lines (442 loc) · 18.7 KB
/
Copy pathSchemaPrinter.java
File metadata and controls
499 lines (442 loc) · 18.7 KB
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
package graphql.schema.idl;
import graphql.Assert;
import graphql.language.Document;
import graphql.schema.GraphQLArgument;
import graphql.schema.GraphQLEnumType;
import graphql.schema.GraphQLEnumValueDefinition;
import graphql.schema.GraphQLFieldDefinition;
import graphql.schema.GraphQLInputObjectField;
import graphql.schema.GraphQLInputObjectType;
import graphql.schema.GraphQLInterfaceType;
import graphql.schema.GraphQLList;
import graphql.schema.GraphQLNonNull;
import graphql.schema.GraphQLObjectType;
import graphql.schema.GraphQLOutputType;
import graphql.schema.GraphQLScalarType;
import graphql.schema.GraphQLSchema;
import graphql.schema.GraphQLType;
import graphql.schema.GraphQLUnionType;
import graphql.schema.visibility.GraphqlFieldVisibility;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Stack;
import java.util.stream.Stream;
import static graphql.schema.visibility.DefaultGraphqlFieldVisibility.DEFAULT_FIELD_VISIBILITY;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
/**
* This can print an in memory GraphQL schema back to a logical schema definition
*/
public class SchemaPrinter {
/**
* Options to use when printing a schema
*/
public static class Options {
private final boolean includeIntrospectionTypes;
private final boolean includeScalars;
private final boolean includeExtendedScalars;
private Options(boolean includeIntrospectionTypes, boolean includeScalars, boolean includeExtendedScalars) {
this.includeIntrospectionTypes = includeIntrospectionTypes;
this.includeScalars = includeScalars;
this.includeExtendedScalars = includeExtendedScalars;
}
public boolean isIncludeIntrospectionTypes() {
return includeIntrospectionTypes;
}
public boolean isIncludeScalars() {
return includeScalars;
}
public boolean isIncludeExtendedScalars() {
return includeExtendedScalars;
}
public static Options defaultOptions() {
return new Options(false, false, false);
}
/**
* This will allow you to include introspection types that are contained in a schema
*
* @param flag whether to include them
*
* @return options
*/
public Options includeIntrospectionTypes(boolean flag) {
return new Options(flag, this.includeScalars, includeExtendedScalars);
}
/**
* This will allow you to include scalar types that are contained in a schema
*
* @param flag whether to include them
*
* @return options
*/
public Options includeScalarTypes(boolean flag) {
return new Options(this.includeIntrospectionTypes, flag, includeExtendedScalars);
}
/**
* This will allow you to include the graphql 'extended' scalar types that come with graphql-java such as
* GraphQLBigDecimal or GraphQLBigInteger
*
* @param flag whether to include them
*
* @return options
*/
public Options includeExtendedScalarTypes(boolean flag) {
return new Options(this.includeIntrospectionTypes, this.includeScalars, flag);
}
}
private final Map<Class, TypePrinter<?>> printers = new LinkedHashMap<>();
private final Options options;
public SchemaPrinter() {
this(Options.defaultOptions());
}
public SchemaPrinter(Options options) {
this.options = options;
printers.put(GraphQLSchema.class, schemaPrinter());
printers.put(GraphQLObjectType.class, objectPrinter());
printers.put(GraphQLEnumType.class, enumPrinter());
printers.put(GraphQLScalarType.class, scalarPrinter());
printers.put(GraphQLInterfaceType.class, interfacePrinter());
printers.put(GraphQLUnionType.class, unionPrinter());
printers.put(GraphQLInputObjectType.class, inputObjectPrinter());
}
/**
* This can print an in memory GraphQL IDL document back to a logical schema definition.
*
* If you want to turn a Introspection query result into a Document (and then into a printed
* schema) then use {@link graphql.introspection.IntrospectionResultToSchema#createSchemaDefinition(java.util.Map)}
* first to get the {@link graphql.language.Document} and then print that.
*
* @param schemaIDL the parsed schema IDL
*
* @return the logical schema definition
*/
public String print(Document schemaIDL) {
TypeDefinitionRegistry registry = new SchemaParser().buildRegistry(schemaIDL);
return print(UnExecutableSchemaGenerator.makeUnExecutableSchema(registry));
}
/**
* This can print an in memory GraphQL schema back to a logical schema definition
*
* @param schema the schema in play
*
* @return the logical schema definition
*/
public String print(GraphQLSchema schema) {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
GraphqlFieldVisibility visibility = schema.getFieldVisibility();
printer(schema.getClass()).print(out, schema, visibility);
List<GraphQLType> typesAsList = schema.getAllTypesAsList()
.stream()
.sorted(Comparator.comparing(GraphQLType::getName))
.collect(toList());
printType(out, typesAsList, GraphQLInterfaceType.class, visibility);
printType(out, typesAsList, GraphQLUnionType.class, visibility);
printType(out, typesAsList, GraphQLObjectType.class, visibility);
printType(out, typesAsList, GraphQLEnumType.class, visibility);
printType(out, typesAsList, GraphQLScalarType.class, visibility);
printType(out, typesAsList, GraphQLInputObjectType.class, visibility);
String result = sw.toString();
if (result.endsWith("\n\n")) {
result = result.substring(0, result.length() - 1);
}
return result;
}
private interface TypePrinter<T> {
void print(PrintWriter out, T type, GraphqlFieldVisibility visibility);
}
private boolean isIntrospectionType(GraphQLType type) {
return !options.isIncludeIntrospectionTypes() && type.getName().startsWith("__");
}
private TypePrinter<GraphQLScalarType> scalarPrinter() {
return (out, type, visibility) -> {
if (!options.isIncludeScalars()) {
return;
}
boolean printScalar;
if (ScalarInfo.isStandardScalar(type)) {
printScalar = false;
//noinspection RedundantIfStatement
if (options.isIncludeExtendedScalars() && !ScalarInfo.isGraphqlSpecifiedScalar(type)) {
printScalar = true;
}
} else {
printScalar = true;
}
if (printScalar) {
printComments(out, type, "");
out.format("scalar %s\n\n", type.getName());
}
};
}
private TypePrinter<GraphQLEnumType> enumPrinter() {
return (out, type, visibility) -> {
if (isIntrospectionType(type)) {
return;
}
printComments(out, type, "");
out.format("enum %s {\n", type.getName());
List<GraphQLEnumValueDefinition> values = type.getValues()
.stream()
.sorted(Comparator.comparing(GraphQLEnumValueDefinition::getName))
.collect(toList());
for (GraphQLEnumValueDefinition enumValueDefinition : values) {
printComments(out, enumValueDefinition, " ");
out.format(" %s\n", enumValueDefinition.getName());
}
out.format("}\n\n");
};
}
private TypePrinter<GraphQLInterfaceType> interfacePrinter() {
return (out, type, visibility) -> {
if (isIntrospectionType(type)) {
return;
}
printComments(out, type, "");
out.format("interface %s {\n", type.getName());
visibility.getFieldDefinitions(type)
.stream()
.sorted(Comparator.comparing(GraphQLFieldDefinition::getName))
.forEach(fd -> {
printComments(out, fd, " ");
out.format(" %s%s: %s\n",
fd.getName(), argsString(fd.getArguments()), typeString(fd.getType()));
});
out.format("}\n\n");
};
}
private TypePrinter<GraphQLUnionType> unionPrinter() {
return (out, type, visibility) -> {
if (isIntrospectionType(type)) {
return;
}
printComments(out, type, "");
out.format("union %s = ", type.getName());
List<GraphQLOutputType> types = type.getTypes()
.stream()
.sorted(Comparator.comparing(GraphQLOutputType::getName))
.collect(toList());
for (int i = 0; i < types.size(); i++) {
GraphQLOutputType objectType = types.get(i);
if (i > 0) {
out.format(" | ");
}
out.format("%s", objectType.getName());
}
out.format("\n\n");
};
}
private TypePrinter<GraphQLObjectType> objectPrinter() {
return (out, type, visibility) -> {
if (isIntrospectionType(type)) {
return;
}
printComments(out, type, "");
if (type.getInterfaces().isEmpty()) {
out.format("type %s {\n", type.getName());
} else {
Stream<String> interfaceNames = type.getInterfaces()
.stream()
.map(GraphQLType::getName)
.sorted(Comparator.naturalOrder());
out.format("type %s implements %s {\n",
type.getName(),
interfaceNames.collect(joining(", ")));
}
visibility.getFieldDefinitions(type)
.stream()
.sorted(Comparator.comparing(GraphQLFieldDefinition::getName))
.forEach(fd -> {
printComments(out, fd, " ");
out.format(" %s%s: %s\n",
fd.getName(), argsString(fd.getArguments()), typeString(fd.getType()));
});
out.format("}\n\n");
};
}
private TypePrinter<GraphQLInputObjectType> inputObjectPrinter() {
return (out, type, visibility) -> {
if (isIntrospectionType(type)) {
return;
}
printComments(out, type, "");
out.format("input %s {\n", type.getName());
type.getFieldDefinitions()
.stream()
.sorted(Comparator.comparing(GraphQLInputObjectField::getName))
.forEach(fd -> {
printComments(out, fd, " ");
out.format(" %s: %s\n",
fd.getName(), typeString(fd.getType()));
});
out.format("}\n\n");
};
}
private TypePrinter<GraphQLSchema> schemaPrinter() {
return (out, type, visibility) -> {
GraphQLObjectType queryType = type.getQueryType();
GraphQLObjectType mutationType = type.getMutationType();
GraphQLObjectType subscriptionType = type.getSubscriptionType();
// when serializing a GraphQL schema using the type system language, a
// schema definition should be omitted if only uses the default root type names.
boolean needsSchemaPrinted = false;
if (queryType != null && !queryType.getName().equals("Query")) {
needsSchemaPrinted = true;
}
if (mutationType != null && !mutationType.getName().equals("Mutation")) {
needsSchemaPrinted = true;
}
if (subscriptionType != null && !subscriptionType.getName().equals("Subscription")) {
needsSchemaPrinted = true;
}
if (needsSchemaPrinted) {
out.format("schema {\n");
if (queryType != null) {
out.format(" query: %s\n", queryType.getName());
}
if (mutationType != null) {
out.format(" mutation: %s\n", mutationType.getName());
}
if (subscriptionType != null) {
out.format(" subscription: %s\n", subscriptionType.getName());
}
out.format("}\n\n");
}
};
}
String typeString(GraphQLType rawType) {
StringBuilder sb = new StringBuilder();
Stack<String> stack = new Stack<>();
GraphQLType type = rawType;
while (true) {
if (type instanceof GraphQLNonNull) {
type = ((GraphQLNonNull) type).getWrappedType();
stack.push("!");
} else if (type instanceof GraphQLList) {
type = ((GraphQLList) type).getWrappedType();
sb.append("[");
stack.push("]");
} else {
sb.append(type.getName());
break;
}
}
while (!stack.isEmpty()) {
sb.append(stack.pop());
}
return sb.toString();
}
String argsString(List<GraphQLArgument> arguments) {
boolean hasDescriptions = arguments.stream().filter(arg -> !isNullOrEmpty(arg.getDescription())).count() > 0;
String prefix = hasDescriptions ? " " : "";
int count = 0;
StringBuilder sb = new StringBuilder();
arguments = arguments
.stream()
.sorted(Comparator.comparing(GraphQLArgument::getName))
.collect(toList());
for (GraphQLArgument argument : arguments) {
if (count == 0) {
sb.append("(");
} else {
sb.append(", ");
}
if (hasDescriptions) {
sb.append("\n");
}
String description = argument.getDescription();
if (!isNullOrEmpty(description)) {
Stream<String> stream = Arrays.stream(description.split("\n"));
stream.map(s -> " #" + s + "\n").forEach(sb::append);
}
sb.append(prefix).append(argument.getName()).append(": ").append(typeString(argument.getType()));
Object defaultValue = argument.getDefaultValue();
if (defaultValue != null) {
sb.append(" = ");
if (defaultValue instanceof Number) {
sb.append(defaultValue);
} else {
sb.append('"').append(defaultValue).append('"');
}
}
count++;
}
if (count > 0) {
if (hasDescriptions) {
sb.append("\n");
}
sb.append(prefix).append(")");
}
return sb.toString();
}
@SuppressWarnings("unchecked")
private <T> TypePrinter<T> printer(Class<?> clazz) {
TypePrinter typePrinter = printers.computeIfAbsent(clazz, k -> {
Class<?> superClazz = clazz.getSuperclass();
TypePrinter result;
if (superClazz != Object.class)
result = printer(superClazz);
else
result = (out, type, visibility) -> out.println("Type not implemented : " + type);
return result;
});
return (TypePrinter<T>) typePrinter;
}
public String print(GraphQLType type) {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
printType(out, type, DEFAULT_FIELD_VISIBILITY);
return sw.toString();
}
@SuppressWarnings("unchecked")
private void printType(PrintWriter out, List<GraphQLType> typesAsList, Class typeClazz, GraphqlFieldVisibility visibility) {
typesAsList.stream()
.filter(type -> typeClazz.isAssignableFrom(type.getClass()))
.forEach(type -> printType(out, type, visibility));
}
private void printType(PrintWriter out, GraphQLType type, GraphqlFieldVisibility visibility) {
TypePrinter<Object> printer = printer(type.getClass());
printer.print(out, type, visibility);
}
private void printComments(PrintWriter out, Object graphQLType, String prefix) {
String description = getDescription(graphQLType);
if (isNullOrEmpty(description)) {
return;
}
Stream<String> stream = Arrays.stream(description.split("\n"));
stream.map(s -> prefix + "#" + s + "\n").forEach(out::write);
}
private String getDescription(Object descriptionHolder) {
if (descriptionHolder instanceof GraphQLObjectType) {
return ((GraphQLObjectType) descriptionHolder).getDescription();
} else if (descriptionHolder instanceof GraphQLEnumType) {
return ((GraphQLEnumType) descriptionHolder).getDescription();
} else if (descriptionHolder instanceof GraphQLFieldDefinition) {
return ((GraphQLFieldDefinition) descriptionHolder).getDescription();
} else if (descriptionHolder instanceof GraphQLEnumValueDefinition) {
return ((GraphQLEnumValueDefinition) descriptionHolder).getDescription();
} else if (descriptionHolder instanceof GraphQLUnionType) {
return ((GraphQLUnionType) descriptionHolder).getDescription();
} else if (descriptionHolder instanceof GraphQLInputObjectType) {
return ((GraphQLInputObjectType) descriptionHolder).getDescription();
} else if (descriptionHolder instanceof GraphQLInputObjectField) {
return ((GraphQLInputObjectField) descriptionHolder).getDescription();
} else if (descriptionHolder instanceof GraphQLInterfaceType) {
return ((GraphQLInterfaceType) descriptionHolder).getDescription();
} else if (descriptionHolder instanceof GraphQLScalarType) {
return ((GraphQLScalarType) descriptionHolder).getDescription();
} else if (descriptionHolder instanceof GraphQLArgument) {
return ((GraphQLArgument) descriptionHolder).getDescription();
} else {
return Assert.assertShouldNeverHappen();
}
}
private static boolean isNullOrEmpty(String s) {
return s == null || s.isEmpty();
}
}