将Mikro ORM关系字段设为可选字段



我正在使用mikro-orm生成graphql模式,并且我的一对一关系中的所有字段都是必需的。如何使它们是可选的,这样当字段返回null时,我的查询就不会抛出错误?以下是我如何在ticket.entity.ts 中定义关系

@Field(() => Order, { nullable: true })
@OneToOne(() => Order, null, { nullable: true })
public order?: Order;

在我生成的tickets.schema.graphql中,Order对象返回以下内容:

type Order {
id: ID!
createdAt: DateTime!
updatedAt: DateTime!
archivedAt: DateTime
client: String!
soldDate: DateTime!
type: String!
...
...
...
}

在我的Order实体中,所有字段都是可选的,生成的SQL表将它们理解为可选的。

export class Order extends BaseEntity {
@Field(() => String)
@Property({ nullable: true })
public client: string;
@Field(() => Date)
@Property({ columnType: "date", nullable: true })
public soldDate: Date;
@Field(() => String)
@Property({ nullable: true })
public type: string;
...
...
...

我与订单实体中的Ticket没有一对一关系。门票有订单,但订单不一定有门票。我在文档中没有看到单向关系,所以我想我会把它放在这里,以防它与我的问题有关。

订单实体中我的字段需要为null。这是一个与@nestjs/graphql相关的问题,而不是与mikro-orm相关的问题。

export class Order extends BaseEntity {
@Field(() => String, { nullable: true} )
@Property({ nullable: true })
public client: string;
@Field(() => Date, { nullable: true} )
@Property({ columnType: "date", nullable: true })
public soldDate: Date;
@Field(() => String, { nullable: true} )
@Property({ nullable: true })
public type: string;

最新更新