From 879ce33090c7e99ae8b466c31c5dc0c67cdbc08f Mon Sep 17 00:00:00 2001 From: Adrien de Peretti Date: Tue, 5 Nov 2024 15:14:28 +0100 Subject: [PATCH] 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 --- .changeset/tame-sloths-pump.md | 5 +++ .../src/dml/__tests__/create-graphql.spec.ts | 4 +- .../graphql-builder/set-relationship.ts | 38 +++---------------- 3 files changed, 13 insertions(+), 34 deletions(-) create mode 100644 .changeset/tame-sloths-pump.md diff --git a/.changeset/tame-sloths-pump.md b/.changeset/tame-sloths-pump.md new file mode 100644 index 0000000000000..bc88cc12c226e --- /dev/null +++ b/.changeset/tame-sloths-pump.md @@ -0,0 +1,5 @@ +--- +"@medusajs/utils": patch +--- + +fix(utils/dml): set-relationship graphql generator from DML wrong managed belongsTo diff --git a/packages/core/utils/src/dml/__tests__/create-graphql.spec.ts b/packages/core/utils/src/dml/__tests__/create-graphql.spec.ts index 5ed1d84594359..0ef2eb4f02eb6 100644 --- a/packages/core/utils/src/dml/__tests__/create-graphql.spec.ts +++ b/packages/core/utils/src/dml/__tests__/create-graphql.spec.ts @@ -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! diff --git a/packages/core/utils/src/dml/helpers/graphql-builder/set-relationship.ts b/packages/core/utils/src/dml/helpers/graphql-builder/set-relationship.ts index 5eaed2b781afe..221f525460808 100644 --- a/packages/core/utils/src/dml/helpers/graphql-builder/set-relationship.ts +++ b/packages/core/utils/src/dml/helpers/graphql-builder/set-relationship.ts @@ -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 | RelationshipType>, - 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) }