Skip to content

Commit

Permalink
fix(utils/dml): set-relationship graphql generator from DML wrong man…
Browse files Browse the repository at this point in the history
…aged belongsTo (medusajs#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
  • Loading branch information
adrien2p authored Nov 5, 2024
1 parent bbf4af1 commit 879ce33
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 34 deletions.
5 changes: 5 additions & 0 deletions .changeset/tame-sloths-pump.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---

fix(utils/dml): set-relationship graphql generator from DML wrong managed belongsTo
4 changes: 2 additions & 2 deletions packages/core/utils/src/dml/__tests__/create-graphql.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { model } from "../entity-builder"
import { toGraphQLSchema } from "../helpers/create-graphql"

describe("GraphQL builder", () => {
test("define an entity", () => {
test("should generate the proper graphql output for the given entities definition", () => {
const tag = model.define("tag", {
id: model.id(),
value: model.text(),
Expand Down Expand Up @@ -70,7 +70,7 @@ describe("GraphQL builder", () => {
email: Email!
spend_limit: String!
phones: [String]!
group: [Group]!
group: Group!
role: UserRoleEnum!
tags: [Tag]!
raw_spend_limit: JSON!
Expand Down
Original file line number Diff line number Diff line change
@@ -1,48 +1,27 @@
import {
PropertyType,
RelationshipMetadata,
RelationshipType,
} from "@medusajs/types"
import { camelToSnakeCase } from "../../../common"
import { RelationshipMetadata } from "@medusajs/types"
import { DmlEntity } from "../../entity"
import { BelongsTo } from "../../relations/belongs-to"
import { HasMany } from "../../relations/has-many"
import { HasOne } from "../../relations/has-one"
import { HasMany, HasOne } from "../../relations"
import { ManyToMany as DmlManyToMany } from "../../relations/many-to-many"
import { parseEntityName } from "../entity-builder/parse-entity-name"
import { parseEntityName } from "../entity-builder"

function defineRelationships(
modelName: string,
relationship: RelationshipMetadata,
relatedEntity: DmlEntity<
Record<string, PropertyType<any> | RelationshipType<any>>,
any
>,
{ relatedModelName }: { relatedModelName: string }
) {
let extra: string | undefined
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)) {
const otherSideFieldName = relationship.options.mappedBy
extra = `extend type ${relatedModelName} {\n ${otherSideFieldName}: ${modelName}!\n}`
}

let isArray = false

/**
* Otherside is a has many. Hence we should defined a ManyToOne
*/
if (
HasMany.isHasMany(otherSideRelation) ||
DmlManyToMany.isManyToMany(relationship) ||
(BelongsTo.isBelongsTo(otherSideRelation) &&
HasMany.isHasMany(relationship))
HasMany.isHasMany(relationship) ||
DmlManyToMany.isManyToMany(relationship)
) {
isArray = true
}
Expand Down Expand Up @@ -87,10 +66,5 @@ export function setGraphQLRelationship(
pgSchema,
}

return defineRelationships(
entityName,
relationship,
relatedEntity,
relatedEntityInfo
)
return defineRelationships(entityName, relationship, relatedEntityInfo)
}

0 comments on commit 879ce33

Please sign in to comment.