I am working with 2 Postgres Models – users and posts, where each user can have multiple posts (One to Many Relation). Here I need to make a user profile function to access data of single user along with the associated posts. I have made the following schema.ts file for the same –
import { Field, InputType, Int, ObjectType } from "@nestjs/graphql";
@ObjectType()
class Post {
@Field()
id: string;
@Field()
title: string;
@Field()
description: string;
}
@ObjectType()
export class Users {
@Field()
id: string;
@Field()
email: string;
@Field()
token: string;
@Field()
name: string;
@Field({ nullable: true })
role: string;
@Field({ nullable: true })
posts: [Post]
}
Upon running the same, I receive this error –
Error: Undefined type error. Make sure you are providing an explicit type for the "posts" of the "Users" class.
All I need is to create a schema where I can have array of posts tied to a user object.
New contributor