mongodbgraphqltypegraphqltypegoose

Can not Query all users because of MongoDB id


I am coding a CRUD API built in TypeScript and TypeGoose.
I get an error saying,

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?

I have a User entity.

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);

And this is how my query resolver looks like.

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

How can I solve this? It seems to be like TypeGraphQL doesn't understand what the MongoDB ID is?


Solution

  • Im not sure about this, but maybe ObjectId.toString() help you. MongoDB doc about ObjectId.toString()