@mintlify/common 1.0.1335 → 1.0.1336
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/dist/graphql/index.d.ts
CHANGED
package/dist/graphql/index.js
CHANGED
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { getNamedGraphqlType, resolveGraphqlOperation } from './resolveGraphqlOperation.js';
|
|
2
|
+
const printTypeRef = (type) => {
|
|
3
|
+
if (type.kind === 'named')
|
|
4
|
+
return type.name;
|
|
5
|
+
if (type.kind === 'list')
|
|
6
|
+
return `[${printTypeRef(type.ofType)}]`;
|
|
7
|
+
return `${printTypeRef(type.ofType)}!`;
|
|
8
|
+
};
|
|
9
|
+
const isRequired = (value) => value.type.kind === 'nonNull' && value.defaultValue === undefined;
|
|
10
|
+
const renderInputValue = (value) => {
|
|
11
|
+
const req = isRequired(value) ? ' (required)' : '';
|
|
12
|
+
const def = value.defaultValueLiteral !== undefined ? ` = ${value.defaultValueLiteral}` : '';
|
|
13
|
+
let md = `**\`${value.name}\`**: \`${printTypeRef(value.type)}\`${def}${req}`;
|
|
14
|
+
if (value.description)
|
|
15
|
+
md += `\n\n${value.description}`;
|
|
16
|
+
if (value.deprecationReason)
|
|
17
|
+
md += `\n\n> Deprecated: ${value.deprecationReason}`;
|
|
18
|
+
return md;
|
|
19
|
+
};
|
|
20
|
+
const renderField = (field) => {
|
|
21
|
+
let md = `**\`${field.name}\`**: \`${printTypeRef(field.type)}\``;
|
|
22
|
+
if (field.description)
|
|
23
|
+
md += `\n\n${field.description}`;
|
|
24
|
+
if (field.deprecationReason)
|
|
25
|
+
md += `\n\n> Deprecated: ${field.deprecationReason}`;
|
|
26
|
+
if (field.arguments.length > 0) {
|
|
27
|
+
md +=
|
|
28
|
+
'\n\n**Arguments:**\n\n' +
|
|
29
|
+
field.arguments
|
|
30
|
+
.map((arg) => {
|
|
31
|
+
const def = arg.defaultValueLiteral !== undefined ? ` = ${arg.defaultValueLiteral}` : '';
|
|
32
|
+
return `- \`${arg.name}\`: \`${printTypeRef(arg.type)}\`${def}${arg.description ? ` — ${arg.description}` : ''}`;
|
|
33
|
+
})
|
|
34
|
+
.join('\n');
|
|
35
|
+
}
|
|
36
|
+
return md;
|
|
37
|
+
};
|
|
38
|
+
const renderEnumValue = (value) => {
|
|
39
|
+
let md = `**\`${value.name}\`**`;
|
|
40
|
+
if (value.description)
|
|
41
|
+
md += ` — ${value.description}`;
|
|
42
|
+
if (value.deprecationReason)
|
|
43
|
+
md += `\n\n> Deprecated: ${value.deprecationReason}`;
|
|
44
|
+
return md;
|
|
45
|
+
};
|
|
46
|
+
const SEPARATOR = '\n\n---\n\n';
|
|
47
|
+
const renderNamedType = (type) => {
|
|
48
|
+
let md = `**Kind**: \`${type.kind}\`\n\n`;
|
|
49
|
+
if (type.kind === 'object' || type.kind === 'interface') {
|
|
50
|
+
if (type.interfaces.length > 0) {
|
|
51
|
+
md += `## Implements\n\n${type.interfaces.map((name) => `- \`${name}\``).join('\n')}\n\n`;
|
|
52
|
+
}
|
|
53
|
+
if (type.fields.length > 0) {
|
|
54
|
+
md += `## Fields\n\n${type.fields.map(renderField).join(SEPARATOR)}\n\n`;
|
|
55
|
+
}
|
|
56
|
+
if (type.kind === 'interface' && type.possibleTypes.length > 0) {
|
|
57
|
+
md += `## Possible types\n\n${type.possibleTypes.map((name) => `- \`${name}\``).join('\n')}\n\n`;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
else if (type.kind === 'input') {
|
|
61
|
+
if (type.fields.length > 0) {
|
|
62
|
+
md += `## Fields\n\n${type.fields.map(renderInputValue).join(SEPARATOR)}\n\n`;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
else if (type.kind === 'enum') {
|
|
66
|
+
if (type.values.length > 0) {
|
|
67
|
+
md += `## Values\n\n${type.values.map(renderEnumValue).join('\n\n')}\n\n`;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
else if (type.kind === 'union') {
|
|
71
|
+
if (type.possibleTypes.length > 0) {
|
|
72
|
+
md += `## Possible types\n\n${type.possibleTypes.map((name) => `- \`${name}\``).join('\n')}\n\n`;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
return md;
|
|
76
|
+
};
|
|
77
|
+
const fieldCall = (field) => {
|
|
78
|
+
const args = field.arguments.map((arg) => `${arg.name}: ${printTypeRef(arg.type)}`).join(', ');
|
|
79
|
+
return `${field.name}${args ? `(${args})` : ''}`;
|
|
80
|
+
};
|
|
81
|
+
const renderOperation = (data) => {
|
|
82
|
+
const { reference, target } = data;
|
|
83
|
+
const resolved = resolveGraphqlOperation(reference, target);
|
|
84
|
+
if (!resolved)
|
|
85
|
+
return '';
|
|
86
|
+
const { field, parents } = resolved;
|
|
87
|
+
const returnTypeName = getNamedGraphqlType(field.type);
|
|
88
|
+
const returnType = reference.types.find((t) => t.name === returnTypeName);
|
|
89
|
+
const returnFields = (returnType === null || returnType === void 0 ? void 0 : returnType.kind) === 'object' || (returnType === null || returnType === void 0 ? void 0 : returnType.kind) === 'interface' ? returnType.fields : [];
|
|
90
|
+
const sig = `${[...parents, field].map(fieldCall).join('.')}: ${printTypeRef(field.type)}`;
|
|
91
|
+
let md = `**${target.kind}** \`${sig}\`\n\n`;
|
|
92
|
+
if (field.deprecationReason) {
|
|
93
|
+
md += `> Deprecated: ${field.deprecationReason}\n\n`;
|
|
94
|
+
}
|
|
95
|
+
if (field.arguments.length > 0) {
|
|
96
|
+
md += `## Arguments\n\n${field.arguments.map(renderInputValue).join(SEPARATOR)}\n\n`;
|
|
97
|
+
}
|
|
98
|
+
for (const parent of parents) {
|
|
99
|
+
if (parent.arguments.length > 0) {
|
|
100
|
+
md += `## ${parent.name} arguments\n\n${parent.arguments.map(renderInputValue).join(SEPARATOR)}\n\n`;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
md += `## Returns\n\n\`${printTypeRef(field.type)}\`\n\n`;
|
|
104
|
+
if (returnFields.length > 0) {
|
|
105
|
+
md += returnFields.map(renderField).join(SEPARATOR) + '\n\n';
|
|
106
|
+
}
|
|
107
|
+
return md;
|
|
108
|
+
};
|
|
109
|
+
export const renderGraphqlReferenceAsMarkdown = (data) => {
|
|
110
|
+
const { reference, target } = data;
|
|
111
|
+
if (target.kind === 'type') {
|
|
112
|
+
const type = reference.types.find((t) => t.name === target.name);
|
|
113
|
+
if (!type)
|
|
114
|
+
return '';
|
|
115
|
+
return renderNamedType(type);
|
|
116
|
+
}
|
|
117
|
+
return renderOperation(data);
|
|
118
|
+
};
|