Skip to content

Commit 10cea5e

Browse files
Alan-ChaErikWittern
authored andcommitted
Add preliminaryChecks method
Signed-off-by: Alan Cha <[email protected]>
1 parent 0c9cb58 commit 10cea5e

File tree

7 files changed

+171
-4
lines changed

7 files changed

+171
-4
lines changed

packages/oasgraph/lib/index.js

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

packages/oasgraph/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/oasgraph/lib/utils.js

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

packages/oasgraph/lib/utils.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/oasgraph/src/index.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,8 @@ async function translateOpenApiToGraphQL(
172172
*/
173173
const data: PreprocessingData = preprocessOas(oass, options)
174174

175+
preliminaryChecks(options, data, oass)
176+
175177
/**
176178
* Create GraphQL fields for every operation and structure them based on their
177179
* characteristics (query vs. mutation, auth vs. non-auth).
@@ -447,3 +449,66 @@ function sortOperations(op1: Operation, op2: Operation): number {
447449
}
448450
}
449451
}
452+
453+
/**
454+
* Ensures that the options are valid
455+
*/
456+
function preliminaryChecks(options: InternalOptions, data: PreprocessingData, oass: Oas3[]): void {
457+
// Check if OASs have unique titles
458+
const titles = oass.map((oas) => {
459+
return oas.info.title
460+
})
461+
462+
// Find duplicates among titles
463+
new Set(titles.filter((title, index) => {
464+
return titles.indexOf(title) !== index
465+
})).forEach((title) => {
466+
handleWarning({
467+
typeKey: 'MULTIPLE_OAS_SAME_TITLE',
468+
culprit: title,
469+
data,
470+
log: translationLog
471+
})
472+
})
473+
474+
// Check customResolvers
475+
if (typeof options.customResolvers === 'object') {
476+
// Check that all OASs that are referenced in the customResolvers are provided
477+
Object.keys(options.customResolvers).filter((title) => {
478+
// If no OAS contains this title
479+
return !oass.some((oas) => {
480+
return title === oas.info.title
481+
})
482+
}).forEach((title) => {
483+
handleWarning({
484+
typeKey: 'CUSTOM_RESOLVER_UNKNOWN_OAS',
485+
culprit: title,
486+
data,
487+
log: translationLog
488+
})
489+
})
490+
491+
Object.keys(options.customResolvers).forEach((title) => {
492+
493+
// Get all operations from a particular OAS
494+
const operations = Object.values(data.operations).filter((operation) => {
495+
return title === operation.oas.info.title
496+
})
497+
498+
Object.keys(options.customResolvers[title]).forEach((path) => {
499+
Object.keys(options.customResolvers[title][path]).forEach((method) => {
500+
if (!operations.some((operation) => {
501+
return path === operation.path && method === operation.method
502+
})) {
503+
handleWarning({
504+
typeKey: 'CUSTOM_RESOLVER_UNKNOWN_PATH_METHOD',
505+
culprit: `A custom resolver references an operation with path "${path}" and method "${method}" but no such operation exists in OAS with title "${title}"`,
506+
data,
507+
log: translationLog
508+
})
509+
}
510+
})
511+
})
512+
})
513+
}
514+
}

packages/oasgraph/src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,28 @@ export const WarningTypes: {
124124
message: `A schema reference could not be resolved due to unknown OAS origin.`,
125125
mitigation: `The schema will not be resolved, which may cause issues.`
126126
}
127+
},
128+
MULTIPLE_OAS_SAME_TITLE: (culprit: string, solution: string) => {
129+
return {
130+
type: 'multipleOasSameTitle',
131+
message: `Multiple OASs share the same title "${culprit}"`,
132+
mitigation: `Continue as is - may break other features`
133+
}
134+
},
135+
CUSTOM_RESOLVER_UNKNOWN_OAS: (culprit: string, solution: string) => {
136+
return {
137+
type: 'customResolverUnknownOAS',
138+
message: `Some custom resolvers reference OAS with title "${culprit}" but no OAS with such title is provided`,
139+
mitigation: `Ignore this set of custom resolvers`
140+
}
141+
},
142+
CUSTOM_RESOLVER_UNKNOWN_PATH_METHOD: (culprit: string, solution: string) => {
143+
return {
144+
type: 'customResolverUnknownOperation',
145+
// TODO: improve message
146+
message: culprit,
147+
mitigation: `Ignore this set of custom resolvers`
148+
}
127149
}
128150
}
129151

packages/oasgraph/test/example_gql_server.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const OasGraph = require('../lib/index.js')
1212

1313
let oas = require('./fixtures/example_oas.json')
1414
// let oas2 = require('./fixtures/example_oas2.json')
15-
// let oas3 = require('./fixtures/example_oas3.json')
15+
let oas3 = require('./fixtures/example_oas3.json')
1616

1717
// let oas = require('./fixtures/github_oas.json')
1818
// let oas = require('./fixtures/instagram.json')
@@ -24,7 +24,7 @@ let oas = require('./fixtures/example_oas.json')
2424
// // requires Box API from API Guru
2525
// let oas = yamljs.parse(fs.readFileSync('../tmp/APIs/box.com/content/2.0/swagger.yaml', 'utf8'))
2626

27-
OasGraph.createGraphQlSchema(oas, { provideErrorExtension: false, customResolvers: {
27+
OasGraph.createGraphQlSchema([oas, oas3], { provideErrorExtension: false, customResolvers: {
2828
'Example API': {
2929
'/users/{username}': {
3030
'get': () => {

0 commit comments

Comments
 (0)