Skip to content

Commit

Permalink
fix: add foreign keys to the generated query types output
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Feb 11, 2025
1 parent 1e7c072 commit c78097b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 10 deletions.
23 changes: 23 additions & 0 deletions packages/core/utils/src/dml/helpers/create-graphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
@@ -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`)
)
}

0 comments on commit c78097b

Please sign in to comment.