I want to create a simple relation between a user and documents in TypeGraphQL. So a user can create unlimited documents and a document has only one creator. But I am receiving an error.
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity, OneToMany } from "typeorm";
import { Field, ID, ObjectType } from "type-graphql";
import { Doc } from "./Doc";
@ObjectType()
@Entity()
export class User extends BaseEntity {
    @Field(() => ID)
    @PrimaryGeneratedColumn()
    id: number;
    @Field()
    @Column()
    firstName: string;
    @Field()
    @Column()
    lastName: string;
    @Field()
    @Column()
    nickname: string;
    @Field()
    @Column("text", { unique: true })
    email: string;
    @Column()
    password: string;
    @Field({ nullable: true })
    @Column()
    created: Date;
    @Field()
    @Column()
    gender: string;
    @OneToMany(() => Doc, doc => doc.creator)
    createdDocs: Promise<Doc[]>;
}
import { Entity, PrimaryGeneratedColumn, Column, BaseEntity, ManyToOne } from "typeorm";
import { Field, ID, ObjectType } from "type-graphql";
import { User } from "./User";
@ObjectType()
@Entity()
export class Doc extends BaseEntity {
    @Field(() => ID)
    @PrimaryGeneratedColumn()
    id: number;
    @Field({ nullable: true })
    @Column()
    created: Date;
    @Field()
    @Column()
    @ManyToOne(() => User, user => user.createdDocs)
    creator: Promise<User>;
}
throw new errors_1.NoExplicitTypeError(prototype.constructor.name, propertyKey, parameterIndex);
              ^
Error: You need to provide explicit type for Doc#creator !
But what is causing this to happen? Of couse the column creator in the table doc is not a real "data-type", because it shouldn't be "static". It needs to be a relation and this relation can't obviously has a "data-type".
There is no need to declare the relations with @Field() or  @Column()
Error: You need to provide explicit type for Doc#creator !
It means that, when your property type is Promise<User>, the reflected type is Object. TypeGraphQL in that case need explicit type in decorator, like @Field(type => User).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With