@graphql-markdown/printer-legacy 1.0.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/LICENSE +21 -0
- package/README.md +4 -0
- package/package.json +36 -0
- package/src/const/mdx.js +7 -0
- package/src/const/strings.js +30 -0
- package/src/index.js +583 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2020 Grégory Heitz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
# @graphql-markdown/printer-legacy
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@graphql-markdown/printer-legacy)
|
|
4
|
+
[](https://raw.githubusercontent.com/graphql-markdown/graphql-markdown/main/LICENSE)
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@graphql-markdown/printer-legacy",
|
|
3
|
+
"homepage": "https://graphql-markdown.github.io",
|
|
4
|
+
"description": "GraphQL-Markdown legacy Docusaurus printer.",
|
|
5
|
+
"bugs": {
|
|
6
|
+
"url": "https://github.com/graphql-markdown/graphql-markdown/issues"
|
|
7
|
+
},
|
|
8
|
+
"version": "1.0.0",
|
|
9
|
+
"license": "MIT",
|
|
10
|
+
"main": "src/index.js",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/graphql-markdown/graphql-markdown.git"
|
|
14
|
+
},
|
|
15
|
+
"author": {
|
|
16
|
+
"name": "Gregory Heitz"
|
|
17
|
+
},
|
|
18
|
+
"scripts": {
|
|
19
|
+
"prepack": "pinst --disable",
|
|
20
|
+
"postpack": "pinst --enable",
|
|
21
|
+
"test": "jest",
|
|
22
|
+
"stryker": "stryker run"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@graphql-markdown/utils": "1.0.0"
|
|
26
|
+
},
|
|
27
|
+
"directories": {
|
|
28
|
+
"test": "tests"
|
|
29
|
+
},
|
|
30
|
+
"engines": {
|
|
31
|
+
"node": ">=16.14"
|
|
32
|
+
},
|
|
33
|
+
"publishConfig": {
|
|
34
|
+
"access": "public"
|
|
35
|
+
}
|
|
36
|
+
}
|
package/src/const/mdx.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
module.exports = `
|
|
2
|
+
export const Bullet = () => <><span style={{ fontWeight: 'normal', fontSize: '.5em', color: 'var(--ifm-color-secondary-darkest)' }}> ● </span></>
|
|
3
|
+
|
|
4
|
+
export const SpecifiedBy = (props) => <>Specification<a className="link" style={{ fontSize:'1.5em', paddingLeft:'4px' }} target="_blank" href={props.url} title={'Specified by ' + props.url}>⎘</a></>
|
|
5
|
+
|
|
6
|
+
export const Badge = (props) => <><span class={'badge badge--' + props.class}>{props.text}</span></>
|
|
7
|
+
`;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
const ROOT_TYPE_LOCALE = {
|
|
2
|
+
QUERY: { singular: "query", plural: "queries" },
|
|
3
|
+
MUTATION: { singular: "mutation", plural: "mutations" },
|
|
4
|
+
SUBSCRIPTION: { singular: "subscription", plural: "subscriptions" },
|
|
5
|
+
TYPE: { singular: "object", plural: "objects" },
|
|
6
|
+
INTERFACE: { singular: "interface", plural: "interfaces" },
|
|
7
|
+
DIRECTIVE: { singular: "directive", plural: "directives" },
|
|
8
|
+
SCALAR: { singular: "scalar", plural: "scalars" },
|
|
9
|
+
ENUM: { singular: "enum", plural: "enums" },
|
|
10
|
+
OPERATION: { singular: "operation", plural: "operations" },
|
|
11
|
+
UNION: { singular: "union", plural: "unions" },
|
|
12
|
+
INPUT: { singular: "input", plural: "inputs" },
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
const HEADER_SECTION_LEVEL = "###";
|
|
16
|
+
const HEADER_SECTION_SUB_LEVEL = "####";
|
|
17
|
+
const HEADER_SECTION_ITEM_LEVEL = "#####";
|
|
18
|
+
const NO_DESCRIPTION_TEXT = "No description";
|
|
19
|
+
const MARKDOWN_EOL = "\n";
|
|
20
|
+
const MARKDOWN_EOP = "\n\n";
|
|
21
|
+
|
|
22
|
+
module.exports = {
|
|
23
|
+
ROOT_TYPE_LOCALE,
|
|
24
|
+
HEADER_SECTION_LEVEL,
|
|
25
|
+
HEADER_SECTION_SUB_LEVEL,
|
|
26
|
+
HEADER_SECTION_ITEM_LEVEL,
|
|
27
|
+
NO_DESCRIPTION_TEXT,
|
|
28
|
+
MARKDOWN_EOL,
|
|
29
|
+
MARKDOWN_EOP,
|
|
30
|
+
};
|
package/src/index.js
ADDED
|
@@ -0,0 +1,583 @@
|
|
|
1
|
+
const {
|
|
2
|
+
isEnumType,
|
|
3
|
+
isUnionType,
|
|
4
|
+
isObjectType,
|
|
5
|
+
isScalarType,
|
|
6
|
+
isOperation,
|
|
7
|
+
getDefaultValue,
|
|
8
|
+
getTypeName,
|
|
9
|
+
getFields,
|
|
10
|
+
isDirectiveType,
|
|
11
|
+
isParametrizedField,
|
|
12
|
+
isInterfaceType,
|
|
13
|
+
getNamedType,
|
|
14
|
+
isInputType,
|
|
15
|
+
isListType,
|
|
16
|
+
isNonNullType,
|
|
17
|
+
isLeafType,
|
|
18
|
+
getRelationOfReturn,
|
|
19
|
+
getRelationOfField,
|
|
20
|
+
getRelationOfImplementation,
|
|
21
|
+
} = require("@graphql-markdown/utils").graphql;
|
|
22
|
+
|
|
23
|
+
const {
|
|
24
|
+
string: { toSlug, escapeMDX },
|
|
25
|
+
object: { hasProperty, hasMethod },
|
|
26
|
+
url: { pathUrl },
|
|
27
|
+
} = require("@graphql-markdown/utils").scalars;
|
|
28
|
+
|
|
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 } = {
|
|
46
|
+
groups: undefined,
|
|
47
|
+
printTypeOptions: undefined,
|
|
48
|
+
},
|
|
49
|
+
) {
|
|
50
|
+
this.schema = schema;
|
|
51
|
+
this.baseURL = baseURL;
|
|
52
|
+
this.linkRoot = linkRoot;
|
|
53
|
+
this.groups = groups;
|
|
54
|
+
this.parentTypePrefix = printTypeOptions?.parentTypePrefix ?? true;
|
|
55
|
+
this.relatedTypeSection = printTypeOptions?.relatedTypeSection ?? true;
|
|
56
|
+
this.typeBadges = printTypeOptions?.typeBadges ?? true;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
getRootTypeLocaleFromString(text) {
|
|
60
|
+
for (const [type, props] of Object.entries(ROOT_TYPE_LOCALE)) {
|
|
61
|
+
if (Object.values(props).includes(text)) {
|
|
62
|
+
return ROOT_TYPE_LOCALE[type];
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
return undefined;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
getLinkCategory(graphLQLNamedType) {
|
|
69
|
+
switch (true) {
|
|
70
|
+
case isEnumType(graphLQLNamedType):
|
|
71
|
+
return ROOT_TYPE_LOCALE.ENUM;
|
|
72
|
+
case isUnionType(graphLQLNamedType):
|
|
73
|
+
return ROOT_TYPE_LOCALE.UNION;
|
|
74
|
+
case isInterfaceType(graphLQLNamedType):
|
|
75
|
+
return ROOT_TYPE_LOCALE.INTERFACE;
|
|
76
|
+
case isObjectType(graphLQLNamedType):
|
|
77
|
+
return ROOT_TYPE_LOCALE.TYPE;
|
|
78
|
+
case isInputType(graphLQLNamedType):
|
|
79
|
+
return ROOT_TYPE_LOCALE.INPUT;
|
|
80
|
+
case isScalarType(graphLQLNamedType):
|
|
81
|
+
return ROOT_TYPE_LOCALE.SCALAR;
|
|
82
|
+
case isDirectiveType(graphLQLNamedType):
|
|
83
|
+
return ROOT_TYPE_LOCALE.DIRECTIVE;
|
|
84
|
+
case isOperation(graphLQLNamedType):
|
|
85
|
+
return ROOT_TYPE_LOCALE.OPERATION;
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
getGroup(type) {
|
|
91
|
+
if (typeof this.groups === "undefined") {
|
|
92
|
+
return "";
|
|
93
|
+
}
|
|
94
|
+
const graphLQLNamedType = getNamedType(type);
|
|
95
|
+
const typeName = graphLQLNamedType.name || graphLQLNamedType;
|
|
96
|
+
return hasProperty(this.groups, typeName)
|
|
97
|
+
? toSlug(this.groups[typeName])
|
|
98
|
+
: "";
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
toLink(type, name, operation) {
|
|
102
|
+
const fallback = {
|
|
103
|
+
text: name,
|
|
104
|
+
url: "#",
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const graphLQLNamedType = getNamedType(type);
|
|
108
|
+
|
|
109
|
+
let category = this.getLinkCategory(graphLQLNamedType);
|
|
110
|
+
|
|
111
|
+
if (
|
|
112
|
+
typeof category === "undefined" ||
|
|
113
|
+
typeof graphLQLNamedType === "undefined" ||
|
|
114
|
+
graphLQLNamedType === null
|
|
115
|
+
) {
|
|
116
|
+
return fallback;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// special case for support relation map
|
|
120
|
+
if (category === ROOT_TYPE_LOCALE.OPERATION) {
|
|
121
|
+
if (typeof operation === "undefined") {
|
|
122
|
+
return fallback;
|
|
123
|
+
}
|
|
124
|
+
category = operation;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const text = graphLQLNamedType.name || graphLQLNamedType;
|
|
128
|
+
const group = this.getGroup(type);
|
|
129
|
+
const url = pathUrl.join(
|
|
130
|
+
this.linkRoot,
|
|
131
|
+
this.baseURL,
|
|
132
|
+
group,
|
|
133
|
+
category.plural,
|
|
134
|
+
toSlug(text),
|
|
135
|
+
);
|
|
136
|
+
|
|
137
|
+
return {
|
|
138
|
+
text: text,
|
|
139
|
+
url: url,
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
getRelationLink(category, type) {
|
|
144
|
+
if (typeof category === "undefined") {
|
|
145
|
+
return "";
|
|
146
|
+
}
|
|
147
|
+
const link = this.toLink(type, type.name, category);
|
|
148
|
+
return `[\`${link.text}\`](${link.url}) <Badge class="secondary" text="${category.singular}"/>`;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
printSection(
|
|
152
|
+
values,
|
|
153
|
+
section,
|
|
154
|
+
{ level, parentType } = {
|
|
155
|
+
level: HEADER_SECTION_LEVEL,
|
|
156
|
+
parentType: undefined,
|
|
157
|
+
},
|
|
158
|
+
) {
|
|
159
|
+
if (values.length === 0) {
|
|
160
|
+
return "";
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if (typeof level === "undefined") {
|
|
164
|
+
level = HEADER_SECTION_LEVEL;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return `${level} ${section}${MARKDOWN_EOP}${this.printSectionItems(values, {
|
|
168
|
+
parentType,
|
|
169
|
+
})}${MARKDOWN_EOP}`;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
printSectionItems(
|
|
173
|
+
values,
|
|
174
|
+
{ level, parentType } = {
|
|
175
|
+
level: HEADER_SECTION_SUB_LEVEL,
|
|
176
|
+
parentType: undefined,
|
|
177
|
+
},
|
|
178
|
+
) {
|
|
179
|
+
if (!Array.isArray(values)) {
|
|
180
|
+
return "";
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
if (typeof level === "undefined") {
|
|
184
|
+
level = HEADER_SECTION_SUB_LEVEL;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
return values
|
|
188
|
+
.map((v) => v && this.printSectionItem(v, { level, parentType }))
|
|
189
|
+
.join(MARKDOWN_EOP);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
printLinkAttributes(type, text) {
|
|
193
|
+
if (typeof type == "undefined") {
|
|
194
|
+
return text ?? "";
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (!isLeafType(type, text) && typeof type.ofType != "undefined") {
|
|
198
|
+
text = this.printLinkAttributes(type.ofType, text);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (isListType(type)) {
|
|
202
|
+
return `[${text}]`;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
if (isNonNullType(type)) {
|
|
206
|
+
return `${text}!`;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
return text;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
printLink(type, withAttributes = false, parentType = undefined) {
|
|
213
|
+
const link = this.toLink(type, getTypeName(type));
|
|
214
|
+
|
|
215
|
+
if (!withAttributes) {
|
|
216
|
+
const printParentType =
|
|
217
|
+
this.parentTypePrefix && typeof parentType !== "undefined";
|
|
218
|
+
const text = printParentType
|
|
219
|
+
? `<code style={{ fontWeight: 'normal' }}>${parentType}.<b>${link.text}</b></code>`
|
|
220
|
+
: `\`${link.text}\``;
|
|
221
|
+
return `[${text}](${link.url})`;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
const text = this.printLinkAttributes(type, link.text);
|
|
225
|
+
|
|
226
|
+
return `[\`${text}\`](${link.url})`;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
getTypeBadges(type) {
|
|
230
|
+
const rootType = hasProperty(type, "type") ? type.type : type;
|
|
231
|
+
|
|
232
|
+
const badges = [];
|
|
233
|
+
|
|
234
|
+
if (type.isDeprecated) {
|
|
235
|
+
badges.push("deprecated");
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
if (isNonNullType(rootType)) {
|
|
239
|
+
badges.push("non-null");
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (isListType(rootType)) {
|
|
243
|
+
badges.push("list");
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const category = this.getLinkCategory(getNamedType(rootType));
|
|
247
|
+
if (typeof category !== "undefined") {
|
|
248
|
+
badges.push(category);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const group = this.getGroup(rootType);
|
|
252
|
+
if (group !== "") {
|
|
253
|
+
badges.push(group);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
return badges;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
printBadges(type) {
|
|
260
|
+
if (!this.typeBadges) {
|
|
261
|
+
return "";
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
const badges = this.getTypeBadges(type);
|
|
265
|
+
|
|
266
|
+
if (badges.length === 0) {
|
|
267
|
+
return "";
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return badges
|
|
271
|
+
.map(
|
|
272
|
+
(badge) =>
|
|
273
|
+
`<Badge class="secondary" text="${badge.singular ?? badge}"/>`,
|
|
274
|
+
)
|
|
275
|
+
.join(" ");
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
printParentLink(type) {
|
|
279
|
+
return hasProperty(type, "type")
|
|
280
|
+
? `<Bullet />${this.printLink(type.type, true)}`
|
|
281
|
+
: "";
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
printSectionItem(
|
|
285
|
+
type,
|
|
286
|
+
{ level, parentType } = {
|
|
287
|
+
level: HEADER_SECTION_SUB_LEVEL,
|
|
288
|
+
parentType: undefined,
|
|
289
|
+
},
|
|
290
|
+
) {
|
|
291
|
+
if (typeof type === "undefined" || type === null) {
|
|
292
|
+
return "";
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
if (typeof level === "undefined") {
|
|
296
|
+
level = HEADER_SECTION_SUB_LEVEL;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
const typeNameLink = this.printLink(type, false, parentType);
|
|
300
|
+
const description = this.printDescription(type, "");
|
|
301
|
+
const badges = this.printBadges(type);
|
|
302
|
+
const parentTypeLink = this.printParentLink(type);
|
|
303
|
+
|
|
304
|
+
let section = `${level} ${typeNameLink}${parentTypeLink} ${badges}${MARKDOWN_EOL}> ${description}${MARKDOWN_EOL}> `;
|
|
305
|
+
if (isParametrizedField(type)) {
|
|
306
|
+
section += this.printSectionItems(type.args, {
|
|
307
|
+
level: HEADER_SECTION_ITEM_LEVEL,
|
|
308
|
+
parentType:
|
|
309
|
+
typeof parentType === "undefined"
|
|
310
|
+
? undefined
|
|
311
|
+
: `${parentType}.${type.name}`,
|
|
312
|
+
});
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
return section;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
printCodeEnum(type) {
|
|
319
|
+
if (!isEnumType(type)) {
|
|
320
|
+
return "";
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
let code = `enum ${getTypeName(type)} {${MARKDOWN_EOL}`;
|
|
324
|
+
code += type
|
|
325
|
+
.getValues()
|
|
326
|
+
.map((v) => ` ${getTypeName(v)}`)
|
|
327
|
+
.join(MARKDOWN_EOL);
|
|
328
|
+
code += `${MARKDOWN_EOL}}`;
|
|
329
|
+
|
|
330
|
+
return code;
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
printCodeUnion(type) {
|
|
334
|
+
if (!isUnionType(type)) {
|
|
335
|
+
return "";
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
let code = `union ${getTypeName(type)} = `;
|
|
339
|
+
code += type
|
|
340
|
+
.getTypes()
|
|
341
|
+
.map((v) => getTypeName(v))
|
|
342
|
+
.join(" | ");
|
|
343
|
+
|
|
344
|
+
return code;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
printCodeScalar(type) {
|
|
348
|
+
return `scalar ${getTypeName(type)}`;
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
printCodeArguments(type) {
|
|
352
|
+
if (!hasProperty(type, "args") || type.args.length === 0) {
|
|
353
|
+
return "";
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
let code = `(${MARKDOWN_EOL}`;
|
|
357
|
+
code += type.args.reduce((r, v) => {
|
|
358
|
+
const defaultValue = getDefaultValue(v);
|
|
359
|
+
const hasDefaultValue =
|
|
360
|
+
typeof defaultValue !== "undefined" && defaultValue !== null;
|
|
361
|
+
const printedDefault = hasDefaultValue ? ` = ${getDefaultValue(v)}` : "";
|
|
362
|
+
const propType = v.type.toString();
|
|
363
|
+
const propName = v.name.toString();
|
|
364
|
+
return `${r} ${propName}: ${propType}${printedDefault}${MARKDOWN_EOL}`;
|
|
365
|
+
}, "");
|
|
366
|
+
code += `)`;
|
|
367
|
+
|
|
368
|
+
return code;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
printCodeField(type) {
|
|
372
|
+
let code = `${getTypeName(type)}`;
|
|
373
|
+
code += this.printCodeArguments(type);
|
|
374
|
+
code += `: ${getTypeName(type.type)}${MARKDOWN_EOL}`;
|
|
375
|
+
|
|
376
|
+
return code;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
printCodeDirective(type) {
|
|
380
|
+
let code = `directive @${getTypeName(type)}`;
|
|
381
|
+
code += this.printCodeArguments(type);
|
|
382
|
+
|
|
383
|
+
return code;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
printCodeType(type) {
|
|
387
|
+
let entity;
|
|
388
|
+
|
|
389
|
+
switch (true) {
|
|
390
|
+
case isInputType(type):
|
|
391
|
+
entity = "input";
|
|
392
|
+
break;
|
|
393
|
+
case isInterfaceType(type):
|
|
394
|
+
entity = "interface";
|
|
395
|
+
break;
|
|
396
|
+
default:
|
|
397
|
+
entity = "type";
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const name = getTypeName(type);
|
|
401
|
+
const extendsInterface =
|
|
402
|
+
hasMethod(type, "getInterfaces") && type.getInterfaces().length > 0
|
|
403
|
+
? ` implements ${type
|
|
404
|
+
.getInterfaces()
|
|
405
|
+
.map((field) => getTypeName(field))
|
|
406
|
+
.join(", ")}`
|
|
407
|
+
: "";
|
|
408
|
+
const typeFields = getFields(type)
|
|
409
|
+
.map((v) => ` ${this.printCodeField(v)}`)
|
|
410
|
+
.join("");
|
|
411
|
+
|
|
412
|
+
return `${entity} ${name}${extendsInterface} {${MARKDOWN_EOL}${typeFields}}`;
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
printHeader(id, title, options) {
|
|
416
|
+
const { toc, pagination } = { toc: true, pagination: true, ...options };
|
|
417
|
+
const pagination_buttons = pagination
|
|
418
|
+
? ""
|
|
419
|
+
: `pagination_next: null${MARKDOWN_EOL}pagination_prev: null${MARKDOWN_EOL}`;
|
|
420
|
+
return `---${MARKDOWN_EOL}id: ${id}${MARKDOWN_EOL}title: ${title}\nhide_table_of_contents: ${!toc}${MARKDOWN_EOL}${pagination_buttons}---${MARKDOWN_EOL}`;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
printDeprecation(type) {
|
|
424
|
+
if (!type.isDeprecated) {
|
|
425
|
+
return "";
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
const reason = type.deprecationReason
|
|
429
|
+
? ": " + escapeMDX(type.deprecationReason)
|
|
430
|
+
: "";
|
|
431
|
+
return `<Badge class="warning" text="DEPRECATED${reason}"/>${MARKDOWN_EOP}`;
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
printDescription(type, noText = NO_DESCRIPTION_TEXT) {
|
|
435
|
+
const description =
|
|
436
|
+
hasProperty(type, "description") && escapeMDX(type.description);
|
|
437
|
+
return `${this.printDeprecation(type)}${description || noText}`;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
printSpecification(type) {
|
|
441
|
+
if (!type.specifiedByURL && !type.specifiedByUrl) {
|
|
442
|
+
return "";
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const url = type.specifiedByURL || type.specifiedByUrl;
|
|
446
|
+
|
|
447
|
+
// Needs newline between "export const specifiedByLinkCss" and markdown header to prevent compilation error in docusaurus
|
|
448
|
+
return `${HEADER_SECTION_LEVEL} <SpecifiedBy url="${url}"/>${MARKDOWN_EOP}`;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
printCode(type) {
|
|
452
|
+
let code = `${MARKDOWN_EOL}\`\`\`graphql${MARKDOWN_EOL}`;
|
|
453
|
+
switch (true) {
|
|
454
|
+
case isOperation(type):
|
|
455
|
+
code += this.printCodeField(type);
|
|
456
|
+
break;
|
|
457
|
+
case isEnumType(type):
|
|
458
|
+
code += this.printCodeEnum(type);
|
|
459
|
+
break;
|
|
460
|
+
case isUnionType(type):
|
|
461
|
+
code += this.printCodeUnion(type);
|
|
462
|
+
break;
|
|
463
|
+
case isInterfaceType(type):
|
|
464
|
+
case isObjectType(type):
|
|
465
|
+
case isInputType(type):
|
|
466
|
+
code += this.printCodeType(type);
|
|
467
|
+
break;
|
|
468
|
+
case isScalarType(type):
|
|
469
|
+
code += this.printCodeScalar(type);
|
|
470
|
+
break;
|
|
471
|
+
case isDirectiveType(type):
|
|
472
|
+
code += this.printCodeDirective(type);
|
|
473
|
+
break;
|
|
474
|
+
default:
|
|
475
|
+
code += `"${getTypeName(type)}" not supported`;
|
|
476
|
+
}
|
|
477
|
+
return code.trim() + `${MARKDOWN_EOL}\`\`\`${MARKDOWN_EOL}`;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
printTypeMetadata(type) {
|
|
481
|
+
let metadata;
|
|
482
|
+
switch (true) {
|
|
483
|
+
case isScalarType(type):
|
|
484
|
+
return this.printSpecification(type);
|
|
485
|
+
case isEnumType(type):
|
|
486
|
+
return this.printSection(type.getValues(), "Values", {
|
|
487
|
+
parentType: type.name,
|
|
488
|
+
});
|
|
489
|
+
case isUnionType(type):
|
|
490
|
+
return this.printSection(type.getTypes(), "Possible types");
|
|
491
|
+
case isObjectType(type):
|
|
492
|
+
case isInterfaceType(type):
|
|
493
|
+
case isInputType(type):
|
|
494
|
+
metadata = this.printSection(getFields(type), "Fields", {
|
|
495
|
+
parentType: type.name,
|
|
496
|
+
});
|
|
497
|
+
if (hasMethod(type, "getInterfaces")) {
|
|
498
|
+
metadata += this.printSection(type.getInterfaces(), "Interfaces");
|
|
499
|
+
}
|
|
500
|
+
return metadata;
|
|
501
|
+
case isDirectiveType(type):
|
|
502
|
+
case isOperation(type):
|
|
503
|
+
metadata = this.printSection(type.args, "Arguments", {
|
|
504
|
+
parentType: type.name,
|
|
505
|
+
});
|
|
506
|
+
|
|
507
|
+
if (isOperation(type)) {
|
|
508
|
+
const queryType = getTypeName(type.type).replace(/[![\]]*/g, "");
|
|
509
|
+
metadata += this.printSection(
|
|
510
|
+
[this.schema.getType(queryType)],
|
|
511
|
+
"Type",
|
|
512
|
+
);
|
|
513
|
+
}
|
|
514
|
+
return metadata;
|
|
515
|
+
}
|
|
516
|
+
return metadata;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
printRelations(type) {
|
|
520
|
+
const relations = {
|
|
521
|
+
"Returned by": getRelationOfReturn,
|
|
522
|
+
"Member of": getRelationOfField,
|
|
523
|
+
"Implemented by": getRelationOfImplementation,
|
|
524
|
+
};
|
|
525
|
+
|
|
526
|
+
let data = "";
|
|
527
|
+
for (const [section, getRelation] of Object.entries(relations)) {
|
|
528
|
+
data += this.printRelationOf(type, section, getRelation);
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
return data;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
printRelationOf(type, section, getRelation) {
|
|
535
|
+
if (
|
|
536
|
+
typeof type === "undefined" ||
|
|
537
|
+
typeof getRelation !== "function" ||
|
|
538
|
+
isOperation(type)
|
|
539
|
+
) {
|
|
540
|
+
return "";
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
const relations = getRelation(type, this.schema);
|
|
544
|
+
|
|
545
|
+
if (typeof relations === "undefined") {
|
|
546
|
+
return "";
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
let data = [];
|
|
550
|
+
for (const [relation, types] of Object.entries(relations)) {
|
|
551
|
+
if (types.length === 0) {
|
|
552
|
+
continue;
|
|
553
|
+
}
|
|
554
|
+
|
|
555
|
+
const category = this.getRootTypeLocaleFromString(relation);
|
|
556
|
+
data = data.concat(types.map((t) => this.getRelationLink(category, t)));
|
|
557
|
+
}
|
|
558
|
+
|
|
559
|
+
if (data.length === 0) {
|
|
560
|
+
return "";
|
|
561
|
+
}
|
|
562
|
+
|
|
563
|
+
const content = [...data]
|
|
564
|
+
.sort((a, b) => a.localeCompare(b))
|
|
565
|
+
.join("<Bullet />");
|
|
566
|
+
|
|
567
|
+
return `${HEADER_SECTION_LEVEL} ${section}${MARKDOWN_EOP}${content}${MARKDOWN_EOP}`;
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
printType(name, type, options) {
|
|
571
|
+
if (typeof type === "undefined" || type === null) {
|
|
572
|
+
return "";
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
const header = this.printHeader(name, getTypeName(type), options);
|
|
576
|
+
const description = this.printDescription(type);
|
|
577
|
+
const code = this.printCode(type);
|
|
578
|
+
const metadata = this.printTypeMetadata(type);
|
|
579
|
+
const relations = this.relatedTypeSection ? this.printRelations(type) : "";
|
|
580
|
+
|
|
581
|
+
return `${header}${MARKDOWN_EOP}${mdx}${MARKDOWN_EOP}${description}${MARKDOWN_EOP}${code}${MARKDOWN_EOP}${metadata}${MARKDOWN_EOP}${relations}${MARKDOWN_EOP}`;
|
|
582
|
+
}
|
|
583
|
+
};
|