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:
committed by
GitHub
parent
bbf4af1725
commit
879ce33090
5
.changeset/tame-sloths-pump.md
Normal file
5
.changeset/tame-sloths-pump.md
Normal 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"
|
||||
|
||||
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(),
|
||||
@@ -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!
|
||||
|
||||
@@ -1,33 +1,17 @@
|
||||
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}`
|
||||
@@ -35,14 +19,9 @@ function defineRelationships(
|
||||
|
||||
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
|
||||
}
|
||||
@@ -87,10 +66,5 @@ export function setGraphQLRelationship(
|
||||
pgSchema,
|
||||
}
|
||||
|
||||
return defineRelationships(
|
||||
entityName,
|
||||
relationship,
|
||||
relatedEntity,
|
||||
relatedEntityInfo
|
||||
)
|
||||
return defineRelationships(entityName, relationship, relatedEntityInfo)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user