Как обернуть _id из mongoose Document в @Field() для добавления его в схему GraphQL

import { Field, ObjectType } from '@nestjs/graphql';
import { SchemaFactory } from '@nestjs/mongoose';
import { Document } from 'mongoose';
import { Role } from 'src/enums/role.enum';

export type UsersDocument = Users & Document;

@ObjectType()
export class Users {
  @Field(() => String)
  username: string;
  @Field(() => String)
  password: string;
  @Field(() => String)
  role: Role;
  @Field(() => String)
  sideInfo: string;
  @Field(() => [String])
  projects: string[];
}
export const UsersSchema = SchemaFactory.createForClass(Users);

Есть модель пользователя, класс UserDocument получает поля от User (видно в коде) и автоматические сгенерированные поля {_id, __v} от каласса Document библиотеки 'mongoose', как обернуть этот _id в @Field() для того чтобы он был включен в схему GraphQL?


Ответы (1 шт):

Автор решения: just_simple_name

Если кому интересно, решение следующее:

import { Field, ObjectType } from '@nestjs/graphql';
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import { Document, Types } from 'mongoose';
import { Role } from 'src/enums/role.enum';

export type UsersDocument = Users & Document;

@Schema()
@ObjectType()
export class Users {
  @Field(() => String)
  _id: Types.ObjectId;
  @Prop()
  @Field(() => String)
  username: string;
  @Prop()
  @Field(() => String)
  password: string;
  @Prop()
  @Field(() => String)
  role: Role;
  @Prop()
  @Field(() => String)
  sideInfo: string;
  @Prop()
  @Field(() => [String])
  projects: string[];
}

export const UsersSchema = SchemaFactory.createForClass(Users);
→ Ссылка