I want to add an activity to a favorite table:
@Mutation(() => FavoriteDto)
@UseGuards(AuthGuard)
async createFavorite(
@Context() context: any,
@Args('createFavoriteInput') createFavoriteDto: CreateFavoriteInput,
): Promise<FavoriteDto> {
const favorite = await this.favoriteService.create(
context.user!.id,
createFavoriteDto,
);
return this.favoriteMapper.convert(favorite);
}
CreateFavoriteInput should only have the id of the activity, the userId will be save with the current auth session like the following code bellow:
@InputType()
export class CreateFavoriteInput {
@Field()
@IsNotEmpty()
@IsMongoId()
activityId!: string;
}
My DTO has the activity and
@ObjectType()
export class FavoriteDto {
@Field()
id!: string;
@Field(() => ActivityDto)
activity!: ActivityDto;
}
When I tried to generate types I have this error:
Error 0: Cannot query field "activityId" on type "FavoriteDto". Did you mean "activity"?
How can I match my CreateFavoriteInput with my DTO ?
I think there is a misunderstanding between the data you expect and the data you return
Here FavoriteDto is the type you return from your
mutationSo you can't query
activityIdas it's in yourCreateFavoriteInputand not inFavoriteDtoIn your client you can only query
idandactivity(assuming it's returned from your mutation). If you return anactivityIdyou just have to replaceidwithactivityIdin FavoriteDto.Also if you want to be consistent you should replace
with
So that it won't be confusing between DTO types and input types.