feat: Application types generation from project GQL schema's (#8995)
This commit is contained in:
committed by
GitHub
parent
ac30a989f4
commit
2c5e72d141
@@ -1,6 +1,11 @@
|
||||
import { MedusaModule } from "@medusajs/modules-sdk"
|
||||
import { ModuleJoinerConfig, ModuleJoinerRelationship } from "@medusajs/types"
|
||||
import { camelToSnakeCase, lowerCaseFirst, toPascalCase } from "@medusajs/utils"
|
||||
import {
|
||||
camelToSnakeCase,
|
||||
isString,
|
||||
lowerCaseFirst,
|
||||
toPascalCase,
|
||||
} from "@medusajs/utils"
|
||||
import { composeTableName } from "./compose-link-name"
|
||||
|
||||
export function generateGraphQLSchema(
|
||||
@@ -38,15 +43,18 @@ export function generateGraphQLSchema(
|
||||
)
|
||||
}
|
||||
|
||||
const extJoinerConfig = MedusaModule.getJoinerConfig(
|
||||
/* const extJoinerConfig = MedusaModule.getJoinerConfig(
|
||||
extend.relationship.serviceName
|
||||
)
|
||||
let extendedEntityName =
|
||||
extJoinerConfig?.linkableKeys?.[extend.relationship.foreignKey]!
|
||||
)*/
|
||||
|
||||
if (!isReadOnlyLink && !extendedEntityName && (!primary || !foreign)) {
|
||||
const extendedEntityName =
|
||||
extendedModule[extend.serviceName].__joinerConfig.linkableKeys[
|
||||
extend.relationship.primaryKey
|
||||
]
|
||||
|
||||
if (!isReadOnlyLink && (!primary || !foreign || !extendedEntityName)) {
|
||||
logger.warn(
|
||||
`Link modules schema: No linkable key found for ${extend.relationship.foreignKey} on module ${extend.relationship.serviceName}.`
|
||||
`Link modules schema: No linkable key found for ${extend.relationship.primaryKey} on module ${extend.serviceName}.`
|
||||
)
|
||||
|
||||
continue
|
||||
@@ -57,15 +65,82 @@ export function generateGraphQLSchema(
|
||||
)
|
||||
|
||||
let type = extend.relationship.isList ? `[${entityName}]` : entityName
|
||||
if (extJoinerConfig?.isReadOnlyLink) {
|
||||
type = extend.relationship.isList
|
||||
if (joinerConfig?.isReadOnlyLink) {
|
||||
// TODO: In readonly, the relation ship of the extend should be applied on all entities in the module that have the relationshiop foregin key attribute
|
||||
/*type = extend.relationship.isList
|
||||
? `[${extendedEntityName}]`
|
||||
: extendedEntityName
|
||||
: extendedEntityName*/
|
||||
continue
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the field aliases shortcut to extend the entity with it
|
||||
*/
|
||||
const fieldsAliasesField = Object.entries(extend.fieldAlias || {})
|
||||
.map(([field, config]) => {
|
||||
const path = isString(config) ? config : config.path
|
||||
const isList = isString(config)
|
||||
? extend.relationship.isList
|
||||
: config.isList ?? extend.relationship.isList
|
||||
|
||||
const pathSegments = path.split(",").reverse()
|
||||
|
||||
/*const relationshipMarkerIndex = pathSegments.findIndex((segment) => {
|
||||
return !!joinerConfig.relationships!.find(
|
||||
(relation) => relation.alias === targetEntityAlias
|
||||
)
|
||||
})
|
||||
|
||||
if (relationshipMarkerIndex === -1) {
|
||||
return
|
||||
}*/
|
||||
|
||||
/*const relationshipPropertyPath = pathSegments
|
||||
.slice(0, relationshipMarkerIndex + 1)
|
||||
.reverse()*/
|
||||
|
||||
const targetEntityAlias = path.split(".").pop()
|
||||
|
||||
const targetEntityRelation = joinerConfig.relationships?.find(
|
||||
(relation) => relation.alias === targetEntityAlias
|
||||
)
|
||||
|
||||
if (!targetEntityRelation) {
|
||||
return
|
||||
}
|
||||
|
||||
const targetEntityName = MedusaModule.getJoinerConfig(
|
||||
targetEntityRelation.serviceName
|
||||
).linkableKeys?.[targetEntityRelation.foreignKey]
|
||||
|
||||
if (!targetEntityName) {
|
||||
logger.warn(
|
||||
`Link modules schema: No linkable key found for ${targetEntityRelation.foreignKey} on module ${targetEntityRelation.serviceName}.`
|
||||
)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// TODO: Re visit field aliases that access properties from a type
|
||||
/*const targetEntityType = `${targetEntityName}${
|
||||
relationshipPropertyPath.length
|
||||
? relationshipPropertyPath.reduce((acc, value) => {
|
||||
return `${acc}[${value}]`
|
||||
}, targetEntityName)
|
||||
: ""
|
||||
}`*/
|
||||
|
||||
return `${field}: ${
|
||||
isList ? `[${targetEntityName}]` : targetEntityName
|
||||
}`
|
||||
})
|
||||
.filter(Boolean)
|
||||
|
||||
typeDef += `
|
||||
extend type ${extend.serviceName} {
|
||||
extend type ${extendedEntityName} {
|
||||
${fieldName}: ${type}
|
||||
|
||||
${fieldsAliasesField.join("\n")}
|
||||
}
|
||||
`
|
||||
}
|
||||
@@ -92,14 +167,26 @@ export function generateGraphQLSchema(
|
||||
}
|
||||
}
|
||||
|
||||
// Link table relationships
|
||||
const primaryField = `${camelToSnakeCase(primary.alias)}: ${toPascalCase(
|
||||
composeTableName(primary.serviceName)
|
||||
)}`
|
||||
// TODO: temporary, every module might always expose their schema
|
||||
const doesPrimaryExportSchema = !!MedusaModule.getJoinerConfig(
|
||||
primary.serviceName
|
||||
)?.schema
|
||||
const doesForeignExportSchema = !!MedusaModule.getJoinerConfig(
|
||||
foreign.serviceName
|
||||
)?.schema
|
||||
|
||||
const foreignField = `${camelToSnakeCase(foreign.alias)}: ${toPascalCase(
|
||||
composeTableName(foreign.serviceName)
|
||||
)}`
|
||||
// Link table relationships
|
||||
const primaryField = doesPrimaryExportSchema
|
||||
? `${camelToSnakeCase(primary.alias)}: ${toPascalCase(
|
||||
composeTableName(primary.serviceName)
|
||||
)}`
|
||||
: ""
|
||||
|
||||
const foreignField = doesForeignExportSchema
|
||||
? `${camelToSnakeCase(foreign.alias)}: ${toPascalCase(
|
||||
composeTableName(foreign.serviceName)
|
||||
)}`
|
||||
: ""
|
||||
|
||||
typeDef += `
|
||||
type ${entityName} {
|
||||
|
||||
Reference in New Issue
Block a user