How to declare Array Of Objects in TypeORM Graphql Schema?

How to declare Array Of Objects in TypeORM Graphql Schema?


0

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

Abhinav Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

1 Answer
1


0

Found the answer by reading the docs.

  @Field(type => [Post])
  posts: Post[]

New contributor

Abhinav Sharma is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.



Leave a Reply

Your email address will not be published. Required fields are marked *