fix(utils/dml): set-relationship graphql generator from DML wrong managed belongsTo (#9932)

**What**
Currently, when setting a `belongsTo` relationship on the DML with the otherside being `hasMany` it result in a wrongly generated gql output making the belongs to being a collection of the relation instead of the relation directly. This pr fixes this issue
This commit is contained in:
Adrien de Peretti
2024-11-05 14:14:28 +00:00
committed by GitHub
parent bbf4af1725
commit 879ce33090
3 changed files with 13 additions and 34 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---
fix(utils/dml): set-relationship graphql generator from DML wrong managed belongsTo
@@ -2,7 +2,7 @@ import { model } from "../entity-builder"
import { toGraphQLSchema } from "../helpers/create-graphql" import { toGraphQLSchema } from "../helpers/create-graphql"
describe("GraphQL builder", () => { describe("GraphQL builder", () => {
test("define an entity", () => { test("should generate the proper graphql output for the given entities definition", () => {
const tag = model.define("tag", { const tag = model.define("tag", {
id: model.id(), id: model.id(),
value: model.text(), value: model.text(),
@@ -70,7 +70,7 @@ describe("GraphQL builder", () => {
email: Email! email: Email!
spend_limit: String! spend_limit: String!
phones: [String]! phones: [String]!
group: [Group]! group: Group!
role: UserRoleEnum! role: UserRoleEnum!
tags: [Tag]! tags: [Tag]!
raw_spend_limit: JSON! raw_spend_limit: JSON!
@@ -1,33 +1,17 @@
import { import { RelationshipMetadata } from "@medusajs/types"
PropertyType,
RelationshipMetadata,
RelationshipType,
} from "@medusajs/types"
import { camelToSnakeCase } from "../../../common"
import { DmlEntity } from "../../entity" import { DmlEntity } from "../../entity"
import { BelongsTo } from "../../relations/belongs-to" import { HasMany, HasOne } from "../../relations"
import { HasMany } from "../../relations/has-many"
import { HasOne } from "../../relations/has-one"
import { ManyToMany as DmlManyToMany } from "../../relations/many-to-many" import { ManyToMany as DmlManyToMany } from "../../relations/many-to-many"
import { parseEntityName } from "../entity-builder/parse-entity-name" import { parseEntityName } from "../entity-builder"
function defineRelationships( function defineRelationships(
modelName: string, modelName: string,
relationship: RelationshipMetadata, relationship: RelationshipMetadata,
relatedEntity: DmlEntity<
Record<string, PropertyType<any> | RelationshipType<any>>,
any
>,
{ relatedModelName }: { relatedModelName: string } { relatedModelName }: { relatedModelName: string }
) { ) {
let extra: string | undefined let extra: string | undefined
const fieldName = relationship.name const fieldName = relationship.name
const mappedBy = relationship.mappedBy || camelToSnakeCase(modelName)
const { schema: relationSchema } = relatedEntity.parse()
const otherSideRelation = relationSchema[mappedBy]
if (relationship.options?.mappedBy && HasOne.isHasOne(relationship)) { if (relationship.options?.mappedBy && HasOne.isHasOne(relationship)) {
const otherSideFieldName = relationship.options.mappedBy const otherSideFieldName = relationship.options.mappedBy
extra = `extend type ${relatedModelName} {\n ${otherSideFieldName}: ${modelName}!\n}` extra = `extend type ${relatedModelName} {\n ${otherSideFieldName}: ${modelName}!\n}`
@@ -35,14 +19,9 @@ function defineRelationships(
let isArray = false let isArray = false
/**
* Otherside is a has many. Hence we should defined a ManyToOne
*/
if ( if (
HasMany.isHasMany(otherSideRelation) || HasMany.isHasMany(relationship) ||
DmlManyToMany.isManyToMany(relationship) || DmlManyToMany.isManyToMany(relationship)
(BelongsTo.isBelongsTo(otherSideRelation) &&
HasMany.isHasMany(relationship))
) { ) {
isArray = true isArray = true
} }
@@ -87,10 +66,5 @@ export function setGraphQLRelationship(
pgSchema, pgSchema,
} }
return defineRelationships( return defineRelationships(entityName, relationship, relatedEntityInfo)
entityName,
relationship,
relatedEntity,
relatedEntityInfo
)
} }