chore: Add import from framework types at the top of ay generated types file (#9389)

This commit is contained in:
Adrien de Peretti
2024-10-01 08:53:52 +02:00
committed by GitHub
parent 9b6d0e9d56
commit b9e772b530
@@ -1,5 +1,5 @@
import { codegen } from "@graphql-codegen/core" import { codegen } from "@graphql-codegen/core"
import { parse, printSchema, type GraphQLSchema } from "graphql" import { type GraphQLSchema, parse, printSchema } from "graphql"
import * as typescriptPlugin from "@graphql-codegen/typescript" import * as typescriptPlugin from "@graphql-codegen/typescript"
import { ModuleJoinerConfig } from "@medusajs/types" import { ModuleJoinerConfig } from "@medusajs/types"
import { FileSystem } from "../common" import { FileSystem } from "../common"
@@ -54,11 +54,12 @@ async function generateTypes({
}) { }) {
const fileSystem = new FileSystem(outputDir) const fileSystem = new FileSystem(outputDir)
let output = await codegen(config) let output = 'import "@medusajs/framework/types\n'
output += await codegen(config)
const entryPoints = buildEntryPointsTypeMap({ schema: output, joinerConfigs }) const entryPoints = buildEntryPointsTypeMap({ schema: output, joinerConfigs })
const remoteQueryEntryPoints = ` const remoteQueryEntryPoints = `
declare module '@medusajs/types' { declare module '@medusajs/framework/types' {
interface ${interfaceName} { interface ${interfaceName} {
${entryPoints ${entryPoints
.map((entry) => ` ${entry.entryPoint}: ${entry.entityType}`) .map((entry) => ` ${entry.entryPoint}: ${entry.entityType}`)
@@ -68,19 +69,20 @@ ${entryPoints
output += remoteQueryEntryPoints output += remoteQueryEntryPoints
const barrelFileName = "index.d.ts"
await fileSystem.create(filename + ".d.ts", output) await fileSystem.create(filename + ".d.ts", output)
const doesBarrelExists = await fileSystem.exists("index.d.ts") const doesBarrelExists = await fileSystem.exists(barrelFileName)
if (!doesBarrelExists) { if (!doesBarrelExists) {
await fileSystem.create( await fileSystem.create(
"index.d.ts", barrelFileName,
`export * as ${interfaceName}Types from './${filename}'` `export * as ${interfaceName}Types from './${filename}'`
) )
} else { } else {
const content = await fileSystem.contents("index.d.ts") const content = await fileSystem.contents(barrelFileName)
if (!content.includes(`${interfaceName}Types`)) { if (!content.includes(`${interfaceName}Types`)) {
const newContent = `export * as ${interfaceName}Types from './${filename}'\n${content}` const newContent = `export * as ${interfaceName}Types from './${filename}'\n${content}`
await fileSystem.create("index.d.ts", newContent) await fileSystem.create(barrelFileName, newContent)
} }
} }
} }