在 TypeGraphQL 中使用 ManyToOne 关系 returns null
Usage of ManyToOne relation returns null in TypeGraphQL
为了学习,我做了一个项目来测试 TypeGraphql 和 TypeORM。我有一个 User
和 Book
实体,我想在 Book
实体上有一个 created_by
字段。
@ObjectType()
@Entity()
export class Book extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
readonly id: number;
@Field({ nullable: true })
@Column({ nullable: true })
name: string;
@Field(() => User)
@ManyToOne(() => User)
@JoinColumn({ name: 'created_by' })
created_by: User;
// @RelationId((orthodontist: Orthodontist) => orthodontist.created_by)
// createdById: number;
}
@ObjectType()
@Entity()
export class User extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
id: number;
@Field({ nullable: true })
@Column({ nullable: true })
first_name?: string;
@Field({ nullable: true })
@Column({ nullable: true })
last_name?: string;
@Field()
@Column({ unique: true })
email: string;
@Column()
password: string;
@Field(() => [Book!])
@OneToMany(() => Book, book => book.created_by)
created_books: Book[];
}
对于我的解析器,它看起来像这样,我按照文档所说正确加载了它。
@Resolver(Book)
export class OrthodontistResolver {
@Query(() => [Book])
books(): Promise<Book[]> {
return Book.find();
}
}
当我进入我的 GraphQL playground 并查询如下内容时:
{
books {
name,
id,
}
}
一切正常,returns 数据正确。但是,当我尝试像这样使用 created_by
字段时:
{
orthodontists {
name,
id,
created_by {
id
}
}
}
它给我以下错误:
Cannot return null for non-nullable field Book.created_by.
我确定该关系存在于数据库中,并使用它的 FK 进行了正确设置。这是从哪里来的?我怎样才能解决这个问题?我确实尝试使用 @RelationId
装饰器,如第一个代码示例所示。不幸的是,它没有用。
编辑:
数据库中只有一本书,其中 created_by
字段不为空。
使用 find
操作时,将您的 books
解析器更改为 return 关系 created_by
:
@Resolver(Book)
export class OrthodontistResolver {
@Query(() => [Book])
books(): Promise<Book[]> {
return Book.find({
relations: ["created_by"],
});
}
}
为了学习,我做了一个项目来测试 TypeGraphql 和 TypeORM。我有一个 User
和 Book
实体,我想在 Book
实体上有一个 created_by
字段。
@ObjectType()
@Entity()
export class Book extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
readonly id: number;
@Field({ nullable: true })
@Column({ nullable: true })
name: string;
@Field(() => User)
@ManyToOne(() => User)
@JoinColumn({ name: 'created_by' })
created_by: User;
// @RelationId((orthodontist: Orthodontist) => orthodontist.created_by)
// createdById: number;
}
@ObjectType()
@Entity()
export class User extends BaseEntity {
@Field(() => ID)
@PrimaryGeneratedColumn()
id: number;
@Field({ nullable: true })
@Column({ nullable: true })
first_name?: string;
@Field({ nullable: true })
@Column({ nullable: true })
last_name?: string;
@Field()
@Column({ unique: true })
email: string;
@Column()
password: string;
@Field(() => [Book!])
@OneToMany(() => Book, book => book.created_by)
created_books: Book[];
}
对于我的解析器,它看起来像这样,我按照文档所说正确加载了它。
@Resolver(Book)
export class OrthodontistResolver {
@Query(() => [Book])
books(): Promise<Book[]> {
return Book.find();
}
}
当我进入我的 GraphQL playground 并查询如下内容时:
{
books {
name,
id,
}
}
一切正常,returns 数据正确。但是,当我尝试像这样使用 created_by
字段时:
{
orthodontists {
name,
id,
created_by {
id
}
}
}
它给我以下错误:
Cannot return null for non-nullable field Book.created_by.
我确定该关系存在于数据库中,并使用它的 FK 进行了正确设置。这是从哪里来的?我怎样才能解决这个问题?我确实尝试使用 @RelationId
装饰器,如第一个代码示例所示。不幸的是,它没有用。
编辑:
数据库中只有一本书,其中 created_by
字段不为空。
使用 find
操作时,将您的 books
解析器更改为 return 关系 created_by
:
@Resolver(Book)
export class OrthodontistResolver {
@Query(() => [Book])
books(): Promise<Book[]> {
return Book.find({
relations: ["created_by"],
});
}
}