-
Notifications
You must be signed in to change notification settings - Fork 207
/
index.js
306 lines (306 loc) · 12.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
"use strict";
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: oasgraph
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const graphql_1 = require("graphql");
// Imports:
const schema_builder_1 = require("./schema_builder");
const resolver_builder_1 = require("./resolver_builder");
const GraphQLTools = require("./graphql_tools");
const preprocessor_1 = require("./preprocessor");
const Oas3Tools = require("./oas_3_tools");
const auth_builder_1 = require("./auth_builder");
const debug_1 = require("debug");
const utils_1 = require("./utils");
const log = debug_1.default('translation');
/**
* Creates a GraphQL interface from the given OpenAPI Specification (2 or 3).
*/
function createGraphQlSchema(spec, options) {
return __awaiter(this, void 0, void 0, function* () {
// deal with option defaults:
// @ts-ignore
if (typeof options === 'undefined')
options = {};
options.strict = typeof options.strict === 'boolean'
? options.strict
: false;
options.addSubOperations = typeof options.addSubOperations === 'boolean'
? options.addSubOperations
: false;
options.viewer = typeof options.viewer === 'boolean'
? options.viewer
: true;
options.sendOAuthTokenInQuery = typeof options.sendOAuthTokenInQuery === 'boolean'
? options.sendOAuthTokenInQuery
: false;
options.fillEmptyResponses = typeof options.fillEmptyResponses === 'boolean'
? options.fillEmptyResponses
: false;
options.operationIdFieldNames = typeof options.operationIdFieldNames === 'boolean'
? options.operationIdFieldNames
: false;
options.report = {
warnings: [],
numOps: 0,
numOpsQuery: 0,
numOpsMutation: 0,
numQueriesCreated: 0,
numMutationsCreated: 0
};
/**
* Check if the spec is a valid OAS 3.0.x
* If the spec is OAS 2.0, attempt to translate it into 3.0.x, then try to
* translate the spec into a GraphQL schema
*/
let oas = yield Oas3Tools.getValidOAS3(spec);
let { schema, report } = yield translateOpenApiToGraphQL(oas, options);
return {
schema,
report
};
});
}
exports.createGraphQlSchema = createGraphQlSchema;
/**
* Creates a GraphQL interface from the given OpenAPI Specification 3.0.x
*/
function translateOpenApiToGraphQL(oas, { strict, headers, qs, viewer, tokenJSONpath, addSubOperations, sendOAuthTokenInQuery, report, fillEmptyResponses, baseUrl, operationIdFieldNames }) {
return __awaiter(this, void 0, void 0, function* () {
let options = {
headers,
qs,
viewer,
tokenJSONpath,
strict,
addSubOperations,
sendOAuthTokenInQuery,
report,
fillEmptyResponses,
baseUrl,
operationIdFieldNames
};
log(`Options: ${JSON.stringify(options)}`);
/**
* Extract information from the OAS and put it inside a data structure that
* is easier for OASGraph to use
*/
let data = preprocessor_1.preprocessOas(oas, options);
/**
* Create GraphQL fields for every operation and structure them based on their
* characteristics (query vs. mutation, auth vs. non-auth).
*/
let queryFields = {};
let mutationFields = {};
let authQueryFields = {};
let authMutationFields = {};
Object.entries(data.operations)
// Start with endpoints that DO contain links OR that DO contain sub
// operations, so that built-up GraphQL object types contain these links
// when they are re-used.
.sort(([op1Id, op1], [op2Id, op2]) => sortByHasLinksOrSubOps(op1, op2))
.forEach(([operationId, operation]) => {
log(`Process operation "${operationId}"...`);
let field = getFieldForOperation(operation, data, oas, options.baseUrl);
if (!operation.isMutation) {
let fieldName = Oas3Tools.uncapitalize(operation.responseDefinition.otName);
if (operation.inViewer) {
for (let securityRequirement of operation.securityRequirements) {
if (typeof authQueryFields[securityRequirement] !== 'object') {
authQueryFields[securityRequirement] = {};
}
// Avoid overwriting fields that return the same data:
if (fieldName in authQueryFields[securityRequirement] ||
operationIdFieldNames) {
fieldName = Oas3Tools.beautifyAndStore(operationId, data.saneMap);
}
if (fieldName in authQueryFields[securityRequirement]) {
utils_1.handleWarning({
typeKey: 'DUPLICATE_FIELD_NAME',
culprit: fieldName,
data,
log
});
}
authQueryFields[securityRequirement][fieldName] = field;
}
}
else {
// Avoid overwriting fields that return the same data:
if (fieldName in queryFields ||
operationIdFieldNames) {
fieldName = Oas3Tools.beautifyAndStore(operationId, data.saneMap);
}
if (fieldName in queryFields) {
utils_1.handleWarning({
typeKey: 'DUPLICATE_FIELD_NAME',
culprit: fieldName,
data,
log
});
}
queryFields[fieldName] = field;
}
}
else {
// Use operationId to avoid problems differentiating operations with the
// same path but differnet methods
let saneFieldName = Oas3Tools.beautifyAndStore(operationId, data.saneMap);
if (operation.inViewer) {
for (let securityRequirement of operation.securityRequirements) {
if (typeof authMutationFields[securityRequirement] !== 'object') {
authMutationFields[securityRequirement] = {};
}
if (saneFieldName in authMutationFields[securityRequirement]) {
utils_1.handleWarning({
typeKey: 'DUPLICATE_FIELD_NAME',
culprit: saneFieldName,
data,
log
});
}
authMutationFields[securityRequirement][saneFieldName] = field;
}
}
else {
if (saneFieldName in mutationFields) {
utils_1.handleWarning({
typeKey: 'DUPLICATE_FIELD_NAME',
culprit: saneFieldName,
data,
log
});
}
mutationFields[saneFieldName] = field;
}
}
});
// Sorting fields
queryFields = utils_1.sortObject(queryFields);
mutationFields = utils_1.sortObject(mutationFields);
authQueryFields = utils_1.sortObject(authQueryFields);
Object.keys(authQueryFields).forEach((key) => {
authQueryFields[key] = utils_1.sortObject(authQueryFields[key]);
});
authMutationFields = utils_1.sortObject(authMutationFields);
Object.keys(authMutationFields).forEach((key) => {
authMutationFields[key] = utils_1.sortObject(authMutationFields[key]);
});
/**
* Count created queries / mutations
*/
options.report.numQueriesCreated =
Object.keys(queryFields).length +
Object.keys(authQueryFields).reduce((sum, key) => {
return sum + Object.keys(authQueryFields[key]).length;
}, 0);
options.report.numMutationsCreated =
Object.keys(mutationFields).length +
Object.keys(authMutationFields).reduce((sum, key) => {
return sum + Object.keys(authMutationFields[key]).length;
}, 0);
/**
* Organize created queries / mutations into viewer objects.
*/
if (Object.keys(authQueryFields).length > 0) {
Object.assign(queryFields, auth_builder_1.createAndLoadViewer(authQueryFields, data, oas, false));
}
if (Object.keys(authMutationFields).length > 0) {
Object.assign(mutationFields, auth_builder_1.createAndLoadViewer(authMutationFields, data, oas, true));
}
/**
* Build up the schema
*/
const schemaConfig = {
query: Object.keys(queryFields).length > 0
? new graphql_1.GraphQLObjectType({
name: 'Query',
description: 'The start of any query',
fields: queryFields
})
: GraphQLTools.getEmptyObjectType('query'),
mutation: Object.keys(mutationFields).length > 0
? new graphql_1.GraphQLObjectType({
name: 'Mutation',
description: 'The start of any mutation',
fields: mutationFields
})
: null
};
// Fill in yet undefined Object Types to avoid GraphQLSchema from breaking.
// The reason: once creating the schema, the 'fields' thunks will resolve
// and if a field references an undefined Object Types, GraphQL will throw.
Object.entries(data.operations).forEach(([opId, operation]) => {
if (typeof operation.responseDefinition.ot === 'undefined') {
operation.responseDefinition.ot = GraphQLTools
.getEmptyObjectType(operation.responseDefinition.otName);
}
});
const schema = new graphql_1.GraphQLSchema(schemaConfig);
return { schema, report: options.report };
});
}
/**
* Helper function for sorting operations based on them having links or sub-
* operations.
*/
function sortByHasLinksOrSubOps(op1, op2) {
const hasOp1 = Object.keys(op1.links).length > 0 ||
(Array.isArray(op1.subOps) && op1.subOps.length > 0);
const hasOp2 = Object.keys(op2.links).length > 0 ||
(Array.isArray(op2.subOps) && op2.subOps.length > 0);
return (hasOp1 === hasOp2) ? 0 : hasOp1 ? -1 : 1; // hasOp1 = true => -1 = first
}
/**
* Creates the field object for the given operation.
*/
function getFieldForOperation(operation, data, oas, baseUrl) {
// create GraphQL Type for response:
let type = schema_builder_1.getGraphQLType({
name: operation.responseDefinition.preferredName,
schema: operation.responseDefinition.schema,
data,
operation,
oas
});
// create resolve function:
let payloadSchemaName = operation.payloadDefinition
? operation.payloadDefinition.iotName
: null;
let payloadSchema = operation.payloadDefinition
? operation.payloadDefinition.schema
: null;
let resolve = resolver_builder_1.getResolver({
operation,
oas,
payloadName: payloadSchemaName,
data,
baseUrl
});
// create args:
let args = schema_builder_1.getArgs({
parameters: operation.parameters,
payloadSchemaName: payloadSchemaName,
payloadSchema,
operation,
data,
oas
});
return {
type,
resolve,
args,
description: operation.description
};
}
//# sourceMappingURL=index.js.map