typescript 无法确定名为的参数的GraphQL输入类型

zqdjd7g9  于 2023-01-06  发布在  TypeScript
关注(0)|答案(4)|浏览(152)

我有两个相关的模型:
1.-角色实体

import { Column, Entity, BaseEntity, OneToMany, PrimaryColumn } from "typeorm";
import { Field, ObjectType } from "type-graphql";

import { UserEntity } from "./user.entity";

@ObjectType()
@Entity({
    name: "tb_roles"
})
export class RoleEntity extends BaseEntity {

    @Field()
    @PrimaryColumn({
        name: "id",
        type: "character varying",
        length: 5
    })
    id!: string;

    @Field()
    @Column({
        name: "description",
        type: "character varying",
        nullable: true
    })
    description!: string

    @Field(() => [UserEntity])
    @OneToMany(() => UserEntity, user => user.role)
    users!: UserEntity[];
}

2.-用户实体

import {Field, ObjectType} from "type-graphql";
import { BaseEntity, Column, CreateDateColumn, Entity, JoinColumn, ManyToOne, PrimaryGeneratedColumn, UpdateDateColumn } from "typeorm";

import { RoleEntity } from "./role.entity";

@ObjectType()
@Entity({
    name: "tb_users"
})
export class UserEntity extends BaseEntity {
    @Field()
    @PrimaryGeneratedColumn("uuid", {
        name: "id"
    })
    id!: string;

    @Field()
    @Column({
        name: "username",
        type: "character varying",
        unique: true,
        nullable: false
    })
    username!: string;

    @Field()
    @Column({
        name: "last_name",
        type: "character varying",
        nullable: false
    })
    lastName!: string;

    @Field()
    @Column({
        name: "mother_last_name",
        type: "character varying",
        nullable: true
    })
    motherLastName!: string;

    @Field()
    @Column({
        name: "first_name",
        type: "character varying",
        nullable: false
    })
    firstName!: string;

    @Field()
    @Column({
        name: "middle_name",
        type: "character varying",
        nullable: true
    })
    middleName!: string;

    @Field()
    @Column({
        name: "email",
        type: "character varying",
        unique: true,
        nullable: false
    })
    email!: string;

    @Field()
    @Column({
        name: "password",
        type: "character varying",
        nullable: false
    })
    password!: string;

    @Field(() => RoleEntity)
    @ManyToOne(() => RoleEntity)
    @JoinColumn({name: "role_id"})
    role!: RoleEntity;

    @Field()
    @Column({
        name: "is_active",
        type: "character varying",
        nullable: true
    })
    isActive!: boolean;

    @Field()
    @CreateDateColumn({
        name: "created_at"
    })
    createdAt!: string;

    @Field()
    @UpdateDateColumn({
        name: "updated_at"
    })
    updatedAt!: string;
}

这是用户的解析程序:

import {Arg, Mutation, Query, Resolver} from "type-graphql";
import bcrypt from "bcryptjs";

import {UserEntity} from "../../entity/user.entity";
import {RoleEntity} from "../../entity/role.entity";

@Resolver()
export class UserResolver {
    @Query(() => [UserEntity])
    async users() {
        return await UserEntity.find();
    }

    @Mutation(() => UserEntity)
    async createUser(
        @Arg('username') username: string,
        @Arg('lastName') lastName: string,
        @Arg('motherLastName') motherLastName: string,
        @Arg('firstName') firstName: string,
        @Arg('middleName') middleName: string,
        @Arg('email') email: string,
        @Arg('password') password: string,
        @Arg('role') role: RoleEntity,
        @Arg('isActive') isActive: boolean
    ): Promise<UserEntity> {

        const hashedPassword = await bcrypt.hashSync(password, bcrypt.genSaltSync(10));

        const user = UserEntity.create({
            username,
            lastName,
            motherLastName,
            firstName,
            middleName,
            email,
            password: hashedPassword,
            role,
            isActive
        }).save();

        return user;
    }
}

但是,我得到这个错误:
(node:14788)未处理承诺拒绝警告:错误:无法确定“UserResolver”类的“createUser”的名为“role”的参数的GraphQL输入类型。用作其TS类型或显式类型的值是否使用正确的装饰器装饰,或者它是否是正确的输入值?
我需要你的帮助。

hts6caw3

hts6caw31#

这个错误是因为不能在mutation ¹中使用ObjectType参数,只能使用普通标量类型或InputType
https://typegraphql.com/docs/resolvers.html#input-types
¹:在您的情况下为RoleEntity对象类型。

qlzsbp2j

qlzsbp2j2#

我解决了我的问题,我修改了我的两个模型,在UserEntity中我删除了:

@Field(() => RoleEntity)
    @ManyToOne(() => RoleEntity)
    @JoinColumn({name: "role_id"})
    role!: RoleEntity;

然后我补充道

@Field()
@Column({name: 'role_id'})
roleId!: string;

@Field(() => RoleEntity)
role!: RoleEntity;
@ManyToOne(() => RoleEntity, role => role.userConnection)
@JoinColumn({name: 'role_id'})
roleConnection!: Promise<RoleEntity>

在RoleEntity中,我添加了:

@OneToMany(() => UserEntity, user => user.roleConnection)
userConnection!: Promise<UserEntity[]>

但是,我不知道我是否以正确的方式进行测试(Playground):

query users {
  users {
    firstName
    username
    roleId
    role {
      description
    }
  }
}

其中一部分错误是
"消息":"无法为不可为空的字段UserEntity.role返回空值。",
有人能给我解释一下,错误在哪里?
谢谢。

3ks5zfa0

3ks5zfa03#

我修复了在userResolver中添加此问题:

@FieldResolver()
    async role(@Root() user: UserEntity): Promise<RoleEntity | undefined> {
        //const role: RoleEntity | undefined = await RoleEntity.findOne(user.roleId);
        //return role;
        return await RoleEntity.findOne(user.roleId);
    }

谢谢。

rdrgkggo

rdrgkggo4#

您可以通过使用@nestjs/graphql中的registerEnumType注册枚举来消除此错误
import {registerEnumType} from '@nestjs/graphql'
示例:registerEnumType(OrderStatus, { name: 'OrderStatus' });

相关问题