由于 MongoDB id,无法查询所有用户

Can not Query all users because of MongoDB id

我正在编写 CRUD API 内置 TypeScript 和 TypeGoose。
我收到一条错误消息,

CannotDetermineGraphQLTypeError: Cannot determine GraphQL output type for '_id' of 'User' class. Is the value, that is used as its TS type or explicit type, decorated with a proper decorator or is it a proper output value?

我有一个用户实体。

import { Field, ObjectType } from 'type-graphql';
import { ObjectId } from 'mongodb';
import { prop as Property, getModelForClass } from '@typegoose/typegoose';

@ObjectType()
export class User {
  @Field()
  readonly _id: ObjectId;

  @Field()
  @Property({ required: true })
  email: string;

  @Field({ nullable: true })
  @Property()
  nickname?: string;

  @Property({ required: true })
  password: string;

  constructor(email: string, password: string) {
    this.email = email;
    this.password = password;
  }
}

export const UserModel = getModelForClass(User);

这就是我的查询解析器的样子。

@Query(() => [User])
  async users() {
    const users = await UserModel.find();
    console.log(users);
    return users;
  }

我该如何解决这个问题?似乎 TypeGraphQL 不明白 MongoDB ID 是什么?

我不确定,但也许 ObjectId.toString() 可以帮助您。 MongoDB doc about ObjectId.toString()