【问题标题】:ManyToOne field doesn't appear in Parent resolverManyToOne 字段未出现在父解析器中
【发布时间】:2021-08-10 22:01:26
【问题描述】:

我在使用 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 时间戳、ref 到评论 ID,以及 owner 是代表用户表中的用户的数字.

现在,这是我的解析器:

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 加载用户。

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

【问题讨论】:

    标签: graphql nestjs typeorm resolver


    【解决方案1】:

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

    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,另见this answer

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 2019-07-14
      • 1970-01-01
      • 2018-11-02
      • 1970-01-01
      • 2021-01-13
      相关资源
      最近更新 更多