错误:使用Nestjs + Graphql + Typeorm时无法确定GraphQL输入类型

时间:2020-11-08 10:13:46

标签: node.js typescript graphql nestjs typeorm

我正在使用Nestjs,Graphql和Typeorm,但我是新手。 我目前在这个问题上很烂。我正在使用@ManyToOne关系将食物与用户链接。 但我不断收到错误消息,说我的课不正确?但是我导入了它,然后看着https://docs.nestjs.com/techniques/database#relations似乎所有语法都是正确的。

看看ManyToOne下的food.entity,我叫usertest(出于测试目的)

在food.entity.ts

import { ObjectType, Field, ID } from '@nestjs/graphql';
import { Entity, DeepPartial, Column, PrimaryGeneratedColumn, ManyToOne } from 'typeorm';
import { AbstractEntity } from '@shared/entities/abstract.entity';
import { User } from '../user/user.entity';

@ObjectType('Food')
@Entity('foods')
export class Food extends AbstractEntity {
    constructor(input?: DeepPartial<Food>) {
        super(input);
    }

    @Field((type) => ID)
    @PrimaryGeneratedColumn()
    id: number;

    @Field({ nullable: false })
    @Column({ type: 'varchar', length: 255, nullable: false })
    name: string;

    @ManyToOne(() => User, (user) => user.id)
    usertest: User;

}

在user.entity.ts

@ObjectType('User')
@Entity('users')
export class User extends AbstractEntity {
    constructor(input?: DeepPartial<User>) {
        super(input);
    }

    @Field((type) => ID)
    @PrimaryGeneratedColumn('uuid')
    id: string;

    @Field({ nullable: true })
    @Column({ unique: true })
    email: string;

    @Field({ nullable: true })
    @Column({ unique: true })
    phoneNumber: string;

    @Field({ nullable: true })
    @Column({ default: false })
    verified: boolean;

    @Field({ nullable: true })
    @Column({ default: false })
    suspended: boolean;

    @Field({ nullable: true })
    @Column({ length: 255, nullable: true })
    name?: string;

    @Field({ nullable: true })
    @Column({ length: 120, nullable: true })
    firstName?: string;

    @Field({ nullable: true })
    @Column({ length: 120, nullable: true })
    lastName?: string;

    @Field((type) => UserRole, { nullable: true })
    @Column({ type: 'enum', enum: UserRole })
    role: UserRole;

    @Column()
    password: string;
}

我收到的错误:

(node:58449) UnhandledPromiseRejectionWarning: Error: Cannot determine a GraphQL input type for the "usertest". Make sure your class is decorated with an appropriate decorator.
    at InputTypeFactory.create (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type.factory.js:19:23)
    at /Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type-definition.factory.js:44:52
    at Array.forEach (<anonymous>)
    at /Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type-definition.factory.js:42:33
    at resolveThunk (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:478:40)
    at defineInputFieldMap (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:1203:18)
    at GraphQLInputObjectType.getFields (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:1151:27)
    at TypeFieldsAccessor.extractFromInputType (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/services/type-fields.accessor.js:9:35)
    at /Users/ivan/Documents/Programming/Baker/api-core/node_modules/@nestjs/graphql/dist/schema-builder/factories/input-type-definition.factory.js:56:66
    at resolveThunk (/Users/ivan/Documents/Programming/Baker/api-core/node_modules/graphql/type/definition.js:478:40)

1 个答案:

答案 0 :(得分:0)

缺少@InputType()

@ObjectType('User')
@InputType('UserInput')
@Entity('users')

我不确定这是正确的方法还是正确的做法,但这对我有用。