我正在使用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;
...
...
...我与我的订单实体中的票证没有一对一的关系。门票有订单,但订单不一定有门票。我在文档中看不到单向关系,所以我想我应该把它放在这里,以防它与我的问题有关。
发布于 2021-06-11 02:44:04
我的字段在order实体中必须是可空的。这是一个与@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;https://stackoverflow.com/questions/67925622
复制相似问题