ManyToOne 字段未出现在父解析器中

ManyToOne field doesn't appear in Parent resolver

我在处理 NestJS 项目中的实体时遇到了一些问题。我正在使用 TypeOrm 来管理 PostgreSQL 数据库,GraphQL 和 DataLoader 来加载,例如,数据库中的用户。

现在,我有这个实体:

import { ObjectType, Field, ID } from "@nestjs/graphql";
import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn } from "typeorm";
import { Comment } from "./comment.entity";
import { User } from "./user.entity";

@ObjectType()
@Entity({name: 'comments_likes'})
export class CommentLike {

    @Field()
    @PrimaryGeneratedColumn()
    id: number;

    @Field()
    @Column({type: 'timestamptz', nullable: false})
    liked_at: Date

    @Field()
    @ManyToOne(() => Comment, (comment) => comment.id)
    @JoinColumn({name: 'refId'})
    ref: number;

    @Field({nullable: false})
    @ManyToOne(() => User, (user)  => user.id)
    @JoinColumn({name: 'ownerId'})
    owner: number
}

如您所见,我有四个不同的列:idliked_at 时间戳、评论 ID 的 refowner是代表users中的一个用户的数字table.

现在,这是我的解析器:

import { Args, Context, Mutation, Parent, Query, ResolveField, Resolver }    from '@nestjs/graphql';
import { User } from '../../models/user.entity';
import { CommentLike } from '../../models/like_comment.entity';
import { LikeCommentInput } from './input/like-comment.input';
import { LikesCommentService } from './likes-comment.service';
import DataLoader from 'dataloader';

@Resolver(CommentLike)
export class LikesCommentResolver {

    constructor(
        private readonly likesCommentService: LikesCommentService
    ) {}

    @Query(() => CommentLike)
    public async getCommentLikeById(@Args('id') id: number) : Promise<CommentLike> {
        return this.likesCommentService.commentLikeRepo.findOne({id: id});
    }

    @Mutation(() => CommentLike)
    public async addCommentLike(@Args('data') input: LikeCommentInput) : Promise<CommentLike> {
        const newLike = new CommentLike;
        newLike.liked_at = input.liked_at;
        newLike.owner = input.owner
        newLike.ref = input.comment;

        return this.likesCommentService.commentLikeRepo.save(newLike);
    }

    @ResolveField('owner', () => User)
    public async getOwner(@Parent() commentLike: CommentLike, @Context('usersLoader') usersLoader: DataLoader<number, User>) : Promise<User> {
        console.log(commentLike, typeof commentLike);
        return usersLoader.load(commentLike.owner);
    }

}

我的问题出在 getCommentLikeById,当我尝试 运行 这个 GraphQL 查询时:

query {
    getCommentLikeById(id: 2) {
        owner {
            username
        }
    }
}

作为错误,我得到了 The loader.load() function must be called with a value,but got: undefined.,它引用了 ResolveProperty 函数 getOwner。在这个函数中,我添加了一个 console.log 只是为了查看 @Parent 参数,我看到我只收到 idliked_at 时间戳,而不是 owner 字段 ref 字段。

我试图将 {eager: true} 添加到 ManyToOne,但是通过这种方式我收到了所有 User 对象,我只想要 id通过 DataLoader.

加载用户

感谢您提供有关如何执行此操作的任何建议:)!

好的,我用这种方式修复了编辑实体

import { ObjectType, Field, ID } from "@nestjs/graphql";
import { Column, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn, RelationId } from "typeorm";
import { Comment } from "./comment.entity";
import { User } from "./user.entity";

@ObjectType()
@Entity({name: 'comments_likes'})
export class CommentLike {

    @Field()
    @PrimaryGeneratedColumn()
    id: number;

    @Field()
    @Column({type: 'timestamptz', nullable: false})
    liked_at: Date

    @Field()
    @ManyToOne(() => Comment, (comment) => comment.id)
    @JoinColumn({name: 'refId'})
    ref: number;

    @Field({nullable: false})
    @ManyToOne(() => User, (user) => user.id)
    @JoinColumn({name: 'ownerId'})
    owner: User

    @Column()
    ownerId: number
}

刚刚在末尾添加了一个Column,另见

也许这会对遇到同样问题的人有所帮助!