Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relations don't work properly in TypeGraphQL (You need to provide explicit type for...)

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.

User

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

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

Error

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

like image 528
dewey Avatar asked Nov 05 '19 14:11

dewey


2 Answers

I've found my mistake...

There is no need to declare the relations with @Field() or @Column()

like image 113
dewey Avatar answered Oct 19 '22 07:10

dewey


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

like image 38
Michał Lytek Avatar answered Oct 19 '22 08:10

Michał Lytek