fix: add foreign keys to the generated query types output (#11388)

This commit is contained in:
Harminder Virk
2025-02-11 14:16:10 +05:30
committed by GitHub
parent eb74167261
commit 244cd714b2
6 changed files with 49 additions and 10 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/utils": patch
---
fix: add foreign keys to the generated query types output

View File

@@ -31,6 +31,7 @@ describe("GraphQL builder", () => {
const group = model.define("group", {
id: model.number(),
name: model.text(),
admin: model.hasOne(() => user, { foreignKey: true }),
users: model.hasMany(() => user),
})
@@ -70,6 +71,7 @@ describe("GraphQL builder", () => {
email: Email!
spend_limit: String!
phones: [String]!
group_id:String!
group: Group!
role: UserRoleEnum!
tags: [Tag]!
@@ -82,6 +84,8 @@ describe("GraphQL builder", () => {
type Group {
id: Int!
name: String!
admin_id: String!
admin: User!
users: [User]!
created_at: DateTime!
updated_at: DateTime!

View File

@@ -3,6 +3,7 @@ import { DmlEntity } from "../entity"
import { parseEntityName } from "./entity-builder/parse-entity-name"
import { setGraphQLRelationship } from "./graphql-builder/set-relationship"
import { getGraphQLAttributeFromDMLPropety } from "./graphql-builder/get-attribute"
import { getForeignKey } from "./entity-builder"
export function generateGraphQLFromEntity<T extends DmlEntity<any, any>>(
entity: T
@@ -29,6 +30,28 @@ export function generateGraphQLFromEntity<T extends DmlEntity<any, any>>(
gqlSchema.push(`${prop.attribute}`)
} else {
if (["belongsTo", "hasOneWithFK"].includes(field.type)) {
const foreignKeyName = getForeignKey(field)
const fkProp = getGraphQLAttributeFromDMLPropety(
modelName,
field.name,
{
$dataType: "",
parse() {
return {
fieldName: foreignKeyName,
computed: false,
dataType: { name: "text" as const },
nullable: field.nullable || false,
indexes: [],
relationships: [],
}
},
}
)
gqlSchema.push(`${fkProp.attribute}`)
}
const prop = setGraphQLRelationship(modelName, field)
if (prop.extra) {
extra.push(prop.extra)

View File

@@ -27,6 +27,7 @@ import { HasOneWithForeignKey } from "../../relations/has-one-fk"
import { ManyToMany as DmlManyToMany } from "../../relations/many-to-many"
import { applyEntityIndexes } from "../mikro-orm/apply-indexes"
import { parseEntityName } from "./parse-entity-name"
import { getForeignKey } from "./relationship-helpers"
type Context = {
MANY_TO_MANY_TRACKED_RELATIONS: Record<string, boolean>
@@ -183,10 +184,7 @@ export function defineHasOneWithFKRelationship(
{ relatedModelName }: { relatedModelName: string },
cascades: EntityCascades<string[], string[]>
) {
const foreignKeyName =
relationship.options.foreignKeyName ??
camelToSnakeCase(`${relationship.name}Id`)
const foreignKeyName = getForeignKey(relationship)
const shouldRemoveRelated = !!cascades.delete?.includes(relationship.name)
let mappedBy: string | undefined = camelToSnakeCase(MikroORMEntity.name)
@@ -428,9 +426,7 @@ export function defineBelongsToRelationship(
HasMany.isHasMany(otherSideRelation) ||
DmlManyToMany.isManyToMany(otherSideRelation)
) {
const foreignKeyName =
relationship.options.foreignKeyName ??
camelToSnakeCase(`${relationship.name}Id`)
const foreignKeyName = getForeignKey(relationship)
const detachCascade =
!!relationship.mappedBy &&
relationCascades.detach?.includes(relationship.mappedBy)
@@ -491,9 +487,7 @@ export function defineBelongsToRelationship(
HasOne.isHasOne(otherSideRelation) ||
HasOneWithForeignKey.isHasOneWithForeignKey(otherSideRelation)
) {
const foreignKeyName =
relationship.options.foreignKeyName ??
camelToSnakeCase(`${relationship.name}Id`)
const foreignKeyName = getForeignKey(relationship)
Property({
columnType: "text",
type: "string",

View File

@@ -6,3 +6,4 @@ export * from "./define-property"
export * from "./define-relationship"
export * from "./parse-entity-name"
export * from "./query-builder"
export * from "./relationship-helpers"

View File

@@ -0,0 +1,12 @@
import { RelationshipMetadata } from "@medusajs/types"
import { camelToSnakeCase } from "../../../common/camel-to-snake-case"
/**
* Returns the foreign key name for a relationship
*/
export function getForeignKey(relationship: RelationshipMetadata) {
return (
relationship.options.foreignKeyName ??
camelToSnakeCase(`${relationship.name}Id`)
)
}