Answer a question

I'm setting up an app using NestJS, Angular, GraphQL and MongoDB and new to these stacks I have 2 collections called Tasks & Statuses with sample data

Tasks

[{
  "_id": {
    "$oid": "5f9138f71163a739c43fc9b3"
  },
  "TaskDesc": "Meeting with Brian",
  "StartedDate": "2020-10-22T07:42:40Z",
  "EndDate": "2020-10-22T10:42:40Z",
  "StatusId": "5f91375d1163a739c43fc9af"
}]

Statuses

[{
  "_id": {
    "$oid": "5f91375d1163a739c43fc9af"
  },
  "StatusDesc": "Done"
}]

Here are schemas defined in NestJS

import { Schema, Types } from 'mongoose';
import { MongooseModule } from '@nestjs/mongoose';

export const TaskSchema = new Schema({
    TaskDesc: String,
    StartedDate: String,
    EndDate: String,
    StatusId:  
    {
        type: Types.ObjectId, 
        ref: 'Statuses'
    }
});

export const StatusSchema = new Schema({
    StatusDesc: String
});

export const SchemaGroups = MongooseModule.forFeature([
    {name: 'Tasks', schema: TaskSchema, collection: 'Tasks'},
    {name: 'Statuses', schema: StatusSchema, collection: 'Statuses'}
]);

The DTO

import { ObjectType, Field, ID } from '@nestjs/graphql';

@ObjectType()
export class TasksDTO {
  @Field(() => ID)
  id?: string;
  @Field()
  TaskDesc: string;
  @Field()
  StartedDate: string;
  @Field()
  EndDate: string;
  @Field()
  StatusId: string;
}

@ObjectType()
export class StatusDTO {
  @Field(() => ID)
  readonly id?: string;
  @Field()
  readonly StatusDesc: string;
}

The model

import { Document, Schema } from 'mongoose';
export interface Tasks extends Document {
    readonly TaskDesc : string,
    readonly StartedDate: string,
    readonly EndDate: string,
    readonly StatusId: Schema.Types.ObjectId
}
export interface Status extends Document {
    readonly StatusDesc : string
}

The resolver

@Resolver('Tasks')
export class ListTodoResolver {
    constructor(private readonly todoItemsService: TodolistService){

    }

    @Query(() => [TasksDTO])
    async Tasks(): Promise<TasksDTO[]> {
      return await this.todoItemsService.getAllTasks();
    }

    @Query(() => [StatusDTO])
    async Statuses(): Promise<StatusDTO[]>{
      return await this.todoItemsService.getAllStatuses();
    }
}

The service

import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
@Injectable()
export class TodolistService {
    constructor(
        @InjectModel('Tasks') private readonly todoItemsModel: Model<Tasks>,
        @InjectModel('Statuses') private readonly statusItemsModel: Model<Status>
    ) { }

    async getAllTasks() : Promise<Tasks[]>{
        let tasks = await this.todoItemsModel.find().exec(); // how should we query in the service to get relational data from another collection?
        console.log(tasks);
         
        return tasks;
    }

    async getAllStatuses() : Promise<Status[]>{
        return await this.statusItemsModel.find().exec();
    }
}

And I had these errors Type 'Tasks' is not assignable to type 'TasksDTO'. Types of property 'StatusId' are incompatible. Type 'ObjectId' is not assignable to type 'string'.

How do we define the ObjectId type in the DTO ? My expected output for getAllTasks() method would be

[{
  "_id": "5f9138f71163a739c43fc9b3",
  "TaskDesc": "Meeting with Brian",
  "StartedDate": "2020-10-22T07:42:40Z",
  "EndDate": "2020-10-22T10:42:40Z",
  "StatusDesc": "Done"
}]

Answers

The error is already show where you should fix it.

Type 'Tasks' is not assignable to type 'TasksDTO'. Types of property 'StatusId' are incompatible. Type 'ObjectId' is not assignable to type 'string'.

Which mean your DTO should be

@ObjectType()
export class TasksDTO {
  @Field(() => ID)
  id?: string;
  @Field()
  TaskDesc: string;
  @Field()
  StartedDate: string;
  @Field()
  EndDate: string;
  @Field()
  StatusId: objectId; // change here
}

Not sure the object Id type in your DTO, try to look up in the documentation to see if there are Object ID type for the DTO if not you should change StatusId for all place to string and that should work

Logo

MongoDB社区为您提供最前沿的新闻资讯和知识内容

更多推荐