Skip to content

Commit eebb855

Browse files
committed
add createSubscriptionsFromCallbacks option ; code cleaning
1 parent 6dfc53e commit eebb855

14 files changed

+109
-95
lines changed

packages/openapi-to-graphql/lib/index.js

Lines changed: 9 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/oas_3_tools.js

Lines changed: 3 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/oas_3_tools.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/preprocessor.js

Lines changed: 14 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/preprocessor.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/resolver_builder.js

Lines changed: 8 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/resolver_builder.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/types/options.d.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,13 @@ export declare type Options = {
129129
};
130130
};
131131
};
132+
/**
133+
* Allow to generate subscription fields from CallbackObjects in OpenAPI schema.
134+
*
135+
* Path ( runtime expression ) of the CallbackObject will be interpolated,
136+
* as topic of publish / subscription to use with a pubsub instance.
137+
*/
138+
createSubscriptionsFromCallbacks?: boolean;
132139
/**
133140
* Determines whether OpenAPI-to-GraphQL should create viewers that allow users to pass
134141
* basic auth and API key credentials.
@@ -276,6 +283,13 @@ export declare type InternalOptions = {
276283
};
277284
};
278285
};
286+
/**
287+
* Allow to generate subscription fields from CallbackObjects in OpenAPI schema.
288+
*
289+
* Path ( runtime expression ) of the CallbackObject will be interpolated,
290+
* as topic of publish / subscription to use with a pubsub instance.
291+
*/
292+
createSubscriptionsFromCallbacks?: boolean;
279293
/**
280294
* Determines whether OpenAPI-to-GraphQL should create viewers that allow users to pass
281295
* basic auth and API key credentials.

packages/openapi-to-graphql/src/index.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ export async function createGraphQLSchema(
9999
? options.genericPayloadArgName
100100
: false
101101

102+
options.createSubscriptionsFromCallbacks =
103+
typeof options.createSubscriptionsFromCallbacks === 'boolean'
104+
? options.createSubscriptionsFromCallbacks
105+
: false
106+
102107
// Authentication options
103108
options.viewer = typeof options.viewer === 'boolean' ? options.viewer : true
104109
options.sendOAuthTokenInQuery =
@@ -181,6 +186,7 @@ async function translateOpenAPIToGraphQL(
181186
connectOptions,
182187
baseUrl,
183188
customResolvers,
189+
createSubscriptionsFromCallbacks,
184190

185191
// Authentication options
186192
viewer,
@@ -211,6 +217,7 @@ async function translateOpenAPIToGraphQL(
211217
baseUrl,
212218
customResolvers,
213219
selectQueryOrMutationField,
220+
createSubscriptionsFromCallbacks,
214221

215222
// Authentication options
216223
viewer,
@@ -231,8 +238,6 @@ async function translateOpenAPIToGraphQL(
231238

232239
preliminaryChecks(options, data)
233240

234-
// console.log('PREPROCESS OPENAPI', data)
235-
236241
/**
237242
* Create GraphQL fields for every operation and structure them based on their
238243
* characteristics (query vs. mutation, auth vs. non-auth).
@@ -244,6 +249,8 @@ async function translateOpenAPIToGraphQL(
244249
let authMutationFields = {}
245250
let authSubscriptionFields = {}
246251

252+
// todo parse data.callbacks to recompose subscription ?
253+
247254
Object.entries(data.operations).forEach(([operationId, operation]) => {
248255
translationLog(`Process operation '${operationId}'...`)
249256

@@ -383,7 +390,7 @@ async function translateOpenAPIToGraphQL(
383390
mutationFields[saneFieldName] = field
384391
}
385392
}
386-
} else {
393+
} else if (operation.isSubscription) {
387394
// handle subscriptions from operation.callbacks
388395
// 1) cbName would be the subscription field name
389396
// each paths contained in operation.callbacks[cbName]
@@ -592,7 +599,7 @@ function getFieldForOperation(
592599

593600
if (operation.isSubscription) {
594601
const responseSchemaName = operation.responseDefinition
595-
? operation.responseDefinition.graphQLInputObjectTypeName
602+
? operation.responseDefinition.graphQLTypeName
596603
: null
597604

598605
const resolve = getPublishResolver({

packages/openapi-to-graphql/src/oas_3_tools.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -982,10 +982,10 @@ export function getEndpointCallbacks(
982982
data: PreprocessingData
983983
): { [key: string]: CallbackObject } {
984984
const callbacks = {}
985-
const endpoint: OperationObject = oas.paths[path][method]
985+
const operation: OperationObject = oas.paths[path][method]
986986

987-
if (typeof endpoint.callbacks === 'object') {
988-
let callbacksObject: CallbacksObject = endpoint.callbacks
987+
if (typeof operation.callbacks === 'object') {
988+
let callbacksObject: CallbacksObject = operation.callbacks
989989
for (let callbackName in callbacksObject) {
990990
if (typeof callbacksObject[callbackName] === 'object') {
991991
let callbackObject: CallbackObject | ReferenceObject =
@@ -999,12 +999,9 @@ export function getEndpointCallbacks(
999999
} else {
10001000
callbackObject = (callbackObject as any) as CallbackObject
10011001
}
1002-
// console.log("CHECK CALLBACK OBJ", callbackName, callbackObject)
1003-
10041002
// Make sure CallbackObject contains PathItemObject:
10051003
for (let expression in callbackObject) {
10061004
let pathItem: PathItemObject = callbackObject[expression]
1007-
// console.log("CHECK CALLBACK ITEM", expression, pathItem)
10081005
if (typeof (pathItem as ReferenceObject).$ref === 'string') {
10091006
pathItem = resolveRef(callbackObject[callbackName]['$ref'], oas)
10101007
} else {

packages/openapi-to-graphql/src/preprocessor.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,8 @@ function processOperationCallbacks(
194194
oas
195195
}
196196

197+
// todo register in data.callbacks instead ?
198+
197199
/**
198200
* Handle operationId property name collision
199201
* May occur if multiple OAS are provided
@@ -244,9 +246,13 @@ export function preprocessOas(
244246
data.options.report.numOps += Oas3Tools.countOperations(oas)
245247
data.options.report.numOpsMutation += Oas3Tools.countOperationsMutation(oas)
246248
data.options.report.numOpsQuery += Oas3Tools.countOperationsQuery(oas)
247-
data.options.report.numOpsSubscription += Oas3Tools.countOperationsSubscription(
248-
oas
249-
)
249+
if (data.options.createSubscriptionsFromCallbacks) {
250+
data.options.report.numOpsSubscription += Oas3Tools.countOperationsSubscription(
251+
oas
252+
)
253+
} else {
254+
data.options.report.numOpsSubscription = 0
255+
}
250256

251257
// Get security schemes
252258
const currentSecurity = getProcessedSecuritySchemes(oas, data)
@@ -355,16 +361,15 @@ export function preprocessOas(
355361
// Links
356362
const links = Oas3Tools.getEndpointLinks(path, method, oas, data)
357363

358-
// Callbacks containing [key: string]:PathItemObject
359-
const callbacks = Oas3Tools.getEndpointCallbacks(
360-
path,
361-
method,
362-
oas,
363-
data
364-
)
364+
let callbacks = {}
365+
366+
if (options.createSubscriptionsFromCallbacks) {
367+
// Callbacks containing [key: string]:PathItemObject
368+
callbacks = Oas3Tools.getEndpointCallbacks(path, method, oas, data)
365369

366-
// should every callback items be registered as operations ?
367-
processOperationCallbacks(callbacks, oas, data, options)
370+
// should every callback items be registered as operations ?
371+
processOperationCallbacks(callbacks, oas, data, options)
372+
}
368373

369374
const responseDefinition = createDataDef(
370375
responseSchemaNames,

0 commit comments

Comments
 (0)