fix: add foreign keys to the generated query types output (#11388)
This commit is contained in:
5
.changeset/stale-bees-beam.md
Normal file
5
.changeset/stale-bees-beam.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/utils": patch
|
||||
---
|
||||
|
||||
fix: add foreign keys to the generated query types output
|
||||
@@ -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!
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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`)
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user