Skip to content

Commit

Permalink
Improve evaluation script
Browse files Browse the repository at this point in the history
Signed-off-by: Alan Cha <Alan.cha1@ibm.com>
  • Loading branch information
Alan-Cha committed Mar 3, 2021
1 parent 0daf771 commit 15ed7f2
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 94 deletions.
40 changes: 24 additions & 16 deletions packages/openapi-to-graphql/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/index.js.map

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions packages/openapi-to-graphql/lib/preprocessor.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/preprocessor.js.map

Large diffs are not rendered by default.

65 changes: 36 additions & 29 deletions packages/openapi-to-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,32 +149,39 @@ export function createGraphQLSchema<TSource, TContext, TArgs>(
if (Array.isArray(spec)) {
// Convert all non-OAS 3 into OAS 3
Promise.all(
spec.map(ele => {
spec.map((ele) => {
return Oas3Tools.getValidOAS3(ele)
})
).then(oass => {
resolve(
translateOpenAPIToGraphQL(
oass,
options as InternalOptions<TSource, TContext, TArgs>
)
.then((oass) => {
resolve(
translateOpenAPIToGraphQL(
oass,
options as InternalOptions<TSource, TContext, TArgs>
)
)
)
})
})
.catch((error) => {
reject(error)
})
} else {
/**
* Check if the spec is a valid OAS 3
* If the spec is OAS 2.0, attempt to translate it into 3, then try to
* translate the spec into a GraphQL schema
*/

Oas3Tools.getValidOAS3(spec).then(oas => {
resolve(
translateOpenAPIToGraphQL(
[oas],
options as InternalOptions<TSource, TContext, TArgs>
Oas3Tools.getValidOAS3(spec)
.then((oas) => {
resolve(
translateOpenAPIToGraphQL(
[oas],
options as InternalOptions<TSource, TContext, TArgs>
)
)
)
})
})
.catch((error) => {
reject(error)
})
}
})
}
Expand Down Expand Up @@ -518,15 +525,15 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
mutationFields = sortObject(mutationFields)
subscriptionFields = sortObject(subscriptionFields)
authQueryFields = sortObject(authQueryFields)
Object.keys(authQueryFields).forEach(key => {
Object.keys(authQueryFields).forEach((key) => {
authQueryFields[key] = sortObject(authQueryFields[key])
})
authMutationFields = sortObject(authMutationFields)
Object.keys(authMutationFields).forEach(key => {
Object.keys(authMutationFields).forEach((key) => {
authMutationFields[key] = sortObject(authMutationFields[key])
})
authSubscriptionFields = sortObject(authSubscriptionFields)
Object.keys(authSubscriptionFields).forEach(key => {
Object.keys(authSubscriptionFields).forEach((key) => {
authSubscriptionFields[key] = sortObject(authSubscriptionFields[key])
})

Expand Down Expand Up @@ -719,13 +726,13 @@ function checkCustomResolversStructure<TSource, TContext, TArgs>(
if (typeof customResolvers === 'object') {
// Check that all OASs that are referenced in the customResolvers are provided
Object.keys(customResolvers)
.filter(title => {
.filter((title) => {
// If no OAS contains this title
return !data.oass.some(oas => {
return !data.oass.some((oas) => {
return title === oas.info.title
})
})
.forEach(title => {
.forEach((title) => {
handleWarning({
mitigationType: MitigationTypes.CUSTOM_RESOLVER_UNKNOWN_OAS,
message:
Expand All @@ -737,16 +744,16 @@ function checkCustomResolversStructure<TSource, TContext, TArgs>(
})

// TODO: Only run the following test on OASs that exist. See previous check.
Object.keys(customResolvers).forEach(title => {
Object.keys(customResolvers).forEach((title) => {
// Get all operations from a particular OAS
const operations = Object.values(data.operations).filter(operation => {
const operations = Object.values(data.operations).filter((operation) => {
return title === operation.oas.info.title
})

Object.keys(customResolvers[title]).forEach(path => {
Object.keys(customResolvers[title][path]).forEach(method => {
Object.keys(customResolvers[title]).forEach((path) => {
Object.keys(customResolvers[title][path]).forEach((method) => {
if (
!operations.some(operation => {
!operations.some((operation) => {
return path === operation.path && method === operation.method
})
) {
Expand Down Expand Up @@ -775,7 +782,7 @@ function preliminaryChecks<TSource, TContext, TArgs>(
data: PreprocessingData<TSource, TContext, TArgs>
): void {
// Check if OASs have unique titles
const titles = data.oass.map(oas => {
const titles = data.oass.map((oas) => {
return oas.info.title
})

Expand All @@ -784,7 +791,7 @@ function preliminaryChecks<TSource, TContext, TArgs>(
titles.filter((title, index) => {
return titles.indexOf(title) !== index
})
).forEach(title => {
).forEach((title) => {
handleWarning({
mitigationType: MitigationTypes.MULTIPLE_OAS_SAME_TITLE,
message: `Multiple OAS share the same title '${title}'`,
Expand Down

0 comments on commit 15ed7f2

Please sign in to comment.