@graphql-markdown/printer-legacy 1.1.6 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/index.js CHANGED
@@ -1,617 +1,3 @@
1
- const {
2
- string: { toSlug, escapeMDX },
3
- object: { hasProperty, hasMethod },
4
- url: { pathUrl },
5
- graphql: {
6
- isEnumType,
7
- isUnionType,
8
- isObjectType,
9
- isScalarType,
10
- isOperation,
11
- getDefaultValue,
12
- getTypeName,
13
- getFields,
14
- isDirectiveType,
15
- isParametrizedField,
16
- isInterfaceType,
17
- getNamedType,
18
- isInputType,
19
- isListType,
20
- isNonNullType,
21
- isLeafType,
22
- getRelationOfReturn,
23
- getRelationOfField,
24
- getRelationOfImplementation,
25
- hasDirective,
26
- },
27
- } = require("@graphql-markdown/utils");
1
+ const { Printer } = require("./printer");
28
2
 
29
- const {
30
- ROOT_TYPE_LOCALE,
31
- HEADER_SECTION_LEVEL,
32
- HEADER_SECTION_SUB_LEVEL,
33
- HEADER_SECTION_ITEM_LEVEL,
34
- NO_DESCRIPTION_TEXT,
35
- MARKDOWN_EOL,
36
- MARKDOWN_EOP,
37
- } = require("./const/strings");
38
- const mdx = require("./const/mdx");
39
-
40
- module.exports = class Printer {
41
- constructor(
42
- schema,
43
- baseURL,
44
- linkRoot = "/",
45
- { groups, printTypeOptions, skipDocDirective } = {
46
- groups: undefined,
47
- printTypeOptions: undefined,
48
- skipDocDirective: undefined,
49
- },
50
- ) {
51
- this.schema = schema;
52
- this.baseURL = baseURL;
53
- this.linkRoot = linkRoot;
54
- this.groups = groups;
55
- this.parentTypePrefix = printTypeOptions?.parentTypePrefix ?? true;
56
- this.relatedTypeSection = printTypeOptions?.relatedTypeSection ?? true;
57
- this.typeBadges = printTypeOptions?.typeBadges ?? true;
58
- this.skipDocDirective = skipDocDirective ?? undefined;
59
- }
60
-
61
- getRootTypeLocaleFromString(text) {
62
- for (const [type, props] of Object.entries(ROOT_TYPE_LOCALE)) {
63
- if (Object.values(props).includes(text)) {
64
- return ROOT_TYPE_LOCALE[type];
65
- }
66
- }
67
- return undefined;
68
- }
69
-
70
- getLinkCategory(graphLQLNamedType) {
71
- switch (true) {
72
- case isEnumType(graphLQLNamedType):
73
- return ROOT_TYPE_LOCALE.ENUM;
74
- case isUnionType(graphLQLNamedType):
75
- return ROOT_TYPE_LOCALE.UNION;
76
- case isInterfaceType(graphLQLNamedType):
77
- return ROOT_TYPE_LOCALE.INTERFACE;
78
- case isObjectType(graphLQLNamedType):
79
- return ROOT_TYPE_LOCALE.TYPE;
80
- case isInputType(graphLQLNamedType):
81
- return ROOT_TYPE_LOCALE.INPUT;
82
- case isScalarType(graphLQLNamedType):
83
- return ROOT_TYPE_LOCALE.SCALAR;
84
- case isDirectiveType(graphLQLNamedType):
85
- return ROOT_TYPE_LOCALE.DIRECTIVE;
86
- case isOperation(graphLQLNamedType):
87
- return ROOT_TYPE_LOCALE.OPERATION;
88
- }
89
- return undefined;
90
- }
91
-
92
- getGroup(type) {
93
- if (typeof this.groups === "undefined") {
94
- return "";
95
- }
96
- const graphLQLNamedType = getNamedType(type);
97
- const typeName = graphLQLNamedType.name || graphLQLNamedType;
98
- return hasProperty(this.groups, typeName)
99
- ? toSlug(this.groups[typeName])
100
- : "";
101
- }
102
-
103
- toLink(type, name, operation) {
104
- const fallback = {
105
- text: name,
106
- url: "#",
107
- };
108
-
109
- if (hasDirective(type, this.skipDocDirective)) {
110
- return fallback;
111
- }
112
-
113
- const graphLQLNamedType = getNamedType(type);
114
-
115
- let category = this.getLinkCategory(graphLQLNamedType);
116
-
117
- if (
118
- typeof category === "undefined" ||
119
- typeof graphLQLNamedType === "undefined" ||
120
- graphLQLNamedType === null
121
- ) {
122
- return fallback;
123
- }
124
-
125
- // special case for support relation map
126
- if (category === ROOT_TYPE_LOCALE.OPERATION) {
127
- if (typeof operation === "undefined") {
128
- return fallback;
129
- }
130
- category = operation;
131
- }
132
-
133
- const text = graphLQLNamedType.name || graphLQLNamedType;
134
- const group = this.getGroup(type);
135
- const url = pathUrl.join(
136
- this.linkRoot,
137
- this.baseURL,
138
- group,
139
- category.plural,
140
- toSlug(text),
141
- );
142
-
143
- return {
144
- text: text,
145
- url: url,
146
- };
147
- }
148
-
149
- getRelationLink(category, type) {
150
- if (typeof category === "undefined") {
151
- return "";
152
- }
153
- const link = this.toLink(type, type.name, category);
154
- return `[\`${link.text}\`](${link.url}) <Badge class="secondary" text="${category.singular}"/>`;
155
- }
156
-
157
- printSection(
158
- values,
159
- section,
160
- { level, parentType } = {
161
- level: HEADER_SECTION_LEVEL,
162
- parentType: undefined,
163
- },
164
- ) {
165
- if (values.length === 0) {
166
- return "";
167
- }
168
-
169
- if (typeof level === "undefined") {
170
- level = HEADER_SECTION_LEVEL;
171
- }
172
-
173
- return `${level} ${section}${MARKDOWN_EOP}${this.printSectionItems(values, {
174
- parentType,
175
- })}${MARKDOWN_EOP}`;
176
- }
177
-
178
- printSectionItems(
179
- values,
180
- { level, parentType } = {
181
- level: HEADER_SECTION_SUB_LEVEL,
182
- parentType: undefined,
183
- },
184
- ) {
185
- if (!Array.isArray(values)) {
186
- return "";
187
- }
188
-
189
- if (typeof level === "undefined") {
190
- level = HEADER_SECTION_SUB_LEVEL;
191
- }
192
-
193
- return values
194
- .map((v) => v && this.printSectionItem(v, { level, parentType }))
195
- .join(MARKDOWN_EOP);
196
- }
197
-
198
- printLinkAttributes(type, text) {
199
- if (typeof type === "undefined") {
200
- return text ?? "";
201
- }
202
-
203
- if (!isLeafType(type, text) && typeof type.ofType != "undefined") {
204
- text = this.printLinkAttributes(type.ofType, text);
205
- }
206
-
207
- if (isListType(type)) {
208
- return `[${text}]`;
209
- }
210
-
211
- if (isNonNullType(type)) {
212
- return `${text}!`;
213
- }
214
-
215
- return text;
216
- }
217
-
218
- printLink(type, withAttributes = false, parentType = undefined) {
219
- const link = this.toLink(type, getTypeName(type));
220
-
221
- if (!withAttributes) {
222
- const printParentType =
223
- this.parentTypePrefix && typeof parentType !== "undefined";
224
- const text = printParentType
225
- ? `<code style={{ fontWeight: 'normal' }}>${parentType}.<b>${link.text}</b></code>`
226
- : `\`${link.text}\``;
227
- return `[${text}](${link.url})`;
228
- }
229
-
230
- const text = this.printLinkAttributes(type, link.text);
231
-
232
- return `[\`${text}\`](${link.url})`;
233
- }
234
-
235
- getTypeBadges(type) {
236
- const rootType = hasProperty(type, "type") ? type.type : type;
237
-
238
- const badges = [];
239
-
240
- if (type.isDeprecated) {
241
- badges.push("deprecated");
242
- }
243
-
244
- if (isNonNullType(rootType)) {
245
- badges.push("non-null");
246
- }
247
-
248
- if (isListType(rootType)) {
249
- badges.push("list");
250
- }
251
-
252
- const category = this.getLinkCategory(getNamedType(rootType));
253
- if (typeof category !== "undefined") {
254
- badges.push(category);
255
- }
256
-
257
- const group = this.getGroup(rootType);
258
- if (group !== "") {
259
- badges.push(group);
260
- }
261
-
262
- return badges;
263
- }
264
-
265
- printBadges(type) {
266
- if (!this.typeBadges) {
267
- return "";
268
- }
269
-
270
- const badges = this.getTypeBadges(type);
271
-
272
- if (badges.length === 0) {
273
- return "";
274
- }
275
-
276
- return badges
277
- .map(
278
- (badge) =>
279
- `<Badge class="secondary" text="${badge.singular ?? badge}"/>`,
280
- )
281
- .join(" ");
282
- }
283
-
284
- printParentLink(type) {
285
- return hasProperty(type, "type")
286
- ? `<Bullet />${this.printLink(type.type, true)}`
287
- : "";
288
- }
289
-
290
- printSectionItem(
291
- type,
292
- { level, parentType } = {
293
- level: HEADER_SECTION_SUB_LEVEL,
294
- parentType: undefined,
295
- },
296
- ) {
297
- if (
298
- typeof type === "undefined" ||
299
- type === null ||
300
- hasDirective(type, this.skipDocDirective)
301
- ) {
302
- return "";
303
- }
304
-
305
- if (typeof level === "undefined") {
306
- level = HEADER_SECTION_SUB_LEVEL;
307
- }
308
-
309
- const typeNameLink = this.printLink(type, false, parentType);
310
- const description = this.printDescription(type, "");
311
- const badges = this.printBadges(type);
312
- const parentTypeLink = this.printParentLink(type);
313
-
314
- let section = `${level} ${typeNameLink}${parentTypeLink} ${badges}${MARKDOWN_EOL}> ${description}${MARKDOWN_EOL}> `;
315
- if (isParametrizedField(type)) {
316
- section += this.printSectionItems(type.args, {
317
- level: HEADER_SECTION_ITEM_LEVEL,
318
- parentType:
319
- typeof parentType === "undefined"
320
- ? undefined
321
- : `${parentType}.${type.name}`,
322
- });
323
- }
324
-
325
- return section;
326
- }
327
-
328
- printCodeEnum(type) {
329
- if (!isEnumType(type)) {
330
- return "";
331
- }
332
-
333
- let code = `enum ${getTypeName(type)} {${MARKDOWN_EOL}`;
334
- code += type
335
- .getValues()
336
- .map((v) => ` ${getTypeName(v)}`)
337
- .join(MARKDOWN_EOL);
338
- code += `${MARKDOWN_EOL}}`;
339
-
340
- return code;
341
- }
342
-
343
- printCodeUnion(type) {
344
- if (!isUnionType(type)) {
345
- return "";
346
- }
347
-
348
- let code = `union ${getTypeName(type)} = `;
349
- code += type
350
- .getTypes()
351
- .map((v) => getTypeName(v))
352
- .join(" | ");
353
-
354
- return code;
355
- }
356
-
357
- printCodeScalar(type) {
358
- return `scalar ${getTypeName(type)}`;
359
- }
360
-
361
- printCodeArguments(type) {
362
- if (!hasProperty(type, "args") || type.args.length === 0) {
363
- return "";
364
- }
365
-
366
- let code = `(${MARKDOWN_EOL}`;
367
- code += type.args.reduce((r, v) => {
368
- const defaultValue = getDefaultValue(v);
369
- const hasDefaultValue =
370
- typeof defaultValue !== "undefined" && defaultValue !== null;
371
- const printedDefault = hasDefaultValue ? ` = ${getDefaultValue(v)}` : "";
372
- const propType = v.type.toString();
373
- const propName = v.name.toString();
374
- return `${r} ${propName}: ${propType}${printedDefault}${MARKDOWN_EOL}`;
375
- }, "");
376
- code += `)`;
377
-
378
- return code;
379
- }
380
-
381
- printCodeField(type) {
382
- let code = `${getTypeName(type)}`;
383
- code += this.printCodeArguments(type);
384
- code += `: ${getTypeName(type.type)}${MARKDOWN_EOL}`;
385
-
386
- return code;
387
- }
388
-
389
- printCodeDirectiveLocation(type) {
390
- if (
391
- typeof type.locations === "undefined" ||
392
- type.locations == null ||
393
- type.locations.length === 0
394
- ) {
395
- return "";
396
- }
397
-
398
- let code = ` on `;
399
- const separator = `\r\n | `;
400
- if (type.locations.length > 1) {
401
- code += separator;
402
- }
403
- code += type.locations.join(separator).trim();
404
-
405
- return code;
406
- }
407
-
408
- printCodeDirective(type) {
409
- let code = `directive @${getTypeName(type)}`;
410
- code += this.printCodeArguments(type);
411
- code += this.printCodeDirectiveLocation(type);
412
-
413
- return code;
414
- }
415
-
416
- printCodeType(type) {
417
- let entity;
418
-
419
- switch (true) {
420
- case isInputType(type):
421
- entity = "input";
422
- break;
423
- case isInterfaceType(type):
424
- entity = "interface";
425
- break;
426
- default:
427
- entity = "type";
428
- }
429
-
430
- const name = getTypeName(type);
431
- const extendsInterface =
432
- hasMethod(type, "getInterfaces") && type.getInterfaces().length > 0
433
- ? ` implements ${type
434
- .getInterfaces()
435
- .map((field) => getTypeName(field))
436
- .join(", ")}`
437
- : "";
438
- const typeFields = getFields(type)
439
- .map((v) => ` ${this.printCodeField(v)}`)
440
- .join("");
441
-
442
- return `${entity} ${name}${extendsInterface} {${MARKDOWN_EOL}${typeFields}}`;
443
- }
444
-
445
- printHeader(id, title, options) {
446
- const { toc, pagination } = { toc: true, pagination: true, ...options };
447
- const pagination_buttons = pagination
448
- ? ""
449
- : `pagination_next: null${MARKDOWN_EOL}pagination_prev: null${MARKDOWN_EOL}`;
450
- return `---${MARKDOWN_EOL}id: ${id}${MARKDOWN_EOL}title: ${title}\nhide_table_of_contents: ${!toc}${MARKDOWN_EOL}${pagination_buttons}---${MARKDOWN_EOL}`;
451
- }
452
-
453
- printDeprecation(type) {
454
- if (!type.isDeprecated) {
455
- return "";
456
- }
457
-
458
- const reason = type.deprecationReason
459
- ? ": " + escapeMDX(type.deprecationReason)
460
- : "";
461
- return `<Badge class="warning" text="DEPRECATED${reason}"/>${MARKDOWN_EOP}`;
462
- }
463
-
464
- printDescription(type, noText = NO_DESCRIPTION_TEXT) {
465
- const description =
466
- hasProperty(type, "description") && escapeMDX(type.description);
467
- return `${this.printDeprecation(type)}${description || noText}`;
468
- }
469
-
470
- printSpecification(type) {
471
- if (!type.specifiedByURL && !type.specifiedByUrl) {
472
- return "";
473
- }
474
-
475
- const url = type.specifiedByURL || type.specifiedByUrl;
476
-
477
- // Needs newline between "export const specifiedByLinkCss" and markdown header to prevent compilation error in docusaurus
478
- return `${HEADER_SECTION_LEVEL} <SpecifiedBy url="${url}"/>${MARKDOWN_EOP}`;
479
- }
480
-
481
- printCode(type) {
482
- let code = `${MARKDOWN_EOL}\`\`\`graphql${MARKDOWN_EOL}`;
483
- switch (true) {
484
- case isOperation(type):
485
- code += this.printCodeField(type);
486
- break;
487
- case isEnumType(type):
488
- code += this.printCodeEnum(type);
489
- break;
490
- case isUnionType(type):
491
- code += this.printCodeUnion(type);
492
- break;
493
- case isInterfaceType(type):
494
- case isObjectType(type):
495
- case isInputType(type):
496
- code += this.printCodeType(type);
497
- break;
498
- case isScalarType(type):
499
- code += this.printCodeScalar(type);
500
- break;
501
- case isDirectiveType(type):
502
- code += this.printCodeDirective(type);
503
- break;
504
- default:
505
- code += `"${getTypeName(type)}" not supported`;
506
- }
507
- return code.trim() + `${MARKDOWN_EOL}\`\`\`${MARKDOWN_EOL}`;
508
- }
509
-
510
- printTypeMetadata(type) {
511
- let metadata;
512
-
513
- switch (true) {
514
- case isScalarType(type):
515
- return this.printSpecification(type);
516
- case isEnumType(type):
517
- return this.printSection(type.getValues(), "Values", {
518
- parentType: type.name,
519
- });
520
- case isUnionType(type):
521
- return this.printSection(type.getTypes(), "Possible types");
522
- case isObjectType(type):
523
- case isInterfaceType(type):
524
- case isInputType(type): {
525
- metadata = this.printSection(getFields(type), "Fields", {
526
- parentType: type.name,
527
- });
528
- if (hasMethod(type, "getInterfaces")) {
529
- metadata += this.printSection(type.getInterfaces(), "Interfaces");
530
- }
531
- return metadata;
532
- }
533
- case isDirectiveType(type): {
534
- metadata = this.printSection(type.args, "Arguments", {
535
- parentType: type.name,
536
- });
537
- return metadata;
538
- }
539
- case isOperation(type): {
540
- metadata = this.printSection(type.args, "Arguments", {
541
- parentType: type.name,
542
- });
543
- const queryType = getTypeName(type.type).replace(/[![\]]*/g, "");
544
- metadata += this.printSection([this.schema.getType(queryType)], "Type");
545
-
546
- return metadata;
547
- }
548
- default:
549
- return metadata;
550
- }
551
- }
552
-
553
- printRelations(type) {
554
- const relations = {
555
- "Returned by": getRelationOfReturn,
556
- "Member of": getRelationOfField,
557
- "Implemented by": getRelationOfImplementation,
558
- };
559
-
560
- let data = "";
561
- for (const [section, getRelation] of Object.entries(relations)) {
562
- data += this.printRelationOf(type, section, getRelation);
563
- }
564
-
565
- return data;
566
- }
567
-
568
- printRelationOf(type, section, getRelation) {
569
- if (
570
- typeof type === "undefined" ||
571
- typeof getRelation !== "function" ||
572
- isOperation(type)
573
- ) {
574
- return "";
575
- }
576
-
577
- const relations = getRelation(type, this.schema);
578
-
579
- if (typeof relations === "undefined") {
580
- return "";
581
- }
582
-
583
- let data = [];
584
- for (const [relation, types] of Object.entries(relations)) {
585
- if (types.length === 0) {
586
- continue;
587
- }
588
-
589
- const category = this.getRootTypeLocaleFromString(relation);
590
- data = data.concat(types.map((t) => this.getRelationLink(category, t)));
591
- }
592
-
593
- if (data.length === 0) {
594
- return "";
595
- }
596
-
597
- const content = [...data]
598
- .sort((a, b) => a.localeCompare(b))
599
- .join("<Bullet />");
600
-
601
- return `${HEADER_SECTION_LEVEL} ${section}${MARKDOWN_EOP}${content}${MARKDOWN_EOP}`;
602
- }
603
-
604
- printType(name, type, options) {
605
- if (typeof type === "undefined" || type === null) {
606
- return "";
607
- }
608
-
609
- const header = this.printHeader(name, getTypeName(type), options);
610
- const description = this.printDescription(type);
611
- const code = this.printCode(type);
612
- const metadata = this.printTypeMetadata(type);
613
- const relations = this.relatedTypeSection ? this.printRelations(type) : "";
614
-
615
- return `${header}${MARKDOWN_EOP}${mdx}${MARKDOWN_EOP}${description}${MARKDOWN_EOP}${code}${MARKDOWN_EOP}${metadata}${MARKDOWN_EOP}${relations}${MARKDOWN_EOP}`;
616
- }
617
- };
3
+ module.exports = Printer;