@omnigraph/openapi 1.0.0-alpha-3fc47d119.0 → 1.0.0-alpha-20230420181317-a95037648
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/cjs/getJSONSchemaOptionsFromOpenAPIOptions.js +471 -0
- package/cjs/index.js +9 -0
- package/cjs/loadGraphQLSchemaFromOpenAPI.js +29 -0
- package/cjs/package.json +1 -0
- package/cjs/types.js +0 -0
- package/cjs/utils.js +44 -0
- package/esm/getJSONSchemaOptionsFromOpenAPIOptions.js +467 -0
- package/esm/index.js +3 -0
- package/esm/loadGraphQLSchemaFromOpenAPI.js +23 -0
- package/esm/types.js +0 -0
- package/esm/utils.js +40 -0
- package/package.json +28 -21
- package/typings/getJSONSchemaOptionsFromOpenAPIOptions.d.cts +26 -0
- package/typings/getJSONSchemaOptionsFromOpenAPIOptions.d.ts +26 -0
- package/typings/index.d.cts +4 -0
- package/typings/index.d.ts +4 -0
- package/typings/loadGraphQLSchemaFromOpenAPI.d.cts +10 -0
- package/{loadGraphQLSchemaFromOpenAPI.d.ts → typings/loadGraphQLSchemaFromOpenAPI.d.ts} +3 -1
- package/typings/types.d.cts +10 -0
- package/{types.d.ts → typings/types.d.ts} +2 -2
- package/typings/utils.d.ts +1 -0
- package/bundle.d.ts +0 -8
- package/getJSONSchemaOptionsFromOpenAPIOptions.d.ts +0 -24
- package/index.d.ts +0 -4
- package/index.js +0 -355
- package/index.mjs +0 -345
- /package/{utils.d.ts → typings/utils.d.cts} +0 -0
|
@@ -0,0 +1,471 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getJSONSchemaOptionsFromOpenAPIOptions = void 0;
|
|
4
|
+
const graphql_1 = require("graphql");
|
|
5
|
+
const json_machete_1 = require("json-machete");
|
|
6
|
+
const cross_helpers_1 = require("@graphql-mesh/cross-helpers");
|
|
7
|
+
const string_interpolation_1 = require("@graphql-mesh/string-interpolation");
|
|
8
|
+
const utils_1 = require("@graphql-mesh/utils");
|
|
9
|
+
const utils_js_1 = require("./utils.js");
|
|
10
|
+
async function getJSONSchemaOptionsFromOpenAPIOptions(name, { source, fallbackFormat, cwd, fetch: fetchFn, endpoint, schemaHeaders, operationHeaders, queryParams = {}, selectQueryOrMutationField = [], logger = new utils_1.DefaultLogger('getJSONSchemaOptionsFromOpenAPIOptions'), }) {
|
|
11
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
|
|
12
|
+
const fieldTypeMap = {};
|
|
13
|
+
for (const { fieldName, type } of selectQueryOrMutationField) {
|
|
14
|
+
fieldTypeMap[fieldName] = type;
|
|
15
|
+
}
|
|
16
|
+
const schemaHeadersFactory = (0, string_interpolation_1.getInterpolatedHeadersFactory)(schemaHeaders);
|
|
17
|
+
logger === null || logger === void 0 ? void 0 : logger.debug(`Fetching OpenAPI Document from ${source}`);
|
|
18
|
+
let oasOrSwagger = typeof source === 'string'
|
|
19
|
+
? await (0, utils_1.readFileOrUrl)(source, {
|
|
20
|
+
cwd,
|
|
21
|
+
fallbackFormat,
|
|
22
|
+
headers: schemaHeadersFactory({ env: cross_helpers_1.process.env }),
|
|
23
|
+
fetch: fetchFn,
|
|
24
|
+
importFn: utils_1.defaultImportFn,
|
|
25
|
+
logger,
|
|
26
|
+
})
|
|
27
|
+
: source;
|
|
28
|
+
(0, json_machete_1.handleUntitledDefinitions)(oasOrSwagger);
|
|
29
|
+
oasOrSwagger = (await (0, json_machete_1.dereferenceObject)(oasOrSwagger));
|
|
30
|
+
const operations = [];
|
|
31
|
+
let baseOperationArgTypeMap;
|
|
32
|
+
if (!endpoint) {
|
|
33
|
+
if ('servers' in oasOrSwagger) {
|
|
34
|
+
const serverObj = oasOrSwagger.servers[0];
|
|
35
|
+
endpoint = serverObj.url.split('{').join('{args.');
|
|
36
|
+
if (serverObj.variables) {
|
|
37
|
+
for (const variableName in serverObj.variables) {
|
|
38
|
+
const variable = serverObj.variables[variableName];
|
|
39
|
+
if (!variable.type) {
|
|
40
|
+
variable.type = 'string';
|
|
41
|
+
}
|
|
42
|
+
baseOperationArgTypeMap = baseOperationArgTypeMap || {};
|
|
43
|
+
baseOperationArgTypeMap[variableName] = variable;
|
|
44
|
+
if (variable.default) {
|
|
45
|
+
endpoint = endpoint.replace(`{args.${variableName}}`, `{args.${variableName}:${variable.default}}`);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if ('schemes' in oasOrSwagger && oasOrSwagger.schemes.length > 0 && oasOrSwagger.host) {
|
|
51
|
+
endpoint = oasOrSwagger.schemes[0] + '://' + oasOrSwagger.host;
|
|
52
|
+
if ('basePath' in oasOrSwagger) {
|
|
53
|
+
endpoint += oasOrSwagger.basePath;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
const methodObjFieldMap = new WeakMap();
|
|
58
|
+
for (const relativePath in oasOrSwagger.paths) {
|
|
59
|
+
const pathObj = oasOrSwagger.paths[relativePath];
|
|
60
|
+
const pathParameters = pathObj.parameters;
|
|
61
|
+
for (const method in pathObj) {
|
|
62
|
+
if (method === 'parameters' ||
|
|
63
|
+
method === 'summary' ||
|
|
64
|
+
method === 'description' ||
|
|
65
|
+
method === 'servers') {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const methodObj = pathObj[method];
|
|
69
|
+
const operationConfig = {
|
|
70
|
+
method: method.toUpperCase(),
|
|
71
|
+
path: relativePath,
|
|
72
|
+
type: method.toUpperCase() === 'GET' ? 'query' : 'mutation',
|
|
73
|
+
field: methodObj.operationId && (0, utils_1.sanitizeNameForGraphQL)(methodObj.operationId),
|
|
74
|
+
description: methodObj.description || methodObj.summary,
|
|
75
|
+
schemaHeaders,
|
|
76
|
+
operationHeaders,
|
|
77
|
+
responseByStatusCode: {},
|
|
78
|
+
...(baseOperationArgTypeMap
|
|
79
|
+
? {
|
|
80
|
+
argTypeMap: {
|
|
81
|
+
...baseOperationArgTypeMap,
|
|
82
|
+
},
|
|
83
|
+
}
|
|
84
|
+
: {}),
|
|
85
|
+
};
|
|
86
|
+
operations.push(operationConfig);
|
|
87
|
+
methodObjFieldMap.set(methodObj, operationConfig);
|
|
88
|
+
let allParams;
|
|
89
|
+
if (methodObj.parameters && Array.isArray(methodObj.parameters)) {
|
|
90
|
+
allParams = [...(pathParameters || []), ...methodObj.parameters];
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
allParams = {
|
|
94
|
+
...(pathParameters || {}),
|
|
95
|
+
...(methodObj.parameters || {}),
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
for (const paramObjIndex in allParams) {
|
|
99
|
+
const paramObj = allParams[paramObjIndex];
|
|
100
|
+
const argName = (0, utils_1.sanitizeNameForGraphQL)(paramObj.name);
|
|
101
|
+
const operationArgTypeMap = (operationConfig.argTypeMap =
|
|
102
|
+
operationConfig.argTypeMap || {});
|
|
103
|
+
switch (paramObj.in) {
|
|
104
|
+
case 'query':
|
|
105
|
+
operationConfig.queryParamArgMap = operationConfig.queryParamArgMap || {};
|
|
106
|
+
operationConfig.queryParamArgMap[paramObj.name] = argName;
|
|
107
|
+
if (paramObj.name in queryParams) {
|
|
108
|
+
paramObj.required = false;
|
|
109
|
+
if (!((_a = paramObj.schema) === null || _a === void 0 ? void 0 : _a.default)) {
|
|
110
|
+
paramObj.schema = paramObj.schema || {
|
|
111
|
+
type: 'string',
|
|
112
|
+
};
|
|
113
|
+
paramObj.schema.default = queryParams[paramObj.name];
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if ('explode' in paramObj) {
|
|
117
|
+
operationConfig.queryStringOptionsByParam =
|
|
118
|
+
operationConfig.queryStringOptionsByParam || {};
|
|
119
|
+
operationConfig.queryStringOptionsByParam[paramObj.name] =
|
|
120
|
+
operationConfig.queryStringOptionsByParam[paramObj.name] || {};
|
|
121
|
+
if (paramObj.explode) {
|
|
122
|
+
operationConfig.queryStringOptionsByParam[paramObj.name].arrayFormat = 'repeat';
|
|
123
|
+
operationConfig.queryStringOptionsByParam[paramObj.name].destructObject = true;
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
if (paramObj.style === 'form') {
|
|
127
|
+
operationConfig.queryStringOptionsByParam[paramObj.name].arrayFormat = 'comma';
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
logger.warn(`Other styles including ${paramObj.style} of query parameters are not supported yet.`);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
break;
|
|
135
|
+
case 'path': {
|
|
136
|
+
// If it is in the path, let JSON Schema handler put it
|
|
137
|
+
operationConfig.path = operationConfig.path.replace(`{${paramObj.name}}`, `{args.${argName}}`);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case 'header': {
|
|
141
|
+
operationConfig.headers = operationConfig.headers || {};
|
|
142
|
+
if (typeof operationHeaders === 'object' && operationHeaders[paramObj.name]) {
|
|
143
|
+
paramObj.required = false;
|
|
144
|
+
const valueFromGlobal = operationHeaders[paramObj.name];
|
|
145
|
+
if (!valueFromGlobal.includes('{')) {
|
|
146
|
+
if (paramObj.schema) {
|
|
147
|
+
paramObj.schema.default = valueFromGlobal;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
else {
|
|
151
|
+
if ((_b = paramObj.schema) === null || _b === void 0 ? void 0 : _b.default) {
|
|
152
|
+
delete paramObj.schema.default;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (typeof operationHeaders === 'function') {
|
|
157
|
+
paramObj.required = false;
|
|
158
|
+
if ((_c = paramObj.schema) === null || _c === void 0 ? void 0 : _c.default) {
|
|
159
|
+
delete paramObj.schema.default;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
let defaultValueSuffix = '';
|
|
163
|
+
if ((_d = paramObj.schema) === null || _d === void 0 ? void 0 : _d.default) {
|
|
164
|
+
defaultValueSuffix = `:${paramObj.schema.default}`;
|
|
165
|
+
}
|
|
166
|
+
operationConfig.headers[paramObj.name] = `{args.${argName}${defaultValueSuffix}}`;
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
case 'cookie': {
|
|
170
|
+
operationConfig.headers = operationConfig.headers || {};
|
|
171
|
+
operationConfig.headers.cookie = operationConfig.headers.cookie || '';
|
|
172
|
+
const cookieParams = operationConfig.headers.cookie.split(' ').filter(c => !!c);
|
|
173
|
+
cookieParams.push(`${paramObj.name}={args.${argName}};`);
|
|
174
|
+
operationConfig.headers.cookie = `${cookieParams.join(' ')}`;
|
|
175
|
+
break;
|
|
176
|
+
}
|
|
177
|
+
case 'body':
|
|
178
|
+
if (paramObj.schema && Object.keys(paramObj.schema).length > 0) {
|
|
179
|
+
operationConfig.requestSchema = paramObj.schema;
|
|
180
|
+
}
|
|
181
|
+
if (paramObj.example) {
|
|
182
|
+
operationConfig.requestSample = paramObj.example;
|
|
183
|
+
}
|
|
184
|
+
if (paramObj.examples) {
|
|
185
|
+
operationConfig.requestSample = Object.values(paramObj.examples)[0];
|
|
186
|
+
}
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
operationArgTypeMap[argName] =
|
|
190
|
+
paramObj.schema || ((_f = (_e = paramObj.content) === null || _e === void 0 ? void 0 : _e['application/json']) === null || _f === void 0 ? void 0 : _f.schema) || paramObj;
|
|
191
|
+
if (!operationArgTypeMap[argName].title) {
|
|
192
|
+
operationArgTypeMap[argName].name = paramObj.name;
|
|
193
|
+
}
|
|
194
|
+
if (!operationArgTypeMap[argName].description) {
|
|
195
|
+
operationArgTypeMap[argName].description = paramObj.description;
|
|
196
|
+
}
|
|
197
|
+
if (paramObj.required) {
|
|
198
|
+
operationArgTypeMap[argName].nullable = false;
|
|
199
|
+
}
|
|
200
|
+
if (!('type' in paramObj) &&
|
|
201
|
+
!paramObj.schema &&
|
|
202
|
+
!paramObj.content &&
|
|
203
|
+
!paramObj.example &&
|
|
204
|
+
!paramObj.examples) {
|
|
205
|
+
operationArgTypeMap[argName].type = 'string';
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
if ('requestBody' in methodObj) {
|
|
209
|
+
const requestBodyObj = methodObj.requestBody;
|
|
210
|
+
if ('content' in requestBodyObj) {
|
|
211
|
+
const contentKey = Object.keys(requestBodyObj.content)[0];
|
|
212
|
+
const contentSchema = (_g = requestBodyObj.content[contentKey]) === null || _g === void 0 ? void 0 : _g.schema;
|
|
213
|
+
if (contentSchema && Object.keys(contentSchema).length > 0) {
|
|
214
|
+
operationConfig.requestSchema = contentSchema;
|
|
215
|
+
}
|
|
216
|
+
const examplesObj = (_h = requestBodyObj.content[contentKey]) === null || _h === void 0 ? void 0 : _h.examples;
|
|
217
|
+
if (examplesObj) {
|
|
218
|
+
operationConfig.requestSample = Object.values(examplesObj)[0];
|
|
219
|
+
}
|
|
220
|
+
if (!((_j = operationConfig.headers) === null || _j === void 0 ? void 0 : _j['Content-Type']) && typeof contentKey === 'string') {
|
|
221
|
+
operationConfig.headers = operationConfig.headers || {};
|
|
222
|
+
operationConfig.headers['Content-Type'] = contentKey;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
const responseByStatusCode = operationConfig.responseByStatusCode;
|
|
227
|
+
// Handling multiple response types
|
|
228
|
+
for (const responseKey in methodObj.responses) {
|
|
229
|
+
const responseObj = methodObj.responses[responseKey];
|
|
230
|
+
let schemaObj;
|
|
231
|
+
if ('consumes' in methodObj) {
|
|
232
|
+
operationConfig.headers = operationConfig.headers || {};
|
|
233
|
+
operationConfig.headers['Content-Type'] = methodObj.consumes.join(', ');
|
|
234
|
+
}
|
|
235
|
+
if ('produces' in methodObj) {
|
|
236
|
+
operationConfig.headers = operationConfig.headers || {};
|
|
237
|
+
operationConfig.headers.Accept = methodObj.produces.join(', ');
|
|
238
|
+
}
|
|
239
|
+
if ('content' in responseObj) {
|
|
240
|
+
const responseObjForStatusCode = {
|
|
241
|
+
oneOf: [],
|
|
242
|
+
};
|
|
243
|
+
let allMimeTypes = [];
|
|
244
|
+
if (typeof operationHeaders === 'object') {
|
|
245
|
+
const acceptFromOperationHeader = operationHeaders.accept || operationHeaders.Accept;
|
|
246
|
+
if (acceptFromOperationHeader) {
|
|
247
|
+
allMimeTypes = [acceptFromOperationHeader];
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
if (allMimeTypes.length === 0) {
|
|
251
|
+
allMimeTypes = Object.keys(responseObj.content);
|
|
252
|
+
}
|
|
253
|
+
const jsonLikeMimeTypes = allMimeTypes.filter(c => c !== '*/*' && c.toString().includes('json'));
|
|
254
|
+
const mimeTypes = jsonLikeMimeTypes.length > 0 ? jsonLikeMimeTypes : allMimeTypes;
|
|
255
|
+
// If we have a better accept header, overwrite User's choice
|
|
256
|
+
if ((!((_k = operationConfig.headers) === null || _k === void 0 ? void 0 : _k.accept) && !((_l = operationConfig.headers) === null || _l === void 0 ? void 0 : _l.Accept)) ||
|
|
257
|
+
mimeTypes.length === 1) {
|
|
258
|
+
operationConfig.headers = operationConfig.headers || {};
|
|
259
|
+
if (operationConfig.headers.Accept) {
|
|
260
|
+
delete operationConfig.headers.Accept;
|
|
261
|
+
}
|
|
262
|
+
operationConfig.headers.accept =
|
|
263
|
+
jsonLikeMimeTypes.length > 0
|
|
264
|
+
? jsonLikeMimeTypes.join(',')
|
|
265
|
+
: allMimeTypes[0].toString();
|
|
266
|
+
}
|
|
267
|
+
for (const contentKey in responseObj.content) {
|
|
268
|
+
if (!mimeTypes.includes(contentKey)) {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
schemaObj = responseObj.content[contentKey].schema;
|
|
272
|
+
if (schemaObj && Object.keys(schemaObj).length > 0) {
|
|
273
|
+
responseObjForStatusCode.oneOf.push(schemaObj);
|
|
274
|
+
}
|
|
275
|
+
else if (contentKey.toString().startsWith('text')) {
|
|
276
|
+
responseObjForStatusCode.oneOf.push({ type: 'string' });
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
const examplesObj = responseObj.content[contentKey].examples;
|
|
280
|
+
if (examplesObj) {
|
|
281
|
+
let examples = Object.values(examplesObj);
|
|
282
|
+
if (contentKey.includes('json')) {
|
|
283
|
+
examples = examples.map(example => {
|
|
284
|
+
if (typeof example === 'string') {
|
|
285
|
+
return JSON.parse(example);
|
|
286
|
+
}
|
|
287
|
+
return example;
|
|
288
|
+
});
|
|
289
|
+
}
|
|
290
|
+
responseObjForStatusCode.oneOf.push({
|
|
291
|
+
examples,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
let example = responseObj.content[contentKey].example;
|
|
295
|
+
if (example) {
|
|
296
|
+
if (typeof example === 'string' && contentKey.includes('json')) {
|
|
297
|
+
example = JSON.parse(example);
|
|
298
|
+
}
|
|
299
|
+
responseObjForStatusCode.oneOf.push({
|
|
300
|
+
examples: [example],
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (responseObjForStatusCode.oneOf.length === 1) {
|
|
306
|
+
responseByStatusCode[responseKey] = responseByStatusCode[responseKey] || {};
|
|
307
|
+
responseByStatusCode[responseKey].responseSchema = responseObjForStatusCode.oneOf[0];
|
|
308
|
+
}
|
|
309
|
+
else if (responseObjForStatusCode.oneOf.length > 1) {
|
|
310
|
+
responseByStatusCode[responseKey] = responseByStatusCode[responseKey] || {};
|
|
311
|
+
responseByStatusCode[responseKey].responseSchema = responseObjForStatusCode;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
else if ('schema' in responseObj) {
|
|
315
|
+
schemaObj = responseObj.schema;
|
|
316
|
+
if (schemaObj && Object.keys(schemaObj).length > 0) {
|
|
317
|
+
responseByStatusCode[responseKey] = responseByStatusCode[responseKey] || {};
|
|
318
|
+
responseByStatusCode[responseKey].responseSchema = schemaObj;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
else if ('examples' in responseObj) {
|
|
322
|
+
const examples = Object.values(responseObj.examples);
|
|
323
|
+
responseByStatusCode[responseKey] = responseByStatusCode[responseKey] || {};
|
|
324
|
+
let example = examples[0];
|
|
325
|
+
if (typeof example === 'string') {
|
|
326
|
+
try {
|
|
327
|
+
// Parse if possible
|
|
328
|
+
example = JSON.parse(example);
|
|
329
|
+
}
|
|
330
|
+
catch (e) {
|
|
331
|
+
// Do nothing
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
responseByStatusCode[responseKey].responseSample = example;
|
|
335
|
+
}
|
|
336
|
+
else if (responseKey.toString() === '204') {
|
|
337
|
+
responseByStatusCode[responseKey] = responseByStatusCode[responseKey] || {};
|
|
338
|
+
responseByStatusCode[responseKey].responseSchema = {
|
|
339
|
+
type: 'null',
|
|
340
|
+
description: responseObj.description,
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
if ('links' in responseObj) {
|
|
344
|
+
const dereferencedLinkObj = await (0, json_machete_1.dereferenceObject)({
|
|
345
|
+
links: responseObj.links,
|
|
346
|
+
}, {
|
|
347
|
+
cwd,
|
|
348
|
+
root: oasOrSwagger,
|
|
349
|
+
fetchFn,
|
|
350
|
+
logger,
|
|
351
|
+
headers: schemaHeaders,
|
|
352
|
+
});
|
|
353
|
+
responseByStatusCode[responseKey].links = responseByStatusCode[responseKey].links || {};
|
|
354
|
+
for (const linkName in dereferencedLinkObj.links) {
|
|
355
|
+
const linkObj = responseObj.links[linkName];
|
|
356
|
+
let args;
|
|
357
|
+
if (linkObj.parameters) {
|
|
358
|
+
args = {};
|
|
359
|
+
for (const parameterName in linkObj.parameters) {
|
|
360
|
+
const parameterExp = linkObj.parameters[parameterName];
|
|
361
|
+
const sanitizedParamName = (0, utils_1.sanitizeNameForGraphQL)(parameterName);
|
|
362
|
+
if (typeof parameterExp === 'string') {
|
|
363
|
+
args[sanitizedParamName] = parameterExp.startsWith('$')
|
|
364
|
+
? `{root.${parameterExp}}`
|
|
365
|
+
: parameterExp.split('$').join('root.$');
|
|
366
|
+
}
|
|
367
|
+
else {
|
|
368
|
+
args[sanitizedParamName] = parameterExp;
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
}
|
|
372
|
+
const sanitizedLinkName = (0, utils_1.sanitizeNameForGraphQL)(linkName);
|
|
373
|
+
if ('operationRef' in linkObj) {
|
|
374
|
+
const [externalPath, ref] = linkObj.operationRef.split('#');
|
|
375
|
+
if (externalPath) {
|
|
376
|
+
logger.debug(`Skipping external operation reference ${linkObj.operationRef}\n Use additionalTypeDefs and additionalResolvers instead.`);
|
|
377
|
+
}
|
|
378
|
+
else {
|
|
379
|
+
const actualOperation = (0, json_machete_1.resolvePath)(ref, oasOrSwagger);
|
|
380
|
+
responseByStatusCode[responseKey].links[sanitizedLinkName] = {
|
|
381
|
+
get fieldName() {
|
|
382
|
+
const linkOperationConfig = methodObjFieldMap.get(actualOperation);
|
|
383
|
+
return linkOperationConfig.field;
|
|
384
|
+
},
|
|
385
|
+
args,
|
|
386
|
+
description: linkObj.description,
|
|
387
|
+
};
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
else if ('operationId' in linkObj) {
|
|
391
|
+
responseByStatusCode[responseKey].links[sanitizedLinkName] = {
|
|
392
|
+
fieldName: (0, utils_1.sanitizeNameForGraphQL)(linkObj.operationId),
|
|
393
|
+
args,
|
|
394
|
+
description: linkObj.description,
|
|
395
|
+
};
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
if (!operationConfig.field) {
|
|
400
|
+
methodObj.operationId = (0, utils_js_1.getFieldNameFromPath)(relativePath, method, schemaObj === null || schemaObj === void 0 ? void 0 : schemaObj.$resolvedRef);
|
|
401
|
+
// Operation ID might not be avaiable so let's generate field name from path and response type schema
|
|
402
|
+
operationConfig.field = (0, utils_1.sanitizeNameForGraphQL)(methodObj.operationId);
|
|
403
|
+
}
|
|
404
|
+
// Give a better name to the request input object
|
|
405
|
+
if (typeof operationConfig.requestSchema === 'object' &&
|
|
406
|
+
!operationConfig.requestSchema.title) {
|
|
407
|
+
operationConfig.requestSchema.title = operationConfig.field + '_request';
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
if ('callbacks' in methodObj) {
|
|
411
|
+
for (const callbackKey in methodObj.callbacks) {
|
|
412
|
+
const callbackObj = methodObj.callbacks[callbackKey];
|
|
413
|
+
for (const callbackUrlRefKey in callbackObj) {
|
|
414
|
+
if (callbackUrlRefKey.startsWith('$')) {
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
const pubsubTopicSuffix = callbackUrlRefKey
|
|
418
|
+
.split('$request.query')
|
|
419
|
+
.join('args')
|
|
420
|
+
.split('$request.body#/')
|
|
421
|
+
.join('args.')
|
|
422
|
+
.split('$response.body#/')
|
|
423
|
+
.join('args.');
|
|
424
|
+
const callbackOperationConfig = {
|
|
425
|
+
type: graphql_1.OperationTypeNode.SUBSCRIPTION,
|
|
426
|
+
field: '',
|
|
427
|
+
pubsubTopic: '',
|
|
428
|
+
};
|
|
429
|
+
const callbackUrlObj = callbackObj[callbackUrlRefKey];
|
|
430
|
+
for (const method in callbackUrlObj) {
|
|
431
|
+
const callbackOperation = callbackUrlObj[method];
|
|
432
|
+
callbackOperationConfig.pubsubTopic = `webhook:${method}:${pubsubTopicSuffix}`;
|
|
433
|
+
callbackOperationConfig.field = callbackOperation.operationId;
|
|
434
|
+
callbackOperationConfig.description =
|
|
435
|
+
callbackOperation.description || callbackOperation.summary;
|
|
436
|
+
const requestBodyContents = (_m = callbackOperation.requestBody) === null || _m === void 0 ? void 0 : _m.content;
|
|
437
|
+
if (requestBodyContents) {
|
|
438
|
+
callbackOperationConfig.responseSchema = requestBodyContents[Object.keys(requestBodyContents)[0]].schema;
|
|
439
|
+
}
|
|
440
|
+
const responses = callbackOperation.responses;
|
|
441
|
+
if (responses) {
|
|
442
|
+
const response = responses[Object.keys(responses)[0]];
|
|
443
|
+
if (response) {
|
|
444
|
+
const responseContents = response.content;
|
|
445
|
+
if (responseContents) {
|
|
446
|
+
callbackOperationConfig.requestSchema = responseContents[Object.keys(responseContents)[0]].schema;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
callbackOperationConfig.field =
|
|
452
|
+
callbackOperationConfig.field || (0, utils_1.sanitizeNameForGraphQL)(callbackKey);
|
|
453
|
+
operations.push(callbackOperationConfig);
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
if (fieldTypeMap[operationConfig.field]) {
|
|
458
|
+
operationConfig.type = fieldTypeMap[operationConfig.field];
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
return {
|
|
463
|
+
operations,
|
|
464
|
+
endpoint,
|
|
465
|
+
cwd,
|
|
466
|
+
fetch: fetchFn,
|
|
467
|
+
schemaHeaders,
|
|
468
|
+
operationHeaders,
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
exports.getJSONSchemaOptionsFromOpenAPIOptions = getJSONSchemaOptionsFromOpenAPIOptions;
|
package/cjs/index.js
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getJSONSchemaOptionsFromOpenAPIOptions = exports.default = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
var loadGraphQLSchemaFromOpenAPI_js_1 = require("./loadGraphQLSchemaFromOpenAPI.js");
|
|
6
|
+
Object.defineProperty(exports, "default", { enumerable: true, get: function () { return loadGraphQLSchemaFromOpenAPI_js_1.loadGraphQLSchemaFromOpenAPI; } });
|
|
7
|
+
tslib_1.__exportStar(require("./loadGraphQLSchemaFromOpenAPI.js"), exports);
|
|
8
|
+
var getJSONSchemaOptionsFromOpenAPIOptions_js_1 = require("./getJSONSchemaOptionsFromOpenAPIOptions.js");
|
|
9
|
+
Object.defineProperty(exports, "getJSONSchemaOptionsFromOpenAPIOptions", { enumerable: true, get: function () { return getJSONSchemaOptionsFromOpenAPIOptions_js_1.getJSONSchemaOptionsFromOpenAPIOptions; } });
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.processDirectives = exports.loadNonExecutableGraphQLSchemaFromOpenAPI = exports.loadGraphQLSchemaFromOpenAPI = void 0;
|
|
4
|
+
const json_schema_1 = require("@omnigraph/json-schema");
|
|
5
|
+
const getJSONSchemaOptionsFromOpenAPIOptions_js_1 = require("./getJSONSchemaOptionsFromOpenAPIOptions.js");
|
|
6
|
+
/**
|
|
7
|
+
* Creates a local GraphQLSchema instance from a OpenAPI Document.
|
|
8
|
+
* Everytime this function is called, the OpenAPI file and its dependencies will be resolved on runtime.
|
|
9
|
+
* If you want to avoid this, use `createBundle` function to create a bundle once and save it to a storage
|
|
10
|
+
* then load it with `loadGraphQLSchemaFromBundle`.
|
|
11
|
+
*/
|
|
12
|
+
async function loadGraphQLSchemaFromOpenAPI(name, options) {
|
|
13
|
+
const extraJSONSchemaOptions = await (0, getJSONSchemaOptionsFromOpenAPIOptions_js_1.getJSONSchemaOptionsFromOpenAPIOptions)(name, options);
|
|
14
|
+
return (0, json_schema_1.loadGraphQLSchemaFromJSONSchemas)(name, {
|
|
15
|
+
...options,
|
|
16
|
+
...extraJSONSchemaOptions,
|
|
17
|
+
});
|
|
18
|
+
}
|
|
19
|
+
exports.loadGraphQLSchemaFromOpenAPI = loadGraphQLSchemaFromOpenAPI;
|
|
20
|
+
async function loadNonExecutableGraphQLSchemaFromOpenAPI(name, options) {
|
|
21
|
+
const extraJSONSchemaOptions = await (0, getJSONSchemaOptionsFromOpenAPIOptions_js_1.getJSONSchemaOptionsFromOpenAPIOptions)(name, options);
|
|
22
|
+
return (0, json_schema_1.loadNonExecutableGraphQLSchemaFromJSONSchemas)(name, {
|
|
23
|
+
...options,
|
|
24
|
+
...extraJSONSchemaOptions,
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
exports.loadNonExecutableGraphQLSchemaFromOpenAPI = loadNonExecutableGraphQLSchemaFromOpenAPI;
|
|
28
|
+
var json_schema_2 = require("@omnigraph/json-schema");
|
|
29
|
+
Object.defineProperty(exports, "processDirectives", { enumerable: true, get: function () { return json_schema_2.processDirectives; } });
|
package/cjs/package.json
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
package/cjs/types.js
ADDED
|
File without changes
|
package/cjs/utils.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getFieldNameFromPath = void 0;
|
|
4
|
+
const change_case_1 = require("change-case");
|
|
5
|
+
function getFieldNameFromPath(path, method, responseTypeSchemaRef) {
|
|
6
|
+
// Replace identifiers with "by"
|
|
7
|
+
path = path.split('{').join('by_').split('}').join('');
|
|
8
|
+
const [actualPartsStr, allQueryPartsStr] = path.split('?');
|
|
9
|
+
const actualParts = actualPartsStr.split('/').filter(Boolean);
|
|
10
|
+
let fieldNameWithoutMethod = actualParts.join('_');
|
|
11
|
+
// If path doesn't give any field name without identifiers, we can use the return type with HTTP Method name
|
|
12
|
+
if ((!fieldNameWithoutMethod || fieldNameWithoutMethod.startsWith('by')) &&
|
|
13
|
+
responseTypeSchemaRef) {
|
|
14
|
+
const refArr = responseTypeSchemaRef.split('/');
|
|
15
|
+
// lowercase looks better in the schema
|
|
16
|
+
const prefix = (0, change_case_1.camelCase)(refArr[refArr.length - 1]);
|
|
17
|
+
if (fieldNameWithoutMethod) {
|
|
18
|
+
fieldNameWithoutMethod = prefix + '_' + fieldNameWithoutMethod;
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
fieldNameWithoutMethod = prefix;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (fieldNameWithoutMethod.startsWith('by_')) {
|
|
25
|
+
fieldNameWithoutMethod = fieldNameWithoutMethod.replace('by_', '');
|
|
26
|
+
}
|
|
27
|
+
if (allQueryPartsStr) {
|
|
28
|
+
const queryParts = allQueryPartsStr.split('&');
|
|
29
|
+
for (const queryPart of queryParts) {
|
|
30
|
+
const [queryName] = queryPart.split('=');
|
|
31
|
+
fieldNameWithoutMethod += '_' + 'by' + '_' + queryName;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// get_ doesn't look good in field names
|
|
35
|
+
const methodPrefix = method.toLowerCase();
|
|
36
|
+
if (methodPrefix === 'get') {
|
|
37
|
+
return fieldNameWithoutMethod;
|
|
38
|
+
}
|
|
39
|
+
if (fieldNameWithoutMethod) {
|
|
40
|
+
return methodPrefix + '_' + fieldNameWithoutMethod;
|
|
41
|
+
}
|
|
42
|
+
return methodPrefix;
|
|
43
|
+
}
|
|
44
|
+
exports.getFieldNameFromPath = getFieldNameFromPath;
|