From a0f8576606265d71cf8ebd770a3cc97d3a0c40da Mon Sep 17 00:00:00 2001 From: Adrien FIGARD Date: Thu, 25 Jul 2024 16:53:02 +0200 Subject: [PATCH] :bug: More precise available types --- .../graphql-mesh/utils/configFromSwaggers.ts | 36 ++++++++++++++++--- .../utils/generateTypeDefsAndResolvers.ts | 2 +- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/packages/graphql-mesh/utils/configFromSwaggers.ts b/packages/graphql-mesh/utils/configFromSwaggers.ts index 2980cb0..2c889c3 100755 --- a/packages/graphql-mesh/utils/configFromSwaggers.ts +++ b/packages/graphql-mesh/utils/configFromSwaggers.ts @@ -60,12 +60,40 @@ 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 ?? {})) + 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) { + availableTypes.push(`${match[1]}_v${xVersion}`) + } + } + }) + } + }) + }) + } + }) + return availableTypes } /** diff --git a/packages/graphql-mesh/utils/generateTypeDefsAndResolvers.ts b/packages/graphql-mesh/utils/generateTypeDefsAndResolvers.ts index f6ca2bc..6a67283 100755 --- a/packages/graphql-mesh/utils/generateTypeDefsAndResolvers.ts +++ b/packages/graphql-mesh/utils/generateTypeDefsAndResolvers.ts @@ -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 }