Search code examples
node.jsrestgraphqltypeormtypegraphql

TypeORM One-To-One relation foreign key not working with TypeGraphQL


I would like to migrate some endpoints for a REST API developed in NodeJs to GraphQL endpoints. TypeOrm has been used for the database communication, so I figured out that there is a library type-graphql, which can ease the migration phase. However, when it comes down to properties which are referred as foreign keys e.g.:

@OneToOne(() => SomeModel)
@JoinColumn()
someModel!: SomeModel;

the decorator @Field does not help, as it considers the property someModel as null. Thought of changing the property to a column, but that would mess up the database table, since it should not be included as a column.

Has anybody encountered a similar situation and managed to solve it?


Solution

  • I managed to solve the issue by wrapping the property type with a promise

    @Field(() => SomeModel)
    @OneToOne(() => SomeModel)
    @JoinColumn()
    someModel!: Promise<SomeModel>;
    

    I got the hint in the documentation of TypeGraphQL, underneath the section about Interoperable