Skip to content

Commit

Permalink
Merge pull request #28 from BouyguesTelecom/fix/available-types
Browse files Browse the repository at this point in the history
🐛 More precise available types
  • Loading branch information
arnaud authored Jul 26, 2024
2 parents 506d36a + a3f04be commit 5171d9f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
6 changes: 4 additions & 2 deletions packages/graphql-mesh/tests/cases/configFromSwaggers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,10 @@ describe('ConfigFromSwaggers tests', () => {

// Test function to get available types
it('should return all the available types', () => {
const expectedTypes = ['Person', 'Vehicles', 'Vehicle', 'Car', 'Bike']
expect(instance.getAvailableTypes()).toEqual(expectedTypes)
const expectedTypes = ['Person', 'Vehicles']
const expectedVersionedTypes = ['Person_v0', 'Vehicles_v0']
expect(instance.getAvailableTypes(false)).toEqual(expectedTypes)
expect(instance.getAvailableTypes(true)).toEqual(expectedVersionedTypes)
})

// Test function to get interfaces with their children
Expand Down
46 changes: 40 additions & 6 deletions packages/graphql-mesh/utils/configFromSwaggers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,44 @@ export default class ConfigFromSwaggers {
* Extracts and returns all available schema types from the Swagger specifications.
*
* @returns {string[]} An array of schema type names.
*
* This function flattens the list of schemas from all Swagger specifications
* and extracts the keys (schema names) from the components' schemas.
*/
getAvailableTypes(): string[] {
return this.specs.flatMap((spec) => Object.keys(spec.components?.schemas ?? {}))
getAvailableTypes(areSchemasSuffixed: boolean): string[] {
const availableTypes = []
this.specs.forEach((spec) => {
// Extract the major version from the Swagger specification
const xVersion = spec.info.version.split('.')[0]
// Check if the specification contains paths
if (spec.paths) {
// Iterate over each method in the paths of the specification
Object.values(spec.paths).forEach((methods) => {
Object.values(methods).forEach((method) => {
// Retrieve the response with the 200 status code (success)
const response200 = method['responses']?.['200']
// Check if the 200 response has content
if (response200?.content) {
// Iterate over each content type in the 200 response
Object.values(response200.content).forEach((content) => {
// Retrieve the schema reference
const ref = content['schema']?.$ref
// If the reference exists, extract the type of object from the referenced schema
if (ref) {
const match = ref.match(/#\/components\/schemas\/(.+)/)
// If a match is found, add the type to availableTypes (with its version)
if (match) {
if (areSchemasSuffixed) {
availableTypes.push(`${match[1]}_v${xVersion}`)
} else {
availableTypes.push(`${match[1]}`)
}
}
}
})
}
})
})
}
})
return availableTypes
}

/**
Expand Down Expand Up @@ -117,6 +149,7 @@ export default class ConfigFromSwaggers {
* This function supports configurations that allow for schema renaming based on the Swagger version.
*/
createTypeDefsAndResolvers() {
let areSchemasSuffixed = false
if (this.config.sources) {
this.specs.forEach((spec, index) => {
// Apply naming transformations if specified in the configuration
Expand All @@ -136,6 +169,7 @@ export default class ConfigFromSwaggers {
})
// Replace the original schemas with the suffixed schemas
spec.components.schemas = schemasWithSuffix
areSchemasSuffixed = true
}
})
}
Expand All @@ -150,7 +184,7 @@ export default class ConfigFromSwaggers {
}
`

const availableTypes = this.getAvailableTypes()
const availableTypes = this.getAvailableTypes(areSchemasSuffixed)
const interfacesWithChildren = this.getInterfacesWithChildren()
const catalog = this.catalog
const config = this.config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ export const generateTypeDefsAndResolversFromSwagger = (
// Delete the additional typeDefs section if no new fields have been added
subTypeDefs = subTypeDefs.replace(`extend ${schemaType} ${trimedSchemaKey} {\n}\n`, '')

if (matchedLinkItems.swaggers) {
if (subTypeDefs !== "") {
typeDefs += subTypeDefs
resolvers[trimedSchemaKey] = subResolver
}
Expand Down

0 comments on commit 5171d9f

Please sign in to comment.