UNPKG

6.62 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.getVariablesJSONSchema = exports.defaultJSONSchemaOptions = void 0;
4const graphql_1 = require("graphql");
5exports.defaultJSONSchemaOptions = {
6 useMarkdownDescription: false,
7};
8function text(into, newText) {
9 into.push(newText);
10}
11function renderType(into, t) {
12 if (graphql_1.isNonNullType(t)) {
13 renderType(into, t.ofType);
14 text(into, '!');
15 }
16 else if (graphql_1.isListType(t)) {
17 text(into, '[');
18 renderType(into, t.ofType);
19 text(into, ']');
20 }
21 else {
22 text(into, t.name);
23 }
24}
25function renderTypeToString(t, useMarkdown) {
26 const into = [];
27 if (useMarkdown) {
28 text(into, '```graphql\n');
29 }
30 renderType(into, t);
31 if (useMarkdown) {
32 text(into, '\n```');
33 }
34 return into.join('');
35}
36const scalarTypesMap = {
37 Int: 'integer',
38 String: 'string',
39 Float: 'number',
40 ID: 'string',
41 Boolean: 'boolean',
42 DateTime: 'string',
43};
44function getJSONSchemaFromGraphQLType(type, options) {
45 var _a;
46 let required = false;
47 let definition = Object.create(null);
48 const definitions = Object.create(null);
49 if ('defaultValue' in type && type.defaultValue !== undefined) {
50 definition.default = type.defaultValue;
51 }
52 if (graphql_1.isEnumType(type)) {
53 definition.type = 'string';
54 definition.enum = type.getValues().map(val => val.name);
55 }
56 if (graphql_1.isScalarType(type)) {
57 definition.type = (_a = scalarTypesMap[type.name]) !== null && _a !== void 0 ? _a : 'any';
58 }
59 if (graphql_1.isListType(type)) {
60 definition.type = 'array';
61 const { definition: def, definitions: defs } = getJSONSchemaFromGraphQLType(type.ofType, options);
62 if (def.$ref) {
63 definition.items = { $ref: def.$ref };
64 }
65 else {
66 definition.items = def;
67 }
68 if (defs) {
69 Object.keys(defs).forEach(defName => {
70 definitions[defName] = defs[defName];
71 });
72 }
73 }
74 if (graphql_1.isNonNullType(type)) {
75 required = true;
76 const { definition: def, definitions: defs } = getJSONSchemaFromGraphQLType(type.ofType, options);
77 definition = def;
78 if (defs) {
79 Object.keys(defs).forEach(defName => {
80 definitions[defName] = defs[defName];
81 });
82 }
83 }
84 if (graphql_1.isInputObjectType(type)) {
85 definition.$ref = `#/definitions/${type.name}`;
86 const fields = type.getFields();
87 const fieldDef = {
88 type: 'object',
89 properties: {},
90 required: [],
91 };
92 if (type.description) {
93 fieldDef.description = type.description + `\n` + renderTypeToString(type);
94 if (options === null || options === void 0 ? void 0 : options.useMarkdownDescription) {
95 fieldDef.markdownDescription =
96 type.description + `\n` + renderTypeToString(type, true);
97 }
98 }
99 else {
100 fieldDef.description = renderTypeToString(type);
101 if (options === null || options === void 0 ? void 0 : options.useMarkdownDescription) {
102 fieldDef.markdownDescription = renderTypeToString(type, true);
103 }
104 }
105 Object.keys(fields).forEach(fieldName => {
106 const field = fields[fieldName];
107 const { required: fieldRequired, definition: typeDefinition, definitions: typeDefinitions, } = getJSONSchemaFromGraphQLType(field.type, options);
108 const { definition: fieldDefinition, } = getJSONSchemaFromGraphQLType(field, options);
109 fieldDef.properties[fieldName] = Object.assign(Object.assign({}, typeDefinition), fieldDefinition);
110 const renderedField = renderTypeToString(field.type);
111 fieldDef.properties[fieldName].description = field.description
112 ? field.description + '\n' + renderedField
113 : renderedField;
114 if (options === null || options === void 0 ? void 0 : options.useMarkdownDescription) {
115 const renderedFieldMarkdown = renderTypeToString(field.type, true);
116 fieldDef.properties[fieldName].markdownDescription = field.description
117 ? field.description + '\n' + renderedFieldMarkdown
118 : renderedFieldMarkdown;
119 }
120 if (fieldRequired) {
121 fieldDef.required.push(fieldName);
122 }
123 if (typeDefinitions) {
124 Object.keys(typeDefinitions).map(defName => {
125 definitions[defName] = typeDefinitions[defName];
126 });
127 }
128 });
129 definitions[type.name] = fieldDef;
130 }
131 if ('description' in type &&
132 !graphql_1.isScalarType(type) &&
133 type.description &&
134 !definition.description) {
135 definition.description = type.description + '\n' + renderTypeToString(type);
136 if (options === null || options === void 0 ? void 0 : options.useMarkdownDescription) {
137 definition.markdownDescription =
138 type.description + '\n' + renderTypeToString(type, true);
139 }
140 }
141 else {
142 definition.description = renderTypeToString(type);
143 if (options === null || options === void 0 ? void 0 : options.useMarkdownDescription) {
144 definition.markdownDescription = renderTypeToString(type, true);
145 }
146 }
147 return { required, definition, definitions };
148}
149function getVariablesJSONSchema(variableToType, options) {
150 const jsonSchema = {
151 $schema: 'https://json-schema.org/draft/2020-12/schema',
152 type: 'object',
153 properties: {},
154 required: [],
155 };
156 if (variableToType) {
157 Object.entries(variableToType).forEach(([variableName, type]) => {
158 var _a;
159 const { definition, required, definitions, } = getJSONSchemaFromGraphQLType(type, options);
160 jsonSchema.properties[variableName] = definition;
161 if (required) {
162 (_a = jsonSchema.required) === null || _a === void 0 ? void 0 : _a.push(variableName);
163 }
164 if (definitions) {
165 jsonSchema.definitions = Object.assign(Object.assign({}, jsonSchema === null || jsonSchema === void 0 ? void 0 : jsonSchema.definitions), definitions);
166 }
167 });
168 }
169 return jsonSchema;
170}
171exports.getVariablesJSONSchema = getVariablesJSONSchema;
172//# sourceMappingURL=getVariablesJSONSchema.js.map
\No newline at end of file