我正在使用 mikro-orm 生成 graphql 模式,我的一对一关系中的所有字段都需要返回。我如何让它们成为可选的,以便我的查询在字段返回 null 时不会抛出错误?这是我在 ticket.entity.ts 中定义关系的方式
@Field(() => Order, { nullable: true })
@OneToOne(() => Order, null, { nullable: true })
public order?: Order;
在我生成的 ticket.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;
...
...
...
我的订单实体中没有与票证的一对一关系。票有订单,但订单不一定有票。我在文档中没有看到单向关系,所以我想我会把它放在这里,以防它与我的问题有关。
答案 0 :(得分:0)
我的字段需要在订单实体中可以为空。这是一个 @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;