Cannot determine GraphQL input type for argument named

怎甘沉沦 提交于 2021-01-29 07:06:59

问题


I have two relationed models:

1.- RoleEntity

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.- UserEntity

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;
}

And this is the resolver for user:

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;
    }
}

but, i get this error:

(node:14788) UnhandledPromiseRejectionWarning: Error: Cannot determine GraphQL input type for argument named 'role' of 'createUser' of 'UserResolver' class. Does the value used as its TS type or explicit type is decorated with a proper decorator or is it a proper input value?

I need your help please.


回答1:


I solved my problem, I modified my two models, in UserEntity I removed:

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

and i added

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

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

and in RoleEntity, i added:

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

But, I don't know if I'm doing my test in right way (Playground):

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

and part of the error is:

"message": "Cannot return null for non-nullable field UserEntity.role.",

Somebody can explain to me, what or where is the error?

Thanks.




回答2:


I fixed adding this in 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);
    }

Thanks.




回答3:


The error is because you can't use ObjectType arguments in mutations¹, just plain scalar types or InputType:
https://typegraphql.com/docs/resolvers.html#input-types

¹: the RoleEntity ObjectType in your case.



来源:https://stackoverflow.com/questions/63681836/cannot-determine-graphql-input-type-for-argument-named

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!