hey I'm using graphQL with NestJS and Apollo Server.
The issue is I can return one object but while returing array of objects it's throwing error.
Please find the response I want to return:
{
data: [
Post { // using typeOrm Repository API, this Post tye is coming from there
id: 1,
uuid: 'f2504a79-dce9-4a14-bb91-162eaa2a75b1',
title: 'Dummy.',
modifiedAt: null
// few more fields
},
Post {
id: 2,
uuid: 'e17afbd6-46fc-4f5f-8181-a045cb38ae27',
title: 'resp.',
modifiedAt: null,
// few more fields
},
],
count: 6
}
Below is my Resolver:
@Query((returns) => ValidatePostRespTypeDto)
async getPost(@Args('getPostData',
new ValidationPipe({ transform: true, transformOptions: { enableImplicitConversion: true } }))
getPostData: GetPostInputType, @Context("req") req: any): Promise<ValidatePostRespTypeDto> {
validateData(getPostData, getPostDataQueryValidator);
return await this.postService.getPostData(getPostData, req);
}
Here Comes the DTO:
import { Field, ObjectType } from "@nestjs/graphql";
@ObjectType()
export class ValidatePostDto {
@Field()
uuid: String;
@Field()
title: String;
@Field()
body: string;
@Field({ nullable: true })
tags?: string;
@Field({ nullable: true })
links?: string;
@Field({ nullable: true })
attachment?: string;
@Field()
createdAt: Date;
@Field({ nullable: true })
modifiedAt?: Date;
}
@ObjectType()
export class ValidatePostRespTypeDto {
@Field(() => ValidatePostDto)
data: ValidatePostDto | ValidatePostDto[];
@Field()
count: number;
}
Can anyone please help me to understand what's the issue here. I'm using below GQL query to get data;
query{
getPost(getPostData: {page: 1, limit: 100}) {
count
data {
uuid
title
body
createdAt
tags
links
attachment
modifiedAt
}
}
}
Error:
"message": "Cannot return null for non-nullable field ValidatePostDto.uuid.",
Can anyone please help me with this.
Thanks in advance.